diff options
Diffstat (limited to 'include/compat/pthread.h')
-rwxr-xr-x[-rw-r--r--] | include/compat/pthread.h | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/include/compat/pthread.h b/include/compat/pthread.h index 8b8c3c6..1527d3c 100644..100755 --- a/include/compat/pthread.h +++ b/include/compat/pthread.h | |||
@@ -8,6 +8,8 @@ | |||
8 | 8 | ||
9 | #ifdef _WIN32 | 9 | #ifdef _WIN32 |
10 | 10 | ||
11 | #include <malloc.h> | ||
12 | #include <stdlib.h> | ||
11 | #include <windows.h> | 13 | #include <windows.h> |
12 | 14 | ||
13 | /* | 15 | /* |
@@ -16,6 +18,11 @@ | |||
16 | #define PTHREAD_ONCE_INIT { INIT_ONCE_STATIC_INIT } | 18 | #define PTHREAD_ONCE_INIT { INIT_ONCE_STATIC_INIT } |
17 | 19 | ||
18 | /* | 20 | /* |
21 | * Static mutex initialization values. | ||
22 | */ | ||
23 | #define PTHREAD_MUTEX_INITIALIZER { .lock = NULL } | ||
24 | |||
25 | /* | ||
19 | * Once definitions. | 26 | * Once definitions. |
20 | */ | 27 | */ |
21 | struct pthread_once { | 28 | struct pthread_once { |
@@ -55,27 +62,43 @@ pthread_equal(pthread_t t1, pthread_t t2) | |||
55 | return t1 == t2; | 62 | return t1 == t2; |
56 | } | 63 | } |
57 | 64 | ||
58 | typedef CRITICAL_SECTION pthread_mutex_t; | 65 | struct pthread_mutex { |
66 | volatile LPCRITICAL_SECTION lock; | ||
67 | }; | ||
68 | typedef struct pthread_mutex pthread_mutex_t; | ||
59 | typedef void pthread_mutexattr_t; | 69 | typedef void pthread_mutexattr_t; |
60 | 70 | ||
61 | static inline int | 71 | static inline int |
62 | pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) | 72 | pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) |
63 | { | 73 | { |
64 | InitializeCriticalSection(mutex); | 74 | if ((mutex->lock = malloc(sizeof(CRITICAL_SECTION))) == NULL) |
75 | exit(ENOMEM); | ||
76 | InitializeCriticalSection(mutex->lock); | ||
65 | return 0; | 77 | return 0; |
66 | } | 78 | } |
67 | 79 | ||
68 | static inline int | 80 | static inline int |
69 | pthread_mutex_lock(pthread_mutex_t *mutex) | 81 | pthread_mutex_lock(pthread_mutex_t *mutex) |
70 | { | 82 | { |
71 | EnterCriticalSection(mutex); | 83 | if (mutex->lock == NULL) { |
84 | LPCRITICAL_SECTION lcs; | ||
85 | |||
86 | if ((lcs = malloc(sizeof(CRITICAL_SECTION))) == NULL) | ||
87 | exit(ENOMEM); | ||
88 | InitializeCriticalSection(lcs); | ||
89 | if (InterlockedCompareExchangePointer((PVOID*)&mutex->lock, (PVOID)lcs, NULL) != NULL) { | ||
90 | DeleteCriticalSection(lcs); | ||
91 | free(lcs); | ||
92 | } | ||
93 | } | ||
94 | EnterCriticalSection(mutex->lock); | ||
72 | return 0; | 95 | return 0; |
73 | } | 96 | } |
74 | 97 | ||
75 | static inline int | 98 | static inline int |
76 | pthread_mutex_unlock(pthread_mutex_t *mutex) | 99 | pthread_mutex_unlock(pthread_mutex_t *mutex) |
77 | { | 100 | { |
78 | LeaveCriticalSection(mutex); | 101 | LeaveCriticalSection(mutex->lock); |
79 | return 0; | 102 | return 0; |
80 | } | 103 | } |
81 | 104 | ||