This commit was manufactured by cvs2svn to create tag 'r22a4-fork'.
[python/dscho.git] / Python / thread_pthread.h
blob26120d6886dac785964ade34c7a8f8b7560c0ca3
2 /* Posix threads interface */
4 #include <stdlib.h>
5 #include <string.h>
6 #include <pthread.h>
9 /* try to determine what version of the Pthread Standard is installed.
10 * this is important, since all sorts of parameter types changed from
11 * draft to draft and there are several (incompatible) drafts in
12 * common use. these macros are a start, at least.
13 * 12 May 1997 -- david arnold <davida@pobox.com>
16 #if defined(__ultrix) && defined(__mips) && defined(_DECTHREADS_)
17 /* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
18 # define PY_PTHREAD_D4
20 #elif defined(__osf__) && defined (__alpha)
21 /* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
22 # if !defined(_PTHREAD_ENV_ALPHA) || defined(_PTHREAD_USE_D4) || defined(PTHREAD_USE_D4)
23 # define PY_PTHREAD_D4
24 # else
25 # define PY_PTHREAD_STD
26 # endif
28 #elif defined(_AIX)
29 /* SCHED_BG_NP is defined if using AIX DCE pthreads
30 * but it is unsupported by AIX 4 pthreads. Default
31 * attributes for AIX 4 pthreads equal to NULL. For
32 * AIX DCE pthreads they should be left unchanged.
34 # if !defined(SCHED_BG_NP)
35 # define PY_PTHREAD_STD
36 # else
37 # define PY_PTHREAD_D7
38 # endif
40 #elif defined(__DGUX)
41 # define PY_PTHREAD_D6
43 #elif defined(__hpux) && defined(_DECTHREADS_)
44 # define PY_PTHREAD_D4
46 #else /* Default case */
47 # define PY_PTHREAD_STD
49 #endif
51 #ifdef USE_GUSI
52 /* The Macintosh GUSI I/O library sets the stackspace to
53 ** 20KB, much too low. We up it to 64K.
55 #define THREAD_STACK_SIZE 0x10000
56 #endif
59 /* set default attribute object for different versions */
61 #if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7)
62 # define pthread_attr_default pthread_attr_default
63 # define pthread_mutexattr_default pthread_mutexattr_default
64 # define pthread_condattr_default pthread_condattr_default
65 #elif defined(PY_PTHREAD_STD) || defined(PY_PTHREAD_D6)
66 # define pthread_attr_default ((pthread_attr_t *)NULL)
67 # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
68 # define pthread_condattr_default ((pthread_condattr_t *)NULL)
69 #endif
72 /* A pthread mutex isn't sufficient to model the Python lock type
73 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
74 * following are undefined:
75 * -> a thread tries to lock a mutex it already has locked
76 * -> a thread tries to unlock a mutex locked by a different thread
77 * pthread mutexes are designed for serializing threads over short pieces
78 * of code anyway, so wouldn't be an appropriate implementation of
79 * Python's locks regardless.
81 * The pthread_lock struct implements a Python lock as a "locked?" bit
82 * and a <condition, mutex> pair. In general, if the bit can be acquired
83 * instantly, it is, else the pair is used to block the thread until the
84 * bit is cleared. 9 May 1994 tim@ksr.com
87 typedef struct {
88 char locked; /* 0=unlocked, 1=locked */
89 /* a <cond, mutex> pair to handle an acquire of a locked lock */
90 pthread_cond_t lock_released;
91 pthread_mutex_t mut;
92 } pthread_lock;
94 #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
97 * Initialization.
100 #ifdef _HAVE_BSDI
101 static
102 void _noop(void)
106 static void
107 PyThread__init_thread(void)
109 /* DO AN INIT BY STARTING THE THREAD */
110 static int dummy = 0;
111 pthread_t thread1;
112 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
113 pthread_join(thread1, NULL);
116 #else /* !_HAVE_BSDI */
118 static void
119 PyThread__init_thread(void)
121 #if defined(_AIX) && defined(__GNUC__)
122 pthread_init();
123 #endif
126 #endif /* !_HAVE_BSDI */
129 * Thread support.
133 int
134 PyThread_start_new_thread(void (*func)(void *), void *arg)
136 pthread_t th;
137 int success;
138 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
139 pthread_attr_t attrs;
140 #endif
141 dprintf(("PyThread_start_new_thread called\n"));
142 if (!initialized)
143 PyThread_init_thread();
145 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
146 pthread_attr_init(&attrs);
147 #endif
148 #ifdef THREAD_STACK_SIZE
149 pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
150 #endif
151 #ifdef PTHREAD_SYSTEM_SCHED_SUPPORTED
152 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
153 #endif
154 success = pthread_create(&th,
155 #if defined(PY_PTHREAD_D4)
156 pthread_attr_default,
157 (pthread_startroutine_t)func,
158 (pthread_addr_t)arg
159 #elif defined(PY_PTHREAD_D6)
160 pthread_attr_default,
161 (void* (*)(void *))func,
163 #elif defined(PY_PTHREAD_D7)
164 pthread_attr_default,
165 func,
167 #elif defined(PY_PTHREAD_STD)
168 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
169 &attrs,
170 #else
171 (pthread_attr_t*)NULL,
172 #endif
173 (void* (*)(void *))func,
174 (void *)arg
175 #endif
177 #ifdef THREAD_STACK_SIZE
178 pthread_attr_destroy(&attrs);
179 #endif
180 if (success == 0) {
181 #if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
182 pthread_detach(&th);
183 #elif defined(PY_PTHREAD_STD)
184 pthread_detach(th);
185 #endif
187 return success != 0 ? 0 : 1;
190 /* XXX This implementation is considered (to quote Tim Peters) "inherently
191 hosed" because:
192 - It does not guanrantee the promise that a non-zero integer is returned.
193 - The cast to long is inherently unsafe.
194 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
195 latter return statement (for Alpha OSF/1) are any longer necessary.
197 long
198 PyThread_get_thread_ident(void)
200 volatile pthread_t threadid;
201 if (!initialized)
202 PyThread_init_thread();
203 /* Jump through some hoops for Alpha OSF/1 */
204 threadid = pthread_self();
205 #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
206 return (long) threadid;
207 #else
208 return (long) *(long *) &threadid;
209 #endif
212 static void
213 do_PyThread_exit_thread(int no_cleanup)
215 dprintf(("PyThread_exit_thread called\n"));
216 if (!initialized) {
217 if (no_cleanup)
218 _exit(0);
219 else
220 exit(0);
224 void
225 PyThread_exit_thread(void)
227 do_PyThread_exit_thread(0);
230 void
231 PyThread__exit_thread(void)
233 do_PyThread_exit_thread(1);
236 #ifndef NO_EXIT_PROG
237 static void
238 do_PyThread_exit_prog(int status, int no_cleanup)
240 dprintf(("PyThread_exit_prog(%d) called\n", status));
241 if (!initialized)
242 if (no_cleanup)
243 _exit(status);
244 else
245 exit(status);
248 void
249 PyThread_exit_prog(int status)
251 do_PyThread_exit_prog(status, 0);
254 void
255 PyThread__exit_prog(int status)
257 do_PyThread_exit_prog(status, 1);
259 #endif /* NO_EXIT_PROG */
262 * Lock support.
264 PyThread_type_lock
265 PyThread_allocate_lock(void)
267 pthread_lock *lock;
268 int status, error = 0;
270 dprintf(("PyThread_allocate_lock called\n"));
271 if (!initialized)
272 PyThread_init_thread();
274 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
275 memset((void *)lock, '\0', sizeof(pthread_lock));
276 if (lock) {
277 lock->locked = 0;
279 status = pthread_mutex_init(&lock->mut,
280 pthread_mutexattr_default);
281 CHECK_STATUS("pthread_mutex_init");
283 status = pthread_cond_init(&lock->lock_released,
284 pthread_condattr_default);
285 CHECK_STATUS("pthread_cond_init");
287 if (error) {
288 free((void *)lock);
289 lock = 0;
293 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
294 return (PyThread_type_lock) lock;
297 void
298 PyThread_free_lock(PyThread_type_lock lock)
300 pthread_lock *thelock = (pthread_lock *)lock;
301 int status, error = 0;
303 dprintf(("PyThread_free_lock(%p) called\n", lock));
305 status = pthread_mutex_destroy( &thelock->mut );
306 CHECK_STATUS("pthread_mutex_destroy");
308 status = pthread_cond_destroy( &thelock->lock_released );
309 CHECK_STATUS("pthread_cond_destroy");
311 free((void *)thelock);
314 int
315 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
317 int success;
318 pthread_lock *thelock = (pthread_lock *)lock;
319 int status, error = 0;
321 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
323 status = pthread_mutex_lock( &thelock->mut );
324 CHECK_STATUS("pthread_mutex_lock[1]");
325 success = thelock->locked == 0;
326 if (success) thelock->locked = 1;
327 status = pthread_mutex_unlock( &thelock->mut );
328 CHECK_STATUS("pthread_mutex_unlock[1]");
330 if ( !success && waitflag ) {
331 /* continue trying until we get the lock */
333 /* mut must be locked by me -- part of the condition
334 * protocol */
335 status = pthread_mutex_lock( &thelock->mut );
336 CHECK_STATUS("pthread_mutex_lock[2]");
337 while ( thelock->locked ) {
338 status = pthread_cond_wait(&thelock->lock_released,
339 &thelock->mut);
340 CHECK_STATUS("pthread_cond_wait");
342 thelock->locked = 1;
343 status = pthread_mutex_unlock( &thelock->mut );
344 CHECK_STATUS("pthread_mutex_unlock[2]");
345 success = 1;
347 if (error) success = 0;
348 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
349 return success;
352 void
353 PyThread_release_lock(PyThread_type_lock lock)
355 pthread_lock *thelock = (pthread_lock *)lock;
356 int status, error = 0;
358 dprintf(("PyThread_release_lock(%p) called\n", lock));
360 status = pthread_mutex_lock( &thelock->mut );
361 CHECK_STATUS("pthread_mutex_lock[3]");
363 thelock->locked = 0;
365 status = pthread_mutex_unlock( &thelock->mut );
366 CHECK_STATUS("pthread_mutex_unlock[3]");
368 /* wake up someone (anyone, if any) waiting on the lock */
369 status = pthread_cond_signal( &thelock->lock_released );
370 CHECK_STATUS("pthread_cond_signal");
374 * Semaphore support.
377 struct semaphore {
378 pthread_mutex_t mutex;
379 pthread_cond_t cond;
380 int value;
383 PyThread_type_sema
384 PyThread_allocate_sema(int value)
386 struct semaphore *sema;
387 int status, error = 0;
389 dprintf(("PyThread_allocate_sema called\n"));
390 if (!initialized)
391 PyThread_init_thread();
393 sema = (struct semaphore *) malloc(sizeof(struct semaphore));
394 if (sema != NULL) {
395 sema->value = value;
396 status = pthread_mutex_init(&sema->mutex,
397 pthread_mutexattr_default);
398 CHECK_STATUS("pthread_mutex_init");
399 status = pthread_cond_init(&sema->cond,
400 pthread_condattr_default);
401 CHECK_STATUS("pthread_cond_init");
402 if (error) {
403 free((void *) sema);
404 sema = NULL;
407 dprintf(("PyThread_allocate_sema() -> %p\n", sema));
408 return (PyThread_type_sema) sema;
411 void
412 PyThread_free_sema(PyThread_type_sema sema)
414 int status, error = 0;
415 struct semaphore *thesema = (struct semaphore *) sema;
417 dprintf(("PyThread_free_sema(%p) called\n", sema));
418 status = pthread_cond_destroy(&thesema->cond);
419 CHECK_STATUS("pthread_cond_destroy");
420 status = pthread_mutex_destroy(&thesema->mutex);
421 CHECK_STATUS("pthread_mutex_destroy");
422 free((void *) thesema);
425 int
426 PyThread_down_sema(PyThread_type_sema sema, int waitflag)
428 int status, error = 0, success;
429 struct semaphore *thesema = (struct semaphore *) sema;
431 dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag));
432 status = pthread_mutex_lock(&thesema->mutex);
433 CHECK_STATUS("pthread_mutex_lock");
434 if (waitflag) {
435 while (!error && thesema->value <= 0) {
436 status = pthread_cond_wait(&thesema->cond,
437 &thesema->mutex);
438 CHECK_STATUS("pthread_cond_wait");
441 if (error)
442 success = 0;
443 else if (thesema->value > 0) {
444 thesema->value--;
445 success = 1;
447 else
448 success = 0;
449 status = pthread_mutex_unlock(&thesema->mutex);
450 CHECK_STATUS("pthread_mutex_unlock");
451 dprintf(("PyThread_down_sema(%p) return\n", sema));
452 return success;
455 void
456 PyThread_up_sema(PyThread_type_sema sema)
458 int status, error = 0;
459 struct semaphore *thesema = (struct semaphore *) sema;
461 dprintf(("PyThread_up_sema(%p)\n", sema));
462 status = pthread_mutex_lock(&thesema->mutex);
463 CHECK_STATUS("pthread_mutex_lock");
464 thesema->value++;
465 status = pthread_cond_signal(&thesema->cond);
466 CHECK_STATUS("pthread_cond_signal");
467 status = pthread_mutex_unlock(&thesema->mutex);
468 CHECK_STATUS("pthread_mutex_unlock");