aboutsummaryrefslogtreecommitdiff
path: root/include/compat/pthread.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/compat/pthread.h')
-rwxr-xr-x[-rw-r--r--]include/compat/pthread.h31
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 */
21struct pthread_once { 28struct 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
58typedef CRITICAL_SECTION pthread_mutex_t; 65struct pthread_mutex {
66 volatile LPCRITICAL_SECTION lock;
67};
68typedef struct pthread_mutex pthread_mutex_t;
59typedef void pthread_mutexattr_t; 69typedef void pthread_mutexattr_t;
60 70
61static inline int 71static inline int
62pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) 72pthread_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
68static inline int 80static inline int
69pthread_mutex_lock(pthread_mutex_t *mutex) 81pthread_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
75static inline int 98static inline int
76pthread_mutex_unlock(pthread_mutex_t *mutex) 99pthread_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