|
9 | 9 | #include <process.h>
|
10 | 10 | #endif
|
11 | 11 |
|
12 |
| -typedef struct NRMUTEX { |
13 |
| - LONG owned ; |
14 |
| - DWORD thread_id ; |
15 |
| - HANDLE hevent ; |
16 |
| -} NRMUTEX, *PNRMUTEX ; |
| 12 | +#define PNRMUTEX HANDLE |
17 | 13 |
|
18 |
| - |
19 |
| -BOOL |
20 |
| -InitializeNonRecursiveMutex(PNRMUTEX mutex) |
| 14 | +PNRMUTEX |
| 15 | +AllocNonRecursiveMutex() |
21 | 16 | {
|
22 |
| - mutex->owned = -1 ; /* No threads have entered NonRecursiveMutex */ |
23 |
| - mutex->thread_id = 0 ; |
24 |
| - mutex->hevent = CreateEvent(NULL, FALSE, FALSE, NULL) ; |
25 |
| - return mutex->hevent != NULL ; /* TRUE if the mutex is created */ |
| 17 | + return CreateSemaphore(NULL, 1, 1, NULL); |
26 | 18 | }
|
27 | 19 |
|
28 | 20 | VOID
|
29 |
| -DeleteNonRecursiveMutex(PNRMUTEX mutex) |
| 21 | +FreeNonRecursiveMutex(PNRMUTEX mutex) |
30 | 22 | {
|
31 | 23 | /* No in-use check */
|
32 |
| - CloseHandle(mutex->hevent) ; |
33 |
| - mutex->hevent = NULL ; /* Just in case */ |
| 24 | + CloseHandle(mutex); |
34 | 25 | }
|
35 | 26 |
|
36 | 27 | DWORD
|
37 | 28 | EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
|
38 | 29 | {
|
39 |
| - /* Assume that the thread waits successfully */ |
40 |
| - DWORD ret ; |
41 |
| - |
42 |
| - /* InterlockedIncrement(&mutex->owned) == 0 means that no thread currently owns the mutex */ |
43 |
| - if (milliseconds == 0) |
44 |
| - { |
45 |
| - if (InterlockedCompareExchange(&mutex->owned, 0, -1) != -1) |
46 |
| - return WAIT_TIMEOUT ; |
47 |
| - ret = WAIT_OBJECT_0 ; |
48 |
| - } |
49 |
| - else |
50 |
| - ret = InterlockedIncrement(&mutex->owned) ? |
51 |
| - /* Some thread owns the mutex, let's wait... */ |
52 |
| - WaitForSingleObject(mutex->hevent, milliseconds) : WAIT_OBJECT_0 ; |
53 |
| - |
54 |
| - mutex->thread_id = GetCurrentThreadId() ; /* We own it */ |
55 |
| - return ret ; |
| 30 | + return WaitForSingleObject(mutex, milliseconds); |
56 | 31 | }
|
57 | 32 |
|
58 | 33 | BOOL
|
59 | 34 | LeaveNonRecursiveMutex(PNRMUTEX mutex)
|
60 | 35 | {
|
61 |
| - /* We don't own the mutex */ |
62 |
| - mutex->thread_id = 0 ; |
63 |
| - return |
64 |
| - InterlockedDecrement(&mutex->owned) < 0 || |
65 |
| - SetEvent(mutex->hevent) ; /* Other threads are waiting, wake one on them up */ |
66 |
| -} |
67 |
| - |
68 |
| -PNRMUTEX |
69 |
| -AllocNonRecursiveMutex(void) |
70 |
| -{ |
71 |
| - PNRMUTEX mutex = (PNRMUTEX)malloc(sizeof(NRMUTEX)) ; |
72 |
| - if (mutex && !InitializeNonRecursiveMutex(mutex)) |
73 |
| - { |
74 |
| - free(mutex) ; |
75 |
| - mutex = NULL ; |
76 |
| - } |
77 |
| - return mutex ; |
78 |
| -} |
79 |
| - |
80 |
| -void |
81 |
| -FreeNonRecursiveMutex(PNRMUTEX mutex) |
82 |
| -{ |
83 |
| - if (mutex) |
84 |
| - { |
85 |
| - DeleteNonRecursiveMutex(mutex) ; |
86 |
| - free(mutex) ; |
87 |
| - } |
| 36 | + return ReleaseSemaphore(mutex, 1, NULL); |
88 | 37 | }
|
89 | 38 |
|
90 | 39 | long PyThread_get_thread_ident(void);
|
|
0 commit comments