Rev 270 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 270 | Rev 800 | ||
|---|---|---|---|
| 1 | /*************************************************************************** |
1 | /*************************************************************************** |
| 2 | wthread.c - description |
2 | wthread.c - description |
| 3 | ------------------- |
3 | ------------------- |
| 4 | begin : Sun Jan 02 2000 |
4 | begin : Sun Jan 02 2000 |
| 5 | copyright : (C) 2001 by Rainer Lehrig |
5 | copyright : (C) 2001 by Rainer Lehrig |
| 6 | email : lehrig@t-online.de |
6 | email : lehrig@t-online.de |
| 7 | ***************************************************************************/ |
7 | ***************************************************************************/ |
| 8 | 8 | ||
| 9 | /*************************************************************************** |
9 | /*************************************************************************** |
| 10 | * * |
10 | * * |
| 11 | * This library is free software; you can redistribute it and/or modify * |
11 | * This library is free software; you can redistribute it and/or modify * |
| 12 | * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as * |
12 | * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as * |
| 13 | * published by the Free Software Foundation * |
13 | * published by the Free Software Foundation * |
| 14 | * * |
14 | * * |
| 15 | ***************************************************************************/ |
15 | ***************************************************************************/ |
| 16 | /*********************************************************************************** |
16 | /*********************************************************************************** |
| 17 | |
17 | |
| 18 | Wrapper for posix threads (UNIX,VMS,windows) |
18 | Wrapper for posix threads (UNIX,VMS,windows) |
| 19 | |
19 | |
| 20 | (C) Rainer Lehrig 2000 lehrig@t-online.de |
20 | (C) Rainer Lehrig 2000 lehrig@t-online.de |
| 21 | |
21 | |
| 22 | ***********************************************************************************/ |
22 | ***********************************************************************************/ |
| 23 | #include <errno.h> |
23 | #include <errno.h> |
| 24 | #include "rlwthread.h" |
24 | #include "rlwthread.h" |
| 25 | #ifdef unix |
25 | #ifdef unix |
| 26 | #include <sys/time.h> |
26 | #include <sys/time.h> |
| 27 | #include <sys/socket.h> |
27 | #include <sys/socket.h> |
| 28 | #include <netinet/in.h> |
28 | #include <netinet/in.h> |
| 29 | #include <netdb.h> |
29 | #include <netdb.h> |
| 30 | #include <pthread.h> |
30 | #include <pthread.h> |
| 31 | #endif |
31 | #endif |
| 32 | 32 | ||
| 33 | /***************************************/ |
33 | /***************************************/ |
| 34 | /* init attributes with default values */ |
34 | /* init attributes with default values */ |
| 35 | /***************************************/ |
35 | /***************************************/ |
| 36 | int rlwthread_attr_init(pthread_attr_t *attr) |
36 | int rlwthread_attr_init(pthread_attr_t *attr) |
| 37 | { |
37 | { |
| 38 | #ifdef _WIN32 |
38 | #ifdef _WIN32 |
| 39 | memset(attr,0,sizeof(pthread_attr_t)); |
39 | memset(attr,0,sizeof(pthread_attr_t)); |
| 40 | return 0; |
40 | return 0; |
| 41 | #else |
41 | #else |
| 42 | return pthread_attr_init(attr); |
42 | return pthread_attr_init(attr); |
| 43 | #endif |
43 | #endif |
| 44 | } |
44 | } |
| 45 | 45 | ||
| 46 | /* #pdoku headerlevel 2 ^^wthread_create */ |
46 | /* #pdoku headerlevel 2 ^^wthread_create */ |
| 47 | /*******************************************/ |
47 | /*******************************************/ |
| 48 | /* create a thread */ |
48 | /* create a thread */ |
| 49 | /* tid: returned thread_id */ |
49 | /* tid: returned thread_id */ |
| 50 | /* attr: thread creation attributes */ |
50 | /* attr: thread creation attributes */ |
| 51 | /* func: thread start routine */ |
51 | /* func: thread start routine */ |
| 52 | /* arg: parameter to thread start routine */ |
52 | /* arg: parameter to thread start routine */ |
| 53 | /*******************************************/ |
53 | /*******************************************/ |
| 54 | int rlwthread_create(pthread_t *tid, const pthread_attr_t *attr, |
54 | int rlwthread_create(pthread_t *tid, const pthread_attr_t *attr, |
| 55 | void *(*func)(void*), void *arg) |
55 | void *(*func)(void*), void *arg) |
| 56 | { |
56 | { |
| 57 | #ifdef _WIN32 |
57 | #ifdef _WIN32 |
| 58 | HANDLE handle; |
58 | HANDLE handle; |
| 59 | int ThreadId; |
59 | int ThreadId; |
| 60 | int dwStackSize = 0; |
60 | int dwStackSize = 0; |
| 61 | if(attr != NULL) dwStackSize = attr->__stacksize; |
61 | if(attr != NULL) dwStackSize = attr->__stacksize; |
| 62 | handle = CreateThread( NULL, /* pointer to thread security attributes */ |
62 | handle = CreateThread( NULL, /* pointer to thread security attributes */ |
| 63 | dwStackSize, /* initial thread stack size, in bytes */ |
63 | dwStackSize, /* initial thread stack size, in bytes */ |
| 64 | (LPTHREAD_START_ROUTINE)func, /* pointer to thread function */ |
64 | (LPTHREAD_START_ROUTINE)func, /* pointer to thread function */ |
| 65 | arg, /* argument for new thread */ |
65 | arg, /* argument for new thread */ |
| 66 | 0, /* creation flags */ |
66 | 0, /* creation flags */ |
| 67 | (unsigned long *) &ThreadId /* pointer to returned thread identifier */ |
67 | (unsigned long *) &ThreadId /* pointer to returned thread identifier */ |
| 68 | ); |
68 | ); |
| 69 | *tid = (pthread_t) handle; |
69 | *tid = (pthread_t) handle; |
| 70 | if(handle == NULL) return -1; |
70 | if(handle == NULL) return -1; |
| 71 | else return 0; |
71 | else return 0; |
| 72 | #else |
72 | #else |
| 73 | return pthread_create(tid,attr,func,arg); |
73 | return pthread_create(tid,attr,func,arg); |
| 74 | #endif |
74 | #endif |
| 75 | } |
75 | } |
| 76 | 76 | ||
| 77 | void rlwthread_close_handle(pthread_t *tid) |
77 | void rlwthread_close_handle(pthread_t *tid) |
| 78 | { |
78 | { |
| 79 | #ifdef _WIN32 |
79 | #ifdef _WIN32 |
| 80 | CloseHandle((HANDLE) *tid); |
80 | CloseHandle((HANDLE) *tid); |
| 81 | #else |
81 | #else |
| 82 | if(tid == NULL) return; |
82 | if(tid == NULL) return; |
| 83 | #endif |
83 | #endif |
| 84 | } |
84 | } |
| 85 | 85 | ||
| 86 | /**************************************************************/ |
86 | /**************************************************************/ |
| 87 | /* terminate a thread */ |
87 | /* terminate a thread */ |
| 88 | /* call this function within a thread to terminate the thead */ |
88 | /* call this function within a thread to terminate the thead */ |
| 89 | /* or return from the thread start routine */ |
89 | /* or return from the thread start routine */ |
| 90 | /* status: return value from the thread (see wthread_join) */ |
90 | /* status: return value from the thread (see wthread_join) */ |
| 91 | /**************************************************************/ |
91 | /**************************************************************/ |
| 92 | void rlwthread_exit(void *status) |
92 | void rlwthread_exit(void *status) |
| 93 | { |
93 | { |
| 94 | #ifdef _WIN32 |
94 | #ifdef _WIN32 |
| 95 | DWORD *ptr; |
95 | DWORD *ptr; |
| 96 | ptr = (DWORD *) status; |
96 | ptr = (DWORD *) status; |
| 97 | if(status == NULL) ExitThread((DWORD) 0); |
97 | if(status == NULL) ExitThread((DWORD) 0); |
| 98 | else ExitThread(*ptr); |
98 | else ExitThread(*ptr); |
| 99 | #else |
99 | #else |
| 100 | pthread_exit(status); |
100 | pthread_exit(status); |
| 101 | #endif |
101 | #endif |
| 102 | } |
102 | } |
| 103 | 103 | ||
| 104 | /* #pdoku headerlevel 2 ^^wthread_join */ |
104 | /* #pdoku headerlevel 2 ^^wthread_join */ |
| 105 | /************************************/ |
105 | /************************************/ |
| 106 | /* wait for termination of thread */ |
106 | /* wait for termination of thread */ |
| 107 | /* tid: thread id */ |
107 | /* tid: thread id */ |
| 108 | /* status: return value from thread */ |
108 | /* status: return value from thread */ |
| 109 | /************************************/ |
109 | /************************************/ |
| 110 | int rlwthread_join(pthread_t tid, void **status) |
110 | int rlwthread_join(pthread_t tid, void **status) |
| 111 | { |
111 | { |
| 112 | #ifdef _WIN32 |
112 | #ifdef _WIN32 |
| 113 | DWORD exitcode; |
113 | DWORD exitcode; |
| 114 | while(1) |
114 | while(1) |
| 115 | { |
115 | { |
| 116 | GetExitCodeThread((HANDLE) tid,&exitcode); |
116 | GetExitCodeThread((HANDLE) tid,&exitcode); |
| 117 | if(exitcode != STILL_ACTIVE) return exitcode; |
117 | if(exitcode != STILL_ACTIVE) return exitcode; |
| 118 | Sleep(10); /* sleep 10 msec */ |
118 | Sleep(10); /* sleep 10 msec */ |
| 119 | } |
119 | } |
| 120 | #else |
120 | #else |
| 121 | return pthread_join(tid,status); |
121 | return pthread_join(tid,status); |
| 122 | #endif |
122 | #endif |
| 123 | } |
123 | } |
| 124 | 124 | ||
| 125 | /**************************/ |
125 | /**************************/ |
| 126 | /* init a mutex */ |
126 | /* init a mutex */ |
| 127 | /* mptr: pointer to mutex */ |
127 | /* mptr: pointer to mutex */ |
| 128 | /* atrr: mutex atrributes */ |
128 | /* atrr: mutex atrributes */ |
| 129 | /**************************/ |
129 | /**************************/ |
| 130 | int rlwthread_mutex_init(pthread_mutex_t *mptr, |
130 | int rlwthread_mutex_init(pthread_mutex_t *mptr, |
| 131 | const pthread_mutexattr_t *attr) |
131 | const pthread_mutexattr_t *attr) |
| 132 | { |
132 | { |
| 133 | #ifdef _WIN32 |
133 | #ifdef _WIN32 |
| 134 | HANDLE handle = CreateMutex(NULL, FALSE, NULL); |
134 | HANDLE handle = CreateMutex(NULL, FALSE, NULL); |
| 135 | if(handle) *mptr = handle; |
135 | if(handle) *mptr = handle; |
| 136 | //old InitializeCriticalSection(mptr); |
136 | //old InitializeCriticalSection(mptr); |
| 137 | return 0; |
137 | return 0; |
| 138 | #else |
138 | #else |
| 139 | return pthread_mutex_init(mptr,attr); |
139 | return pthread_mutex_init(mptr,attr); |
| 140 | #endif |
140 | #endif |
| 141 | } |
141 | } |
| 142 | 142 | ||
| 143 | /**************************/ |
143 | /**************************/ |
| 144 | /* destroy a mutex */ |
144 | /* destroy a mutex */ |
| 145 | /* mptr: pointer to mutex */ |
145 | /* mptr: pointer to mutex */ |
| 146 | /**************************/ |
146 | /**************************/ |
| 147 | int rlwthread_mutex_destroy(pthread_mutex_t *mptr) |
147 | int rlwthread_mutex_destroy(pthread_mutex_t *mptr) |
| 148 | { |
148 | { |
| 149 | #ifdef _WIN32 |
149 | #ifdef _WIN32 |
| 150 | CloseHandle(*mptr); |
150 | CloseHandle(*mptr); |
| 151 | //old DeleteCriticalSection(mptr); |
151 | //old DeleteCriticalSection(mptr); |
| 152 | return 0; |
152 | return 0; |
| 153 | #else |
153 | #else |
| 154 | return pthread_mutex_destroy(mptr); |
154 | return pthread_mutex_destroy(mptr); |
| 155 | #endif |
155 | #endif |
| 156 | } |
156 | } |
| 157 | 157 | ||
| 158 | /**************************/ |
158 | /**************************/ |
| 159 | /* lock a mutex */ |
159 | /* lock a mutex */ |
| 160 | /* mptr: pointer to mutex */ |
160 | /* mptr: pointer to mutex */ |
| 161 | /**************************/ |
161 | /**************************/ |
| 162 | int rlwthread_mutex_lock(pthread_mutex_t *mptr) |
162 | int rlwthread_mutex_lock(pthread_mutex_t *mptr) |
| 163 | { |
163 | { |
| 164 | #ifdef _WIN32 |
164 | #ifdef _WIN32 |
| 165 | if(WaitForSingleObject(*mptr, INFINITE) == WAIT_OBJECT_0) return 0; |
165 | if(WaitForSingleObject(*mptr, INFINITE) == WAIT_OBJECT_0) return 0; |
| 166 | //old EnterCriticalSection(mptr); // pointer to critical section object |
166 | //old EnterCriticalSection(mptr); // pointer to critical section object |
| 167 | return 0; |
167 | return 0; |
| 168 | #else |
168 | #else |
| 169 | return pthread_mutex_lock(mptr); |
169 | return pthread_mutex_lock(mptr); |
| 170 | #endif |
170 | #endif |
| 171 | } |
171 | } |
| 172 | 172 | ||
| 173 | /********************************/ |
173 | /********************************/ |
| 174 | /* try to lock a mutex */ |
174 | /* try to lock a mutex */ |
| 175 | /* mptr: pointer to mutex */ |
175 | /* mptr: pointer to mutex */ |
| 176 | /* return 0 if already locked */ |
176 | /* return 0 if already locked */ |
| 177 | /* return !0 if lock sucessfull */ |
177 | /* return !0 if lock sucessfull */ |
| 178 | /********************************/ |
178 | /********************************/ |
| 179 | 179 | ||
| 180 | #ifdef _WIN32 |
180 | #ifdef _WIN32 |
| 181 | WINBASEAPI BOOL WINAPI TryEnterCriticalSection( LPCRITICAL_SECTION lpCriticalSection); |
181 | WINBASEAPI BOOL WINAPI TryEnterCriticalSection( LPCRITICAL_SECTION lpCriticalSection); |
| 182 | #endif |
182 | #endif |
| 183 | 183 | ||
| 184 | int rlwthread_mutex_trylock(pthread_mutex_t *mptr) |
184 | int rlwthread_mutex_trylock(pthread_mutex_t *mptr) |
| 185 | { |
185 | { |
| 186 | #ifdef _WIN32 |
186 | #ifdef _WIN32 |
| 187 | DWORD ret; |
187 | DWORD ret; |
| 188 | 188 | ||
| 189 | ret = WaitForSingleObject(*mptr, 0); |
189 | ret = WaitForSingleObject(*mptr, 0); |
| 190 | if(ret == WAIT_OBJECT_0) return 1; |
190 | if(ret == WAIT_OBJECT_0) return 1; |
| 191 | return 0; |
191 | return 0; |
| 192 | //old ret = TryEnterCriticalSection(mptr); // pointer to critical section object |
192 | //old ret = TryEnterCriticalSection(mptr); // pointer to critical section object |
| 193 | return ret; |
193 | return ret; |
| 194 | #else |
194 | #else |
| 195 | int ret; |
195 | int ret; |
| 196 | 196 | ||
| 197 | ret = pthread_mutex_trylock(mptr); |
197 | ret = pthread_mutex_trylock(mptr); |
| 198 | if(ret == EBUSY) return 0; |
198 | if(ret == EBUSY) return 0; |
| 199 | return 1; |
199 | return 1; |
| 200 | #endif |
200 | #endif |
| 201 | } |
201 | } |
| 202 | 202 | ||
| 203 | /**************************/ |
203 | /**************************/ |
| 204 | /* unlock a mutex */ |
204 | /* unlock a mutex */ |
| 205 | /* mptr: pointer to mutex */ |
205 | /* mptr: pointer to mutex */ |
| 206 | /**************************/ |
206 | /**************************/ |
| 207 | int rlwthread_mutex_unlock(pthread_mutex_t *mptr) |
207 | int rlwthread_mutex_unlock(pthread_mutex_t *mptr) |
| 208 | { |
208 | { |
| 209 | #ifdef _WIN32 |
209 | #ifdef _WIN32 |
| 210 | ReleaseMutex(*mptr); |
210 | ReleaseMutex(*mptr); |
| 211 | //old LeaveCriticalSection(mptr); |
211 | //old LeaveCriticalSection(mptr); |
| 212 | return 0; |
212 | return 0; |
| 213 | #else |
213 | #else |
| 214 | return pthread_mutex_unlock(mptr); |
214 | return pthread_mutex_unlock(mptr); |
| 215 | #endif |
215 | #endif |
| 216 | } |
216 | } |
| 217 | 217 | ||
| 218 | /**************************/ |
218 | /**************************/ |
| 219 | /* kill a thread */ |
219 | /* kill a thread */ |
| 220 | /* tid handel of thread */ |
220 | /* tid handel of thread */ |
| 221 | /**************************/ |
221 | /**************************/ |
| 222 | int rlwthread_cancel(pthread_t tid) |
222 | int rlwthread_cancel(pthread_t tid) |
| 223 | { |
223 | { |
| 224 | #ifdef _WIN32 |
224 | #ifdef _WIN32 |
| 225 | return (int) CloseHandle((HANDLE) tid); |
225 | return (int) CloseHandle((HANDLE) tid); |
| 226 | #else |
226 | #else |
| 227 | return pthread_cancel(tid); |
227 | return pthread_cancel(tid); |
| 228 | #endif |
228 | #endif |
| 229 | } |
229 | } |
| 230 | 230 | ||
| 231 | /***********************************************/ |
231 | /***********************************************/ |
| 232 | /* initialize a semaphore */ |
232 | /* initialize a semaphore */ |
| 233 | /* s is a pointer to a WSEMAPHORE struct */ |
233 | /* s is a pointer to a WSEMAPHORE struct */ |
| 234 | /* cmax is the maximum number of the semaphore */ |
234 | /* cmax is the maximum number of the semaphore */ |
| 235 | /* return 0 */ |
235 | /* return 0 */ |
| 236 | /***********************************************/ |
236 | /***********************************************/ |
| 237 | int rlwrapinit_semaphore(WSEMAPHORE *s, int cmax) |
237 | int rlwrapinit_semaphore(WSEMAPHORE *s, int cmax) |
| 238 | { |
238 | { |
| 239 | /* Create a semaphore with initial count=0 max. counts of cmax. */ |
239 | /* Create a semaphore with initial count=0 max. counts of cmax. */ |
| 240 | #ifdef _WIN32 |
240 | #ifdef _WIN32 |
| 241 | 241 | ||
| 242 | s->cmax = cmax; |
242 | s->cmax = cmax; |
| 243 | s->hSemaphore = CreateSemaphore( |
243 | s->hSemaphore = CreateSemaphore( |
| 244 | NULL, /* no security attributes */ |
244 | NULL, /* no security attributes */ |
| 245 | 0, /* initial count */ |
245 | 0, /* initial count */ |
| 246 | cmax, /* maximum count */ |
246 | cmax, /* maximum count */ |
| 247 | NULL); /* unnamed semaphore */ |
247 | NULL); /* unnamed semaphore */ |
| 248 | 248 | ||
| 249 | if(s->hSemaphore == NULL) return -1; /* Check for error. */ |
249 | if(s->hSemaphore == NULL) return -1; /* Check for error. */ |
| 250 | return 0; |
250 | return 0; |
| 251 | 251 | ||
| 252 | #else |
252 | #else |
| 253 | 253 | ||
| 254 | s->cmax = cmax; |
254 | s->cmax = cmax; |
| 255 | s->nready = 0; |
255 | s->nready = 0; |
| 256 | rlwthread_mutex_init(&s->mutex, NULL); |
256 | rlwthread_mutex_init(&s->mutex, NULL); |
| 257 | pthread_cond_init(&s->cond, NULL); |
257 | pthread_cond_init(&s->cond, NULL); |
| 258 | return 0; |
258 | return 0; |
| 259 | 259 | ||
| 260 | #endif |
260 | #endif |
| 261 | } |
261 | } |
| 262 | 262 | ||
| 263 | int rlwrapdestroy_semaphore(WSEMAPHORE *s) |
263 | int rlwrapdestroy_semaphore(WSEMAPHORE *s) |
| 264 | { |
264 | { |
| 265 | #ifdef _WIN32 |
265 | #ifdef _WIN32 |
| 266 | CloseHandle(s->hSemaphore); |
266 | CloseHandle(s->hSemaphore); |
| 267 | #else |
267 | #else |
| 268 | rlwthread_mutex_destroy(&s->mutex); |
268 | rlwthread_mutex_destroy(&s->mutex); |
| 269 | #endif |
269 | #endif |
| 270 | return 0; |
270 | return 0; |
| 271 | } |
271 | } |
| 272 | 272 | ||
| 273 | /***********************************************************************/ |
273 | /***********************************************************************/ |
| 274 | /* increment a semaphore */ |
274 | /* increment a semaphore */ |
| 275 | /* s is a pointer to a WSEMAPHORE struct */ |
275 | /* s is a pointer to a WSEMAPHORE struct */ |
| 276 | /* the producer will call this function whenever new data is available */ |
276 | /* the producer will call this function whenever new data is available */ |
| 277 | /* return 0=OK -1=error */ |
277 | /* return 0=OK -1=error */ |
| 278 | /***********************************************************************/ |
278 | /***********************************************************************/ |
| 279 | int rlwrapincrement_semaphore(WSEMAPHORE *s) |
279 | int rlwrapincrement_semaphore(WSEMAPHORE *s) |
| 280 | { |
280 | { |
| 281 | /* Increment the count of the semaphore. */ |
281 | /* Increment the count of the semaphore. */ |
| 282 | #ifdef _WIN32 |
282 | #ifdef _WIN32 |
| 283 | 283 | ||
| 284 | if(!ReleaseSemaphore( |
284 | if(!ReleaseSemaphore( |
| 285 | s->hSemaphore, /* handle of semaphore */ |
285 | s->hSemaphore, /* handle of semaphore */ |
| 286 | 1, /* increase count by one */ |
286 | 1, /* increase count by one */ |
| 287 | NULL) ) /* not interested in previous count */ |
287 | NULL) ) /* not interested in previous count */ |
| 288 | { |
288 | { |
| 289 | return -1; /* Deal with the error. */ |
289 | return -1; /* Deal with the error. */ |
| 290 | } |
290 | } |
| 291 | return 0; |
291 | return 0; |
| 292 | 292 | ||
| 293 | #else |
293 | #else |
| 294 | 294 | ||
| 295 | pthread_mutex_lock(&s->mutex); |
295 | pthread_mutex_lock(&s->mutex); |
| 296 | if(s->nready == 0) pthread_cond_signal(&s->cond); |
296 | if(s->nready == 0) pthread_cond_signal(&s->cond); |
| 297 | s->nready++; |
297 | s->nready++; |
| 298 | pthread_mutex_unlock(&s->mutex); |
298 | pthread_mutex_unlock(&s->mutex); |
| 299 | return 0; |
299 | return 0; |
| 300 | 300 | ||
| 301 | #endif |
301 | #endif |
| 302 | } |
302 | } |
| 303 | 303 | ||
| 304 | /*********************************************************************************/ |
304 | /*********************************************************************************/ |
| 305 | /* wait for a semaphore */ |
305 | /* wait for a semaphore */ |
| 306 | /* s is a pointer to a WSEMAPHORE struct */ |
306 | /* s is a pointer to a WSEMAPHORE struct */ |
| 307 | /* the consumer will call this function to wait for new data to become available */ |
307 | /* the consumer will call this function to wait for new data to become available */ |
| 308 | /* return 0 */ |
308 | /* return 0 */ |
| 309 | /*********************************************************************************/ |
309 | /*********************************************************************************/ |
| 310 | int rlwrapwait_semaphore(WSEMAPHORE *s) |
310 | int rlwrapwait_semaphore(WSEMAPHORE *s) |
| 311 | { |
311 | { |
| 312 | #ifdef _WIN32 |
312 | #ifdef _WIN32 |
| 313 | 313 | ||
| 314 | int ret; |
314 | int ret; |
| 315 | ret = WaitForSingleObject( |
315 | ret = WaitForSingleObject( |
| 316 | s->hSemaphore, /* handle of semaphore */ |
316 | s->hSemaphore, /* handle of semaphore */ |
| 317 | INFINITE); /* infinite time-out interval */ |
317 | INFINITE); /* infinite time-out interval */ |
| 318 | if(ret) return 0; |
318 | if(ret) return 0; |
| 319 | return 0; |
319 | return 0; |
| 320 | 320 | ||
| 321 | #else |
321 | #else |
| 322 | 322 | ||
| 323 | pthread_mutex_lock(&s->mutex); |
323 | pthread_mutex_lock(&s->mutex); |
| 324 | while(s->nready == 0) |
324 | while(s->nready == 0) |
| 325 | { |
325 | { |
| 326 | pthread_cond_wait(&s->cond,&s->mutex); |
326 | pthread_cond_wait(&s->cond,&s->mutex); |
| 327 | } |
327 | } |
| 328 | s->nready--; |
328 | s->nready--; |
| 329 | pthread_mutex_unlock(&s->mutex); |
329 | pthread_mutex_unlock(&s->mutex); |
| 330 | return 0; |
330 | return 0; |
| 331 | 331 | ||
| 332 | #endif |
332 | #endif |
| 333 | } |
333 | } |
| 334 | 334 | ||
| 335 | int rlwthread_sleep(long msec) |
335 | int rlwthread_sleep(long msec) |
| 336 | { |
336 | { |
| 337 | #ifdef _WIN32 |
337 | #ifdef _WIN32 |
| 338 | Sleep(msec); |
338 | Sleep(msec); |
| 339 | return 0; |
339 | return 0; |
| 340 | #endif |
340 | #endif |
| 341 | 341 | ||
| 342 | #ifdef unix |
342 | #ifdef unix |
| 343 | fd_set wset,rset,eset; |
343 | fd_set wset,rset,eset; |
| 344 | struct timeval timeout; |
344 | struct timeval timeout; |
| 345 | 345 | ||
| 346 | FD_ZERO(&rset); |
346 | FD_ZERO(&rset); |
| 347 | FD_ZERO(&wset); |
347 | FD_ZERO(&wset); |
| 348 | FD_ZERO(&eset); |
348 | FD_ZERO(&eset); |
| 349 | timeout.tv_sec = msec / 1000; |
349 | timeout.tv_sec = msec / 1000; |
| 350 | timeout.tv_usec = (msec % 1000) * 1000; |
350 | timeout.tv_usec = (msec % 1000) * 1000; |
| 351 | select(1,&rset,&wset,&eset,&timeout); |
351 | select(1,&rset,&wset,&eset,&timeout); |
| 352 | return 0; |
352 | return 0; |
| 353 | #endif |
353 | #endif |
| 354 | 354 | ||
| 355 | #ifdef __VMS |
355 | #ifdef __VMS |
| 356 | struct timespec interval; |
356 | struct timespec interval; |
| 357 | 357 | ||
| 358 | interval.tv_sec = msec / 1000; |
358 | interval.tv_sec = msec / 1000; |
| 359 | interval.tv_nsec = (msec % 1000) * 1000 * 1000; /* wait msec msec */ |
359 | interval.tv_nsec = (msec % 1000) * 1000 * 1000; /* wait msec msec */ |
| 360 | pthread_delay_np(&interval); |
360 | pthread_delay_np(&interval); |
| 361 | return 0; |
361 | return 0; |
| 362 | #endif |
362 | #endif |
| 363 | } |
363 | } |
| 364 | 364 | ||
| 365 | void rlsleep(long msec) |
365 | void rlsleep(long msec) |
| 366 | { |
366 | { |
| 367 | rlwthread_sleep(msec); |
367 | rlwthread_sleep(msec); |
| 368 | } |
368 | } |
| 369 | /*** test routine ***/ |
369 | /*** test routine ***/ |
| 370 | /**************************************************************/ |
370 | /**************************************************************/ |
| 371 | /* This is an example how to use the wthread_xxx functions */ |
371 | /* This is an example how to use the wthread_xxx functions */ |
| 372 | /**************************************************************/ |
372 | /**************************************************************/ |
| 373 | #define TESTINGx |
373 | #define TESTINGx |
| 374 | #ifdef TESTING |
374 | #ifdef TESTING |
| 375 | #include "stdio.h" |
375 | #include "stdio.h" |
| 376 | #include "stdlib.h" |
376 | #include "stdlib.h" |
| 377 | #ifndef _WIN32 |
377 | #ifndef _WIN32 |
| 378 | #include "unistd.h" |
378 | #include "unistd.h" |
| 379 | #endif |
379 | #endif |
| 380 | 380 | ||
| 381 | /******************************************/ |
381 | /******************************************/ |
| 382 | /* here we define 2 mutexes (semaphores) */ |
382 | /* here we define 2 mutexes (semaphores) */ |
| 383 | /* the mutexes are global two all threads */ |
383 | /* the mutexes are global two all threads */ |
| 384 | /******************************************/ |
384 | /******************************************/ |
| 385 | pthread_mutex_t mutex1, mutex2; |
385 | pthread_mutex_t mutex1, mutex2; |
| 386 | WSEMAPHORE semaphore; |
386 | WSEMAPHORE semaphore; |
| 387 | 387 | ||
| 388 | /************************************************************************/ |
388 | /************************************************************************/ |
| 389 | /* this is the tread function */ |
389 | /* this is the tread function */ |
| 390 | /* it's name is given as parameter to wthread_create */ |
390 | /* it's name is given as parameter to wthread_create */ |
| 391 | /* *arg is an argument given by the last parameter of wthread_create */ |
391 | /* *arg is an argument given by the last parameter of wthread_create */ |
| 392 | /* you can get the return value by pthread_join (status) */ |
392 | /* you can get the return value by pthread_join (status) */ |
| 393 | /************************************************************************/ |
393 | /************************************************************************/ |
| 394 | void *thread(void *arg) |
394 | void *thread(void *arg) |
| 395 | { |
395 | { |
| 396 | int i=0; |
396 | int i=0; |
| 397 | printf("thread start : %s\n",(const char *) arg); |
397 | printf("thread start : %s\n",(const char *) arg); |
| 398 | while(i++ < 50) |
398 | while(i++ < 50) |
| 399 | { |
399 | { |
| 400 | rlwrapwait_semaphore(&semaphore); |
400 | rlwrapwait_semaphore(&semaphore); |
| 401 | printf("thread %d\n",i); |
401 | printf("thread %d\n",i); |
| 402 | /* sleep(1); */ |
402 | /* sleep(1); */ |
| 403 | } |
403 | } |
| 404 | return NULL; |
404 | return NULL; |
| 405 | } |
405 | } |
| 406 | 406 | ||
| 407 | int main() |
407 | int main() |
| 408 | { |
408 | { |
| 409 | pthread_t tid; /* this is the thread_id */ |
409 | pthread_t tid; /* this is the thread_id */ |
| 410 | void *status; /* this is the return value of the tread */ |
410 | void *status; /* this is the return value of the tread */ |
| 411 | int i; |
411 | int i; |
| 412 | 412 | ||
| 413 | /*****************************************************************************/ |
413 | /*****************************************************************************/ |
| 414 | /* here we initialize 2 mutexes */ |
414 | /* here we initialize 2 mutexes */ |
| 415 | /* the mutexes are defined global to all threads */ |
415 | /* the mutexes are defined global to all threads */ |
| 416 | /* for compatibility with MSWINDOWS the second parameter should be NULL */ |
416 | /* for compatibility with MSWINDOWS the second parameter should be NULL */ |
| 417 | /* */ |
417 | /* */ |
| 418 | /* in order to mark critical sections we can now use the following functions */ |
418 | /* in order to mark critical sections we can now use the following functions */ |
| 419 | /* wthread_mutex_lock */ |
419 | /* wthread_mutex_lock */ |
| 420 | /* wthread_mutex_unlock */ |
420 | /* wthread_mutex_unlock */ |
| 421 | /* if a mutex is locked by a thread and an other thread is calling */ |
421 | /* if a mutex is locked by a thread and an other thread is calling */ |
| 422 | /* wthread_mutex_lock it will be blocked until the first thread calls */ |
422 | /* wthread_mutex_lock it will be blocked until the first thread calls */ |
| 423 | /* the wthread_mutex_unlock function */ |
423 | /* the wthread_mutex_unlock function */ |
| 424 | /* */ |
424 | /* */ |
| 425 | /* this function is the reverse of wthread_mutex_init */ |
425 | /* this function is the reverse of wthread_mutex_init */ |
| 426 | /* wthread_mutex_destroy */ |
426 | /* wthread_mutex_destroy */ |
| 427 | /*****************************************************************************/ |
427 | /*****************************************************************************/ |
| 428 | rlwthread_mutex_init(&mutex1, NULL); |
428 | rlwthread_mutex_init(&mutex1, NULL); |
| 429 | rlwthread_mutex_init(&mutex2, NULL); |
429 | rlwthread_mutex_init(&mutex2, NULL); |
| 430 | 430 | ||
| 431 | rlwrapinit_semaphore(&semaphore, 1000); |
431 | rlwrapinit_semaphore(&semaphore, 1000); |
| 432 | printf("create a thread\n"); |
432 | printf("create a thread\n"); |
| 433 | /****************************************************************/ |
433 | /****************************************************************/ |
| 434 | /* this call will create a new thread */ |
434 | /* this call will create a new thread */ |
| 435 | /* tid is the returned thread_id */ |
435 | /* tid is the returned thread_id */ |
| 436 | /* you can use this id for example in wthread_join */ |
436 | /* you can use this id for example in wthread_join */ |
| 437 | /* in this example the second parameter is NULL */ |
437 | /* in this example the second parameter is NULL */ |
| 438 | /* you can call wthread_attr_init(pthread_attr_t *attr) */ |
438 | /* you can call wthread_attr_init(pthread_attr_t *attr) */ |
| 439 | /* to initialize the start attributes */ |
439 | /* to initialize the start attributes */ |
| 440 | /* pthread_attr_t attr; */ |
440 | /* pthread_attr_t attr; */ |
| 441 | /* wthread_attr_init(&attr); */ |
441 | /* wthread_attr_init(&attr); */ |
| 442 | /* wthread_create(&tid, &attr, thread, "hello thread"); */ |
442 | /* wthread_create(&tid, &attr, thread, "hello thread"); */ |
| 443 | /* thread is the name of the thread function (see above) */ |
443 | /* thread is the name of the thread function (see above) */ |
| 444 | /* the last parameter is given to the thread function */ |
444 | /* the last parameter is given to the thread function */ |
| 445 | /****************************************************************/ |
445 | /****************************************************************/ |
| 446 | rlwthread_create(&tid, NULL, thread, "hello thread"); |
446 | rlwthread_create(&tid, NULL, thread, "hello thread"); |
| 447 | 447 | ||
| 448 | i=0; |
448 | i=0; |
| 449 | while(i++ < 50) |
449 | while(i++ < 50) |
| 450 | { |
450 | { |
| 451 | printf("main %d\n",i); |
451 | printf("main %d\n",i); |
| 452 | rlwrapincrement_semaphore(&semaphore); |
452 | rlwrapincrement_semaphore(&semaphore); |
| 453 | if(i%4 == 0) Sleep(10); |
453 | if(i%4 == 0) Sleep(10); |
| 454 | } |
454 | } |
| 455 | 455 | ||
| 456 | printf("waiting for thread\n"); |
456 | printf("waiting for thread\n"); |
| 457 | /*****************************************************/ |
457 | /*****************************************************/ |
| 458 | /* this call waits for the termination of thread tid */ |
458 | /* this call waits for the termination of thread tid */ |
| 459 | /* the return value of the thread is given to status */ |
459 | /* the return value of the thread is given to status */ |
| 460 | /*****************************************************/ |
460 | /*****************************************************/ |
| 461 | rlwthread_join(tid, &status); |
461 | rlwthread_join(tid, &status); |
| 462 | printf("finish\n"); |
462 | printf("finish\n"); |
| 463 | return 0; |
463 | return 0; |
| 464 | } |
464 | } |
| 465 | #endif |
465 | #endif |
| 466 | 466 | ||