1 /* Threads.h -- multithreading library
2 2013-11-12 : Igor Pavlov : Public domain */
3 
4 #ifndef __7Z_THREADS_H
5 #define __7Z_THREADS_H
6 
7 #ifdef _WIN32
8 #include <windows.h>
9 #endif
10 
11 #include "7zTypes.h"
12 
13 EXTERN_C_BEGIN
14 
15 WRes HandlePtr_Close(HANDLE *h);
16 WRes Handle_WaitObject(HANDLE h);
17 
18 typedef HANDLE CThread;
19 #define Thread_Construct(p) *(p) = NULL
20 #define Thread_WasCreated(p) (*(p) != NULL)
21 #define Thread_Close(p) HandlePtr_Close(p)
22 #define Thread_Wait(p) Handle_WaitObject(*(p))
23 
24 typedef
25 #ifdef UNDER_CE
26   DWORD
27 #else
28   unsigned
29 #endif
30   THREAD_FUNC_RET_TYPE;
31 
32 #define THREAD_FUNC_CALL_TYPE MY_STD_CALL
33 #define THREAD_FUNC_DECL THREAD_FUNC_RET_TYPE THREAD_FUNC_CALL_TYPE
34 typedef THREAD_FUNC_RET_TYPE (THREAD_FUNC_CALL_TYPE * THREAD_FUNC_TYPE)(void *);
35 WRes Thread_Create(CThread *p, THREAD_FUNC_TYPE func, LPVOID param);
36 
37 typedef HANDLE CEvent;
38 typedef CEvent CAutoResetEvent;
39 typedef CEvent CManualResetEvent;
40 #define Event_Construct(p) *(p) = NULL
41 #define Event_IsCreated(p) (*(p) != NULL)
42 #define Event_Close(p) HandlePtr_Close(p)
43 #define Event_Wait(p) Handle_WaitObject(*(p))
44 WRes Event_Set(CEvent *p);
45 WRes Event_Reset(CEvent *p);
46 WRes ManualResetEvent_Create(CManualResetEvent *p, int signaled);
47 WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *p);
48 WRes AutoResetEvent_Create(CAutoResetEvent *p, int signaled);
49 WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p);
50 
51 typedef HANDLE CSemaphore;
52 #define Semaphore_Construct(p) (*p) = NULL
53 #define Semaphore_Close(p) HandlePtr_Close(p)
54 #define Semaphore_Wait(p) Handle_WaitObject(*(p))
55 WRes Semaphore_Create(CSemaphore *p, UInt32 initCount, UInt32 maxCount);
56 WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 num);
57 WRes Semaphore_Release1(CSemaphore *p);
58 
59 typedef CRITICAL_SECTION CCriticalSection;
60 WRes CriticalSection_Init(CCriticalSection *p);
61 #define CriticalSection_Delete(p) DeleteCriticalSection(p)
62 #define CriticalSection_Enter(p) EnterCriticalSection(p)
63 #define CriticalSection_Leave(p) LeaveCriticalSection(p)
64 
65 EXTERN_C_END
66 
67 #endif
68