2 /* Posix threads interface */
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
25 # define PY_PTHREAD_STD
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
37 # define PY_PTHREAD_D7
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
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
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)
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
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
;
94 #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
107 PyThread__init_thread(void)
109 /* DO AN INIT BY STARTING THE THREAD */
110 static int dummy
= 0;
112 pthread_create(&thread1
, NULL
, (void *) _noop
, &dummy
);
113 pthread_join(thread1
, NULL
);
116 #else /* !_HAVE_BSDI */
119 PyThread__init_thread(void)
121 #if defined(_AIX) && defined(__GNUC__)
126 #endif /* !_HAVE_BSDI */
134 PyThread_start_new_thread(void (*func
)(void *), void *arg
)
138 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
139 pthread_attr_t attrs
;
141 dprintf(("PyThread_start_new_thread called\n"));
143 PyThread_init_thread();
145 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
146 pthread_attr_init(&attrs
);
148 #ifdef THREAD_STACK_SIZE
149 pthread_attr_setstacksize(&attrs
, THREAD_STACK_SIZE
);
151 #ifdef PTHREAD_SYSTEM_SCHED_SUPPORTED
152 pthread_attr_setscope(&attrs
, PTHREAD_SCOPE_SYSTEM
);
154 success
= pthread_create(&th
,
155 #if defined(PY_PTHREAD_D4)
156 pthread_attr_default
,
157 (pthread_startroutine_t
)func
,
159 #elif defined(PY_PTHREAD_D6)
160 pthread_attr_default
,
161 (void* (*)(void *))func
,
163 #elif defined(PY_PTHREAD_D7)
164 pthread_attr_default
,
167 #elif defined(PY_PTHREAD_STD)
168 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
171 (pthread_attr_t
*)NULL
,
173 (void* (*)(void *))func
,
177 #ifdef THREAD_STACK_SIZE
178 pthread_attr_destroy(&attrs
);
181 #if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
183 #elif defined(PY_PTHREAD_STD)
187 return success
!= 0 ? 0 : 1;
190 /* XXX This implementation is considered (to quote Tim Peters) "inherently
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.
198 PyThread_get_thread_ident(void)
200 volatile pthread_t threadid
;
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
;
208 return (long) *(long *) &threadid
;
213 do_PyThread_exit_thread(int no_cleanup
)
215 dprintf(("PyThread_exit_thread called\n"));
225 PyThread_exit_thread(void)
227 do_PyThread_exit_thread(0);
231 PyThread__exit_thread(void)
233 do_PyThread_exit_thread(1);
238 do_PyThread_exit_prog(int status
, int no_cleanup
)
240 dprintf(("PyThread_exit_prog(%d) called\n", status
));
249 PyThread_exit_prog(int status
)
251 do_PyThread_exit_prog(status
, 0);
255 PyThread__exit_prog(int status
)
257 do_PyThread_exit_prog(status
, 1);
259 #endif /* NO_EXIT_PROG */
265 PyThread_allocate_lock(void)
268 int status
, error
= 0;
270 dprintf(("PyThread_allocate_lock called\n"));
272 PyThread_init_thread();
274 lock
= (pthread_lock
*) malloc(sizeof(pthread_lock
));
275 memset((void *)lock
, '\0', sizeof(pthread_lock
));
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");
293 dprintf(("PyThread_allocate_lock() -> %p\n", lock
));
294 return (PyThread_type_lock
) lock
;
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
);
315 PyThread_acquire_lock(PyThread_type_lock lock
, int waitflag
)
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
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
,
340 CHECK_STATUS("pthread_cond_wait");
343 status
= pthread_mutex_unlock( &thelock
->mut
);
344 CHECK_STATUS("pthread_mutex_unlock[2]");
347 if (error
) success
= 0;
348 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock
, waitflag
, success
));
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]");
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");
378 pthread_mutex_t mutex
;
384 PyThread_allocate_sema(int value
)
386 struct semaphore
*sema
;
387 int status
, error
= 0;
389 dprintf(("PyThread_allocate_sema called\n"));
391 PyThread_init_thread();
393 sema
= (struct semaphore
*) malloc(sizeof(struct semaphore
));
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");
407 dprintf(("PyThread_allocate_sema() -> %p\n", sema
));
408 return (PyThread_type_sema
) sema
;
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
);
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");
435 while (!error
&& thesema
->value
<= 0) {
436 status
= pthread_cond_wait(&thesema
->cond
,
438 CHECK_STATUS("pthread_cond_wait");
443 else if (thesema
->value
> 0) {
449 status
= pthread_mutex_unlock(&thesema
->mutex
);
450 CHECK_STATUS("pthread_mutex_unlock");
451 dprintf(("PyThread_down_sema(%p) return\n", sema
));
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");
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");