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
22024-03-28 : 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#include "Compiler.h"
13
14// #define Z7_AFFINITY_DISABLE
15#if defined(__linux__)
16#if !defined(__APPLE__) && !defined(_AIX) && !defined(__ANDROID__)
17#ifndef Z7_AFFINITY_DISABLE
18#define Z7_AFFINITY_SUPPORTED
19// #pragma message(" ==== Z7_AFFINITY_SUPPORTED")
20#if !defined(_GNU_SOURCE)
21// #pragma message(" ==== _GNU_SOURCE set")
22// we need _GNU_SOURCE for cpu_set_t, if we compile for MUSL
24#define _GNU_SOURCE
26#endif
27#endif
28#endif
29#endif
30
31#include <pthread.h>
32
33#endif
34
35#include "7zTypes.h"
36
38
39#ifdef _WIN32
40
41WRes HandlePtr_Close(HANDLE *h);
43
44typedef HANDLE CThread;
45
46#define Thread_CONSTRUCT(p) { *(p) = NULL; }
47#define Thread_WasCreated(p) (*(p) != NULL)
48#define Thread_Close(p) HandlePtr_Close(p)
49// #define Thread_Wait(p) Handle_WaitObject(*(p))
50
51#ifdef UNDER_CE
52 // if (USE_THREADS_CreateThread is defined), we use _beginthreadex()
53 // if (USE_THREADS_CreateThread is not definned), we use CreateThread()
54 #define USE_THREADS_CreateThread
55#endif
56
57typedef
58 #ifdef USE_THREADS_CreateThread
59 DWORD
60 #else
61 unsigned
62 #endif
64
65#define THREAD_FUNC_RET_ZERO 0
66
68typedef DWORD_PTR CCpuSet;
69
70#define CpuSet_Zero(p) *(p) = (0)
71#define CpuSet_Set(p, cpu) *(p) |= ((DWORD_PTR)1 << (cpu))
72
73#else // _WIN32
74
75typedef struct
76{
77 pthread_t _tid;
79} CThread;
80
81#define Thread_CONSTRUCT(p) { (p)->_tid = 0; (p)->_created = 0; }
82#define Thread_WasCreated(p) ((p)->_created != 0)
84// #define Thread_Wait Thread_Wait_Close
85
86typedef void * THREAD_FUNC_RET_TYPE;
87#define THREAD_FUNC_RET_ZERO NULL
88
89
91
92#ifdef Z7_AFFINITY_SUPPORTED
93
94typedef cpu_set_t CCpuSet;
95#define CpuSet_Zero(p) CPU_ZERO(p)
96#define CpuSet_Set(p, cpu) CPU_SET(cpu, p)
97#define CpuSet_IsSet(p, cpu) CPU_ISSET(cpu, p)
98
99#else
100
102#define CpuSet_Zero(p) *(p) = (0)
103#define CpuSet_Set(p, cpu) *(p) |= ((UInt64)1 << (cpu))
104#define CpuSet_IsSet(p, cpu) ((*(p) & ((UInt64)1 << (cpu))) != 0)
105
106#endif
107
108
109#endif // _WIN32
110
111
112#define THREAD_FUNC_CALL_TYPE Z7_STDCALL
113
114#if defined(_WIN32) && defined(__GNUC__)
115/* GCC compiler for x86 32-bit uses the rule:
116 the stack is 16-byte aligned before CALL instruction for function calling.
117 But only root function main() contains instructions that
118 set 16-byte alignment for stack pointer. And another functions
119 just keep alignment, if it was set in some parent function.
120
121 The problem:
122 if we create new thread in MinGW (GCC) 32-bit x86 via _beginthreadex() or CreateThread(),
123 the root function of thread doesn't set 16-byte alignment.
124 And stack frames in all child functions also will be unaligned in that case.
125
126 Here we set (force_align_arg_pointer) attribute for root function of new thread.
127 Do we need (force_align_arg_pointer) also for another systems? */
128
129 #define THREAD_FUNC_ATTRIB_ALIGN_ARG __attribute__((force_align_arg_pointer))
130 // #define THREAD_FUNC_ATTRIB_ALIGN_ARG // for debug : bad alignment in SSE functions
131#else
132 #define THREAD_FUNC_ATTRIB_ALIGN_ARG
133#endif
134
135#define THREAD_FUNC_DECL THREAD_FUNC_ATTRIB_ALIGN_ARG THREAD_FUNC_RET_TYPE THREAD_FUNC_CALL_TYPE
136
141
142#ifdef _WIN32
143#define Thread_Create_With_CpuSet(p, func, param, cs) \
144 Thread_Create_With_Affinity(p, func, param, *cs)
145#else
147#endif
148
149
150#ifdef _WIN32
151
152typedef HANDLE CEvent;
153typedef CEvent CAutoResetEvent;
155#define Event_Construct(p) *(p) = NULL
156#define Event_IsCreated(p) (*(p) != NULL)
157#define Event_Close(p) HandlePtr_Close(p)
158#define Event_Wait(p) Handle_WaitObject(*(p))
165
166typedef HANDLE CSemaphore;
167#define Semaphore_Construct(p) *(p) = NULL
168#define Semaphore_IsCreated(p) (*(p) != NULL)
169#define Semaphore_Close(p) HandlePtr_Close(p)
170#define Semaphore_Wait(p) Handle_WaitObject(*(p))
171WRes Semaphore_Create(CSemaphore *p, UInt32 initCount, UInt32 maxCount);
172WRes Semaphore_OptCreateInit(CSemaphore *p, UInt32 initCount, UInt32 maxCount);
175
176typedef CRITICAL_SECTION CCriticalSection;
178#define CriticalSection_Delete(p) DeleteCriticalSection(p)
179#define CriticalSection_Enter(p) EnterCriticalSection(p)
180#define CriticalSection_Leave(p) LeaveCriticalSection(p)
181
182
183#else // _WIN32
184
185typedef struct
186{
190 pthread_mutex_t _mutex;
191 pthread_cond_t _cond;
192} CEvent;
193
196
197#define Event_Construct(p) (p)->_created = 0
198#define Event_IsCreated(p) ((p)->_created)
199
204
209
210
211typedef struct
212{
216 pthread_mutex_t _mutex;
217 pthread_cond_t _cond;
218} CSemaphore;
219
220#define Semaphore_Construct(p) (p)->_created = 0
221#define Semaphore_IsCreated(p) ((p)->_created)
222
223WRes Semaphore_Create(CSemaphore *p, UInt32 initCount, UInt32 maxCount);
224WRes Semaphore_OptCreateInit(CSemaphore *p, UInt32 initCount, UInt32 maxCount);
226#define Semaphore_Release1(p) Semaphore_ReleaseN(p, 1)
229
230
231typedef struct
232{
233 pthread_mutex_t _mutex;
235
240
241LONG InterlockedIncrement(LONG volatile *addend);
242LONG InterlockedDecrement(LONG volatile *addend);
243
244#endif // _WIN32
245
247
249
250#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
LONG InterlockedDecrement(LONG volatile *addend)
Definition Threads.c:559
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:232
pthread_mutex_t _mutex
Definition Threads.h:233
Definition Threads.h:186
int _state
Definition Threads.h:189
int _manual_reset
Definition Threads.h:188
pthread_cond_t _cond
Definition Threads.h:191
int _created
Definition Threads.h:187
pthread_mutex_t _mutex
Definition Threads.h:190
Definition Threads.h:212
pthread_cond_t _cond
Definition Threads.h:217
UInt32 _maxCount
Definition Threads.h:215
int _created
Definition Threads.h:213
UInt32 _count
Definition Threads.h:214
pthread_mutex_t _mutex
Definition Threads.h:216
Definition Threads.h:76
pthread_t _tid
Definition Threads.h:77
int _created
Definition Threads.h:78
unsigned int UInt32
Definition bzlib_private.h:45
#define Z7_DIAGNOSTIC_IGNORE_BEGIN_RESERVED_MACRO_IDENTIFIER
Definition Compiler.h:229
#define Z7_DIAGNOSTIC_IGNORE_END_RESERVED_MACRO_IDENTIFIER
Definition Compiler.h:230
#define h(i)
Definition sha256.c:48