aboutsummaryrefslogtreecommitdiff
path: root/src/examples/pthread_windows.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/pthread_windows.c')
-rw-r--r--src/examples/pthread_windows.c252
1 files changed, 252 insertions, 0 deletions
diff --git a/src/examples/pthread_windows.c b/src/examples/pthread_windows.c
new file mode 100644
index 00000000..ed519ebc
--- /dev/null
+++ b/src/examples/pthread_windows.c
@@ -0,0 +1,252 @@
1#include "pthread_windows.h"
2#include <Windows.h>
3
4struct _pthread_t
5{
6 HANDLE thread;
7};
8
9struct _pthread_cond_t
10{
11 HANDLE event;
12};
13
14struct _pthread_mutex_t
15{
16 HANDLE mutex;
17};
18
19struct StdCallThread
20{
21 void *(__cdecl *start) (void *);
22 void *arg;
23};
24
25DWORD WINAPI
26ThreadProc (LPVOID lpParameter)
27{
28 struct StdCallThread st = *((struct StdCallThread*) lpParameter);
29 free (lpParameter);
30 st.start (st.arg);
31 return 0;
32}
33
34
35int
36pthread_create (pthread_t *pt,
37 const void *attr,
38 void *(__cdecl *start)(void *),
39 void *arg)
40{
41 pthread_t pt_ = (pthread_t) malloc (sizeof(struct _pthread_t));
42 if (NULL == pt_)
43 return 1;
44 struct StdCallThread *sct;
45 sct = (struct StdCallThread*) malloc (sizeof(struct StdCallThread));
46 if (NULL == sct)
47 {
48 free (pt_);
49 return 1;
50 }
51
52 sct->start = start;
53 sct->arg = arg;
54 pt_->thread = CreateThread (NULL, 0, ThreadProc, sct, 0, NULL);
55 if (NULL == pt_->thread)
56 {
57 free (sct);
58 free (pt_);
59 return 1;
60 }
61 *pt = pt_;
62
63 return 0;
64}
65
66
67int
68pthread_detach (pthread_t pt)
69{
70 if (pt)
71 {
72 CloseHandle (pt->thread);
73 free (pt);
74 }
75 return 0;
76}
77
78
79int
80pthread_join (pthread_t pt,
81 void **value_ptr)
82{
83 if (NULL == pt)
84 return 1;
85
86 if (value_ptr)
87 {
88 *value_ptr = NULL;
89 }
90 WaitForSingleObject (pt->thread, INFINITE);
91 CloseHandle (pt->thread);
92 free (pt);
93
94 return 0;
95}
96
97
98int
99pthread_mutex_init (pthread_mutex_t *mutex,
100 const void *attr)
101{
102 pthread_mutex_t mutex_ = (pthread_mutex_t) malloc (sizeof(struct
103 _pthread_mutex_t));
104 if (NULL == mutex_)
105 return 1;
106 mutex_->mutex = CreateMutex (NULL, FALSE, NULL);
107 if (NULL == mutex_->mutex)
108 {
109 free (mutex_);
110 return 1;
111 }
112 *mutex = mutex_;
113
114 return 0;
115}
116
117
118int
119pthread_mutex_destroy (pthread_mutex_t *mutex)
120{
121 if (NULL == mutex)
122 return 1;
123 if ((NULL == *mutex) || (PTHREAD_MUTEX_INITIALIZER == *mutex))
124 return 0;
125
126 CloseHandle ((*mutex)->mutex);
127 free (*mutex);
128 *mutex = NULL;
129
130 return 0;
131}
132
133
134int
135pthread_mutex_lock (pthread_mutex_t *mutex)
136{
137 if (NULL == mutex)
138 return 1;
139 if (NULL == *mutex)
140 return 1;
141 if (PTHREAD_MUTEX_INITIALIZER == *mutex)
142 {
143 int ret = pthread_mutex_init (mutex, NULL);
144 if (0 != ret)
145 return ret;
146 }
147 if (WAIT_OBJECT_0 != WaitForSingleObject ((*mutex)->mutex, INFINITE))
148 return 1;
149 return 0;
150}
151
152
153int
154pthread_mutex_unlock (pthread_mutex_t *mutex)
155{
156 if (NULL == mutex)
157 return 1;
158 if ((NULL == *mutex) || (PTHREAD_MUTEX_INITIALIZER == *mutex))
159 return 1;
160
161 if (0 == ReleaseMutex ((*mutex)->mutex))
162 return 1;
163
164 return 0;
165}
166
167
168int
169pthread_cond_init (pthread_cond_t *cond,
170 const void *attr)
171{
172 pthread_cond_t cond_ = (pthread_cond_t) malloc (sizeof(struct
173 _pthread_cond_t));
174 if (NULL == cond_)
175 return 1;
176 cond_->event = CreateEvent (NULL, FALSE, FALSE, NULL);
177 if (NULL == cond_->event)
178 {
179 free (cond_);
180 return 1;
181 }
182 *cond = cond_;
183
184 return 0;
185}
186
187
188int
189pthread_cond_destroy (pthread_cond_t *cond)
190{
191 if (NULL == cond)
192 return 1;
193 if ((NULL == *cond) || (PTHREAD_COND_INITIALIZER == *cond))
194 return 1;
195
196 CloseHandle ((*cond)->event);
197 free (*cond);
198
199 return 0;
200}
201
202
203int
204pthread_cond_wait (pthread_cond_t *cond,
205 pthread_mutex_t *mutex)
206{
207 if ((NULL == cond) || (NULL == mutex))
208 return 1;
209 if ((NULL == *cond) || (NULL == *mutex))
210 return 1;
211 if (PTHREAD_COND_INITIALIZER == *cond)
212 {
213 int ret = pthread_cond_init (cond, NULL);
214 if (0 != ret)
215 return ret;
216 }
217 if (PTHREAD_MUTEX_INITIALIZER == *mutex)
218 {
219 int ret = pthread_mutex_init (mutex, NULL);
220 if (0 != ret)
221 return ret;
222 }
223 ReleaseMutex ((*mutex)->mutex);
224 if (WAIT_OBJECT_0 != WaitForSingleObject ((*cond)->event, INFINITE))
225 return 1;
226 if (WAIT_OBJECT_0 != WaitForSingleObject ((*mutex)->mutex, INFINITE))
227 return 1;
228
229 return 0;
230}
231
232
233int
234pthread_cond_signal (pthread_cond_t *cond)
235{
236 if (NULL == cond)
237 return 1;
238 if ((NULL == *cond) || (PTHREAD_COND_INITIALIZER == *cond))
239 return 1;
240
241 if (0 == SetEvent ((*cond)->event))
242 return 1;
243
244 return 0;
245}
246
247
248int
249pthread_cond_broadcast (pthread_cond_t *cond)
250{
251 return pthread_cond_signal (cond);
252}