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 /* set default attribute object for different versions */
54 #if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7)
55 # define pthread_attr_default pthread_attr_default
56 # define pthread_mutexattr_default pthread_mutexattr_default
57 # define pthread_condattr_default pthread_condattr_default
58 #elif defined(PY_PTHREAD_STD) || defined(PY_PTHREAD_D6)
59 # define pthread_attr_default ((pthread_attr_t *)NULL)
60 # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
61 # define pthread_condattr_default ((pthread_condattr_t *)NULL)
65 /* A pthread mutex isn't sufficient to model the Python lock type
66 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
67 * following are undefined:
68 * -> a thread tries to lock a mutex it already has locked
69 * -> a thread tries to unlock a mutex locked by a different thread
70 * pthread mutexes are designed for serializing threads over short pieces
71 * of code anyway, so wouldn't be an appropriate implementation of
72 * Python's locks regardless.
74 * The pthread_lock struct implements a Python lock as a "locked?" bit
75 * and a <condition, mutex> pair. In general, if the bit can be acquired
76 * instantly, it is, else the pair is used to block the thread until the
77 * bit is cleared. 9 May 1994 tim@ksr.com
81 char locked
; /* 0=unlocked, 1=locked */
82 /* a <cond, mutex> pair to handle an acquire of a locked lock */
83 pthread_cond_t lock_released
;
87 #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
100 PyThread__init_thread(void)
102 /* DO AN INIT BY STARTING THE THREAD */
103 static int dummy
= 0;
105 pthread_create(&thread1
, NULL
, (void *) _noop
, &dummy
);
106 pthread_join(thread1
, NULL
);
109 #else /* !_HAVE_BSDI */
112 PyThread__init_thread(void)
114 #if defined(_AIX) && defined(__GNUC__)
119 #endif /* !_HAVE_BSDI */
127 PyThread_start_new_thread(void (*func
)(void *), void *arg
)
131 dprintf(("PyThread_start_new_thread called\n"));
133 PyThread_init_thread();
135 success
= pthread_create(&th
,
136 #if defined(PY_PTHREAD_D4)
137 pthread_attr_default
,
138 (pthread_startroutine_t
)func
,
140 #elif defined(PY_PTHREAD_D6)
141 pthread_attr_default
,
142 (void* (*)(void *))func
,
144 #elif defined(PY_PTHREAD_D7)
145 pthread_attr_default
,
148 #elif defined(PY_PTHREAD_STD)
149 (pthread_attr_t
*)NULL
,
150 (void* (*)(void *))func
,
156 #if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
158 #elif defined(PY_PTHREAD_STD)
162 return success
!= 0 ? 0 : 1;
165 /* XXX This implementation is considered (to quote Tim Peters) "inherently
167 - It does not guanrantee the promise that a non-zero integer is returned.
168 - The cast to long is inherently unsafe.
169 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
170 latter return statement (for Alpha OSF/1) are any longer necessary.
173 PyThread_get_thread_ident(void)
175 volatile pthread_t threadid
;
177 PyThread_init_thread();
178 /* Jump through some hoops for Alpha OSF/1 */
179 threadid
= pthread_self();
180 #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
181 return (long) threadid
;
183 return (long) *(long *) &threadid
;
188 do_PyThread_exit_thread(int no_cleanup
)
190 dprintf(("PyThread_exit_thread called\n"));
200 PyThread_exit_thread(void)
202 do_PyThread_exit_thread(0);
206 PyThread__exit_thread(void)
208 do_PyThread_exit_thread(1);
213 do_PyThread_exit_prog(int status
, int no_cleanup
)
215 dprintf(("PyThread_exit_prog(%d) called\n", status
));
224 PyThread_exit_prog(int status
)
226 do_PyThread_exit_prog(status
, 0);
230 PyThread__exit_prog(int status
)
232 do_PyThread_exit_prog(status
, 1);
234 #endif /* NO_EXIT_PROG */
240 PyThread_allocate_lock(void)
243 int status
, error
= 0;
245 dprintf(("PyThread_allocate_lock called\n"));
247 PyThread_init_thread();
249 lock
= (pthread_lock
*) malloc(sizeof(pthread_lock
));
250 memset((void *)lock
, '\0', sizeof(pthread_lock
));
254 status
= pthread_mutex_init(&lock
->mut
,
255 pthread_mutexattr_default
);
256 CHECK_STATUS("pthread_mutex_init");
258 status
= pthread_cond_init(&lock
->lock_released
,
259 pthread_condattr_default
);
260 CHECK_STATUS("pthread_cond_init");
268 dprintf(("PyThread_allocate_lock() -> %p\n", lock
));
269 return (PyThread_type_lock
) lock
;
273 PyThread_free_lock(PyThread_type_lock lock
)
275 pthread_lock
*thelock
= (pthread_lock
*)lock
;
276 int status
, error
= 0;
278 dprintf(("PyThread_free_lock(%p) called\n", lock
));
280 status
= pthread_mutex_destroy( &thelock
->mut
);
281 CHECK_STATUS("pthread_mutex_destroy");
283 status
= pthread_cond_destroy( &thelock
->lock_released
);
284 CHECK_STATUS("pthread_cond_destroy");
286 free((void *)thelock
);
290 PyThread_acquire_lock(PyThread_type_lock lock
, int waitflag
)
293 pthread_lock
*thelock
= (pthread_lock
*)lock
;
294 int status
, error
= 0;
296 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock
, waitflag
));
298 status
= pthread_mutex_lock( &thelock
->mut
);
299 CHECK_STATUS("pthread_mutex_lock[1]");
300 success
= thelock
->locked
== 0;
301 if (success
) thelock
->locked
= 1;
302 status
= pthread_mutex_unlock( &thelock
->mut
);
303 CHECK_STATUS("pthread_mutex_unlock[1]");
305 if ( !success
&& waitflag
) {
306 /* continue trying until we get the lock */
308 /* mut must be locked by me -- part of the condition
310 status
= pthread_mutex_lock( &thelock
->mut
);
311 CHECK_STATUS("pthread_mutex_lock[2]");
312 while ( thelock
->locked
) {
313 status
= pthread_cond_wait(&thelock
->lock_released
,
315 CHECK_STATUS("pthread_cond_wait");
318 status
= pthread_mutex_unlock( &thelock
->mut
);
319 CHECK_STATUS("pthread_mutex_unlock[2]");
322 if (error
) success
= 0;
323 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock
, waitflag
, success
));
328 PyThread_release_lock(PyThread_type_lock lock
)
330 pthread_lock
*thelock
= (pthread_lock
*)lock
;
331 int status
, error
= 0;
333 dprintf(("PyThread_release_lock(%p) called\n", lock
));
335 status
= pthread_mutex_lock( &thelock
->mut
);
336 CHECK_STATUS("pthread_mutex_lock[3]");
340 status
= pthread_mutex_unlock( &thelock
->mut
);
341 CHECK_STATUS("pthread_mutex_unlock[3]");
343 /* wake up someone (anyone, if any) waiting on the lock */
344 status
= pthread_cond_signal( &thelock
->lock_released
);
345 CHECK_STATUS("pthread_cond_signal");
353 pthread_mutex_t mutex
;
359 PyThread_allocate_sema(int value
)
361 struct semaphore
*sema
;
362 int status
, error
= 0;
364 dprintf(("PyThread_allocate_sema called\n"));
366 PyThread_init_thread();
368 sema
= (struct semaphore
*) malloc(sizeof(struct semaphore
));
371 status
= pthread_mutex_init(&sema
->mutex
,
372 pthread_mutexattr_default
);
373 CHECK_STATUS("pthread_mutex_init");
374 status
= pthread_cond_init(&sema
->cond
,
375 pthread_condattr_default
);
376 CHECK_STATUS("pthread_cond_init");
382 dprintf(("PyThread_allocate_sema() -> %p\n", sema
));
383 return (PyThread_type_sema
) sema
;
387 PyThread_free_sema(PyThread_type_sema sema
)
389 int status
, error
= 0;
390 struct semaphore
*thesema
= (struct semaphore
*) sema
;
392 dprintf(("PyThread_free_sema(%p) called\n", sema
));
393 status
= pthread_cond_destroy(&thesema
->cond
);
394 CHECK_STATUS("pthread_cond_destroy");
395 status
= pthread_mutex_destroy(&thesema
->mutex
);
396 CHECK_STATUS("pthread_mutex_destroy");
397 free((void *) thesema
);
401 PyThread_down_sema(PyThread_type_sema sema
, int waitflag
)
403 int status
, error
= 0, success
;
404 struct semaphore
*thesema
= (struct semaphore
*) sema
;
406 dprintf(("PyThread_down_sema(%p, %d) called\n", sema
, waitflag
));
407 status
= pthread_mutex_lock(&thesema
->mutex
);
408 CHECK_STATUS("pthread_mutex_lock");
410 while (!error
&& thesema
->value
<= 0) {
411 status
= pthread_cond_wait(&thesema
->cond
,
413 CHECK_STATUS("pthread_cond_wait");
418 else if (thesema
->value
> 0) {
424 status
= pthread_mutex_unlock(&thesema
->mutex
);
425 CHECK_STATUS("pthread_mutex_unlock");
426 dprintf(("PyThread_down_sema(%p) return\n", sema
));
431 PyThread_up_sema(PyThread_type_sema sema
)
433 int status
, error
= 0;
434 struct semaphore
*thesema
= (struct semaphore
*) sema
;
436 dprintf(("PyThread_up_sema(%p)\n", sema
));
437 status
= pthread_mutex_lock(&thesema
->mutex
);
438 CHECK_STATUS("pthread_mutex_lock");
440 status
= pthread_cond_signal(&thesema
->cond
);
441 CHECK_STATUS("pthread_cond_signal");
442 status
= pthread_mutex_unlock(&thesema
->mutex
);
443 CHECK_STATUS("pthread_mutex_unlock");