Parolin 0.7.9 6796
Console (soon DLLs) to do a tar like job
Loading...
Searching...
No Matches
Threads.h
Go to the documentation of this file.
1/* Threads.h -- multithreading library
22023-04-02 : Igor Pavlov : Public domain */
3
4#ifndef ZIP7_INC_THREADS_H
5#define ZIP7_INC_THREADS_H
6
7#ifdef _WIN32
8#include "7zWindows.h"
9
10#else
11
12#if defined(__linux__)
13#if !defined(__APPLE__) && !defined(_AIX) && !defined(__ANDROID__)
14#ifndef Z7_AFFINITY_DISABLE
15#define Z7_AFFINITY_SUPPORTED
16// #pragma message(" ==== Z7_AFFINITY_SUPPORTED")
17// #define _GNU_SOURCE
18#endif
19#endif
20#endif
21
22#include <pthread.h>
23
24#endif
25
26#include "7zTypes.h"
27
29
30#ifdef _WIN32
31
32WRes HandlePtr_Close(HANDLE *h);
34
35typedef HANDLE CThread;
36
37#define Thread_CONSTRUCT(p) { *(p) = NULL; }
38#define Thread_WasCreated(p) (*(p) != NULL)
39#define Thread_Close(p) HandlePtr_Close(p)
40// #define Thread_Wait(p) Handle_WaitObject(*(p))
41
42#ifdef UNDER_CE
43 // if (USE_THREADS_CreateThread is defined), we use _beginthreadex()
44 // if (USE_THREADS_CreateThread is not definned), we use CreateThread()
45 #define USE_THREADS_CreateThread
46#endif
47
48typedef
49 #ifdef USE_THREADS_CreateThread
50 DWORD
51 #else
52 unsigned
53 #endif
55
56#define THREAD_FUNC_RET_ZERO 0
57
59typedef DWORD_PTR CCpuSet;
60
61#define CpuSet_Zero(p) *(p) = (0)
62#define CpuSet_Set(p, cpu) *(p) |= ((DWORD_PTR)1 << (cpu))
63
64#else // _WIN32
65
66typedef struct
67{
68 pthread_t _tid;
69 int _created;
70} CThread;
71
72#define Thread_CONSTRUCT(p) { (p)->_tid = 0; (p)->_created = 0; }
73#define Thread_WasCreated(p) ((p)->_created != 0)
75// #define Thread_Wait Thread_Wait_Close
76
77typedef void * THREAD_FUNC_RET_TYPE;
78#define THREAD_FUNC_RET_ZERO NULL
79
80
81typedef UInt64 CAffinityMask;
82
83#ifdef Z7_AFFINITY_SUPPORTED
84
85typedef cpu_set_t CCpuSet;
86#define CpuSet_Zero(p) CPU_ZERO(p)
87#define CpuSet_Set(p, cpu) CPU_SET(cpu, p)
88#define CpuSet_IsSet(p, cpu) CPU_ISSET(cpu, p)
89
90#else
91
92typedef UInt64 CCpuSet;
93#define CpuSet_Zero(p) *(p) = (0)
94#define CpuSet_Set(p, cpu) *(p) |= ((UInt64)1 << (cpu))
95#define CpuSet_IsSet(p, cpu) ((*(p) & ((UInt64)1 << (cpu))) != 0)
96
97#endif
98
99
100#endif // _WIN32
101
102
103#define THREAD_FUNC_CALL_TYPE Z7_STDCALL
104
105#if defined(_WIN32) && defined(__GNUC__)
106/* GCC compiler for x86 32-bit uses the rule:
107 the stack is 16-byte aligned before CALL instruction for function calling.
108 But only root function main() contains instructions that
109 set 16-byte alignment for stack pointer. And another functions
110 just keep alignment, if it was set in some parent function.
111
112 The problem:
113 if we create new thread in MinGW (GCC) 32-bit x86 via _beginthreadex() or CreateThread(),
114 the root function of thread doesn't set 16-byte alignment.
115 And stack frames in all child functions also will be unaligned in that case.
116
117 Here we set (force_align_arg_pointer) attribute for root function of new thread.
118 Do we need (force_align_arg_pointer) also for another systems? */
119
120 #define THREAD_FUNC_ATTRIB_ALIGN_ARG __attribute__((force_align_arg_pointer))
121 // #define THREAD_FUNC_ATTRIB_ALIGN_ARG // for debug : bad alignment in SSE functions
122#else
123 #define THREAD_FUNC_ATTRIB_ALIGN_ARG
124#endif
125
126#define THREAD_FUNC_DECL THREAD_FUNC_ATTRIB_ALIGN_ARG THREAD_FUNC_RET_TYPE THREAD_FUNC_CALL_TYPE
127
132
133#ifdef _WIN32
134#define Thread_Create_With_CpuSet(p, func, param, cs) \
135 Thread_Create_With_Affinity(p, func, param, *cs)
136#else
138#endif
139
140
141#ifdef _WIN32
142
143typedef HANDLE CEvent;
144typedef CEvent CAutoResetEvent;
146#define Event_Construct(p) *(p) = NULL
147#define Event_IsCreated(p) (*(p) != NULL)
148#define Event_Close(p) HandlePtr_Close(p)
149#define Event_Wait(p) Handle_WaitObject(*(p))
156
157typedef HANDLE CSemaphore;
158#define Semaphore_Construct(p) *(p) = NULL
159#define Semaphore_IsCreated(p) (*(p) != NULL)
160#define Semaphore_Close(p) HandlePtr_Close(p)
161#define Semaphore_Wait(p) Handle_WaitObject(*(p))
162WRes Semaphore_Create(CSemaphore *p, UInt32 initCount, UInt32 maxCount);
163WRes Semaphore_OptCreateInit(CSemaphore *p, UInt32 initCount, UInt32 maxCount);
166
167typedef CRITICAL_SECTION CCriticalSection;
169#define CriticalSection_Delete(p) DeleteCriticalSection(p)
170#define CriticalSection_Enter(p) EnterCriticalSection(p)
171#define CriticalSection_Leave(p) LeaveCriticalSection(p)
172
173
174#else // _WIN32
175
176typedef struct _CEvent
177{
181 pthread_mutex_t _mutex;
182 pthread_cond_t _cond;
184
185typedef CEvent CAutoResetEvent;
187
188#define Event_Construct(p) (p)->_created = 0
189#define Event_IsCreated(p) ((p)->_created)
190
195
200
201
202typedef struct _CSemaphore
203{
207 pthread_mutex_t _mutex;
208 pthread_cond_t _cond;
210
211#define Semaphore_Construct(p) (p)->_created = 0
212#define Semaphore_IsCreated(p) ((p)->_created)
213
214WRes Semaphore_Create(CSemaphore *p, UInt32 initCount, UInt32 maxCount);
215WRes Semaphore_OptCreateInit(CSemaphore *p, UInt32 initCount, UInt32 maxCount);
217#define Semaphore_Release1(p) Semaphore_ReleaseN(p, 1)
220
221
222typedef struct _CCriticalSection
223{
224 pthread_mutex_t _mutex;
226
231
232LONG InterlockedIncrement(LONG volatile *addend);
233
234#endif // _WIN32
235
237
239
240#endif
INT32 LONG
Definition 7zTypes.h:190
int WRes
Definition 7zTypes.h:75
#define EXTERN_C_BEGIN
Definition 7zTypes.h:20
unsigned long DWORD_PTR
Definition 7zTypes.h:207
unsigned long long int UInt64
Definition 7zTypes.h:234
void * LPVOID
Definition 7zTypes.h:200
UINT32 DWORD
Definition 7zTypes.h:194
#define EXTERN_C_END
Definition 7zTypes.h:21
WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 num)
Definition Threads.c:458
WRes CriticalSection_Init(CCriticalSection *p)
Definition Threads.c:505
void * THREAD_FUNC_RET_TYPE
Definition Threads.h:86
WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p)
Definition Threads.c:368
WRes Thread_Create_With_CpuSet(CThread *p, THREAD_FUNC_TYPE func, LPVOID param, const CCpuSet *cpuSet)
Definition Threads.c:227
WRes Event_Set(CEvent *p)
Definition Threads.c:377
UInt64 CCpuSet
Definition Threads.h:101
WRes Semaphore_Create(CSemaphore *p, UInt32 initCount, UInt32 maxCount)
Definition Threads.c:425
#define THREAD_FUNC_CALL_TYPE
Definition Threads.h:112
CEvent CManualResetEvent
Definition Threads.h:195
WRes Thread_Create(CThread *p, THREAD_FUNC_TYPE func, LPVOID param)
Definition Threads.c:295
WRes ManualResetEvent_Create(CManualResetEvent *p, int signaled)
Definition Threads.c:362
UInt64 CAffinityMask
Definition Threads.h:90
WRes Semaphore_OptCreateInit(CSemaphore *p, UInt32 initCount, UInt32 maxCount)
Definition Threads.c:438
WRes AutoResetEvent_OptCreate_And_Reset(CAutoResetEvent *p)
Definition Threads.c:573
WRes Thread_Create_With_Affinity(CThread *p, THREAD_FUNC_TYPE func, LPVOID param, CAffinityMask affinity)
Definition Threads.c:301
CEvent CAutoResetEvent
Definition Threads.h:194
THREAD_FUNC_RET_TYPE(THREAD_FUNC_CALL_TYPE * THREAD_FUNC_TYPE)(void *)
Definition Threads.h:137
#define Semaphore_Release1(p)
Definition Threads.h:226
WRes Thread_Wait_Close(CThread *p)
Definition Threads.c:335
WRes AutoResetEvent_Create(CAutoResetEvent *p, int signaled)
Definition Threads.c:366
WRes Event_Reset(CEvent *p)
Definition Threads.c:388
LONG InterlockedIncrement(LONG volatile *addend)
Definition Threads.c:543
WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *p)
Definition Threads.c:364
WRes Handle_WaitObject(HANDLE h)
Definition Threads.c:32
WRes HandlePtr_Close(HANDLE *p)
Definition Threads.c:21
HANDLE CThread
Definition Threads.h:18
#define CriticalSection_Delete(p)
Definition Threads.h:62
#define CriticalSection_Leave(p)
Definition Threads.h:64
#define Semaphore_Wait(p)
Definition Threads.h:55
#define Semaphore_Close(p)
Definition Threads.h:54
#define Event_Close(p)
Definition Threads.h:42
#define Thread_Close(p)
Definition Threads.h:21
#define CriticalSection_Enter(p)
Definition Threads.h:63
#define Event_Wait(p)
Definition Threads.h:43
struct _CCriticalSection CCriticalSection
struct _CEvent CEvent
struct _CSemaphore CSemaphore
Definition Threads.h:223
pthread_mutex_t _mutex
Definition Threads.h:224
Definition Threads.h:177
int _state
Definition Threads.h:180
int _manual_reset
Definition Threads.h:179
pthread_cond_t _cond
Definition Threads.h:182
int _created
Definition Threads.h:178
pthread_mutex_t _mutex
Definition Threads.h:181
Definition Threads.h:203
pthread_cond_t _cond
Definition Threads.h:208
UInt32 _maxCount
Definition Threads.h:206
int _created
Definition Threads.h:204
UInt32 _count
Definition Threads.h:205
pthread_mutex_t _mutex
Definition Threads.h:207
Definition Threads.h:232
Definition Threads.h:186
Definition Threads.h:212
Definition Threads.h:76
unsigned int UInt32
Definition bzlib_private.h:45
#define h(i)
Definition sha256.c:48