38
|
1 #include "SimpleMutex.h"
|
|
2 #include <stdlib.h>
|
|
3
|
|
4 #if defined(DEBUG)
|
|
5 #include <stdio.h>
|
|
6 #define MUTEXDBG(x) printf x
|
|
7 #else
|
|
8 #define MUTEXDBG(x)
|
|
9 #endif
|
|
10
|
|
11 #if defined(_WIN32) && !defined(__CYGWIN32__)
|
|
12 #include <windows.h>
|
|
13 #include <winbase.h> /* For CreateMutex(), LockFile() */
|
|
14
|
|
15 struct SimpleMutex
|
|
16 {
|
|
17 HANDLE nativeMutex;
|
|
18 };
|
|
19
|
|
20
|
|
21 SimpleMutex* SimpleMutex_CreateMutex()
|
|
22 {
|
|
23 SimpleMutex* simple_mutex = (SimpleMutex*)malloc(sizeof(SimpleMutex));
|
|
24 if(NULL == simple_mutex)
|
|
25 {
|
|
26 MUTEXDBG(("Out of memory.\n"));
|
|
27 return NULL;
|
|
28 }
|
|
29 simple_mutex->nativeMutex = CreateMutex(NULL, FALSE, NULL);
|
|
30 if(NULL == simple_mutex->nativeMutex)
|
|
31 {
|
|
32 MUTEXDBG(("Out of memory.\n"));
|
|
33 free(simple_mutex);
|
|
34 return NULL;
|
|
35 }
|
|
36 return simple_mutex;
|
|
37 }
|
|
38 void SimpleMutex_DestroyMutex(SimpleMutex* simple_mutex)
|
|
39 {
|
|
40 if(NULL == simple_mutex)
|
|
41 {
|
|
42 return;
|
|
43 }
|
|
44 CloseHandle(simple_mutex->nativeMutex);
|
|
45 free(simple_mutex);
|
|
46 }
|
|
47 /* This will return true if locking is successful, false if not.
|
|
48 */
|
|
49 int SimpleMutex_LockMutex(SimpleMutex* simple_mutex)
|
|
50 {
|
|
51 #ifdef DEBUG
|
|
52 if(NULL == simple_mutex)
|
|
53 {
|
|
54 MUTEXDBG(("SimpleMutex_LockMutex was passed NULL\n"));
|
|
55 return 0;
|
|
56 }
|
|
57 #endif
|
|
58 return(
|
|
59 WaitForSingleObject(
|
|
60 simple_mutex->nativeMutex,
|
|
61 INFINITE
|
|
62 ) != WAIT_FAILED
|
|
63 );
|
|
64 }
|
|
65 void SimpleMutex_UnlockMutex(SimpleMutex* simple_mutex)
|
|
66 {
|
|
67 #ifdef DEBUG
|
|
68 if(NULL == simple_mutex)
|
|
69 {
|
|
70 MUTEXDBG(("SimpleMutex_UnlockMutex was passed NULL\n"));
|
|
71 return;
|
|
72 }
|
|
73 #endif
|
|
74 ReleaseMutex(
|
|
75 simple_mutex->nativeMutex
|
|
76 );
|
|
77 }
|
|
78 #else /* Assuming POSIX...maybe not a good assumption. */
|
|
79 #include <pthread.h>
|
|
80
|
|
81 struct SimpleMutex
|
|
82 {
|
|
83 pthread_mutex_t* nativeMutex;
|
|
84 };
|
|
85
|
|
86 SimpleMutex* SimpleMutex_CreateMutex()
|
|
87 {
|
|
88 int ret_val;
|
|
89 SimpleMutex* simple_mutex = (SimpleMutex*)malloc(sizeof(SimpleMutex));
|
|
90 if(NULL == simple_mutex)
|
|
91 {
|
|
92 MUTEXDBG(("Out of memory.\n"));
|
|
93 return NULL;
|
|
94 }
|
|
95 simple_mutex->nativeMutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
|
|
96 if(NULL == simple_mutex->nativeMutex)
|
|
97 {
|
|
98 MUTEXDBG(("Out of memory.\n"));
|
|
99 free(simple_mutex);
|
|
100 return NULL;
|
|
101 }
|
|
102
|
|
103 ret_val = pthread_mutex_init(simple_mutex->nativeMutex, NULL);
|
|
104 if(0 != ret_val)
|
|
105 {
|
|
106 free(simple_mutex->nativeMutex);
|
|
107 free(simple_mutex);
|
|
108 return NULL;
|
|
109 }
|
|
110 return simple_mutex;
|
|
111 }
|
|
112 void SimpleMutex_DestroyMutex(SimpleMutex* simple_mutex)
|
|
113 {
|
|
114 if(NULL != simple_mutex)
|
|
115 {
|
|
116 pthread_mutex_destroy(simple_mutex->nativeMutex);
|
|
117 free(simple_mutex->nativeMutex);
|
|
118 free(simple_mutex);
|
|
119 }
|
|
120 }
|
|
121 /* This will return true if locking is successful, false if not.
|
|
122 * (This is the opposite of pthread_mutex_lock which returns
|
|
123 * 0 for success.)
|
|
124 */
|
|
125 int SimpleMutex_LockMutex(SimpleMutex* simple_mutex)
|
|
126 {
|
|
127 #ifdef DEBUG
|
|
128 if(NULL == simple_mutex)
|
|
129 {
|
|
130 MUTEXDBG(("SimpleMutex_LockMutex was passed NULL\n"));
|
|
131 return 0;
|
|
132 }
|
|
133 #endif
|
|
134 return(
|
|
135 pthread_mutex_lock(
|
|
136 simple_mutex->nativeMutex
|
|
137 ) == 0
|
|
138 );
|
|
139 }
|
|
140 void SimpleMutex_UnlockMutex(SimpleMutex* simple_mutex)
|
|
141 {
|
|
142 #ifdef DEBUG
|
|
143 if(NULL == simple_mutex)
|
|
144 {
|
|
145 MUTEXDBG(("SimpleMutex_LockMutex was passed NULL\n"));
|
|
146 return;
|
|
147 }
|
|
148 #endif
|
|
149 pthread_mutex_unlock(
|
|
150 simple_mutex->nativeMutex
|
|
151 );
|
|
152 }
|
|
153 #endif
|
|
154
|
|
155
|