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