2 /* Posix threads interface */
6 #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
7 #define destructor xxdestructor
10 #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
15 /* The POSIX spec says that implementations supporting the sem_*
16 family of functions must indicate this by defining
18 #ifdef _POSIX_SEMAPHORES
19 #include <semaphore.h>
23 #if !defined(pthread_attr_default)
24 # define pthread_attr_default ((pthread_attr_t *)NULL)
26 #if !defined(pthread_mutexattr_default)
27 # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
29 #if !defined(pthread_condattr_default)
30 # define pthread_condattr_default ((pthread_condattr_t *)NULL)
34 /* Whether or not to use semaphores directly rather than emulating them with
35 * mutexes and condition variables:
37 #if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
38 # define USE_SEMAPHORES
40 # undef USE_SEMAPHORES
44 /* On platforms that don't use standard POSIX threads pthread_sigmask()
45 * isn't present. DEC threads uses sigprocmask() instead as do most
46 * other UNIX International compliant systems that don't have the full
47 * pthread implementation.
49 #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
50 # define SET_THREAD_SIGMASK pthread_sigmask
52 # define SET_THREAD_SIGMASK sigprocmask
56 /* A pthread mutex isn't sufficient to model the Python lock type
57 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
58 * following are undefined:
59 * -> a thread tries to lock a mutex it already has locked
60 * -> a thread tries to unlock a mutex locked by a different thread
61 * pthread mutexes are designed for serializing threads over short pieces
62 * of code anyway, so wouldn't be an appropriate implementation of
63 * Python's locks regardless.
65 * The pthread_lock struct implements a Python lock as a "locked?" bit
66 * and a <condition, mutex> pair. In general, if the bit can be acquired
67 * instantly, it is, else the pair is used to block the thread until the
68 * bit is cleared. 9 May 1994 tim@ksr.com
72 char locked
; /* 0=unlocked, 1=locked */
73 /* a <cond, mutex> pair to handle an acquire of a locked lock */
74 pthread_cond_t lock_released
;
78 #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
91 PyThread__init_thread(void)
93 /* DO AN INIT BY STARTING THE THREAD */
96 pthread_create(&thread1
, NULL
, (void *) _noop
, &dummy
);
97 pthread_join(thread1
, NULL
);
100 #else /* !_HAVE_BSDI */
103 PyThread__init_thread(void)
105 #if defined(_AIX) && defined(__GNUC__)
110 #endif /* !_HAVE_BSDI */
118 PyThread_start_new_thread(void (*func
)(void *), void *arg
)
122 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
123 pthread_attr_t attrs
;
125 dprintf(("PyThread_start_new_thread called\n"));
127 PyThread_init_thread();
129 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
130 pthread_attr_init(&attrs
);
132 #ifdef THREAD_STACK_SIZE
133 pthread_attr_setstacksize(&attrs
, THREAD_STACK_SIZE
);
135 #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) && !defined(__FreeBSD__)
136 pthread_attr_setscope(&attrs
, PTHREAD_SCOPE_SYSTEM
);
139 status
= pthread_create(&th
,
140 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
143 (pthread_attr_t
*)NULL
,
145 (void* (*)(void *))func
,
149 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
150 pthread_attr_destroy(&attrs
);
157 #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
160 return (long) *(long *) &th
;
164 /* XXX This implementation is considered (to quote Tim Peters) "inherently
166 - It does not guarantee the promise that a non-zero integer is returned.
167 - The cast to long is inherently unsafe.
168 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
169 latter return statement (for Alpha OSF/1) are any longer necessary.
172 PyThread_get_thread_ident(void)
174 volatile pthread_t threadid
;
176 PyThread_init_thread();
177 /* Jump through some hoops for Alpha OSF/1 */
178 threadid
= pthread_self();
179 #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
180 return (long) threadid
;
182 return (long) *(long *) &threadid
;
187 do_PyThread_exit_thread(int no_cleanup
)
189 dprintf(("PyThread_exit_thread called\n"));
199 PyThread_exit_thread(void)
201 do_PyThread_exit_thread(0);
205 PyThread__exit_thread(void)
207 do_PyThread_exit_thread(1);
212 do_PyThread_exit_prog(int status
, int no_cleanup
)
214 dprintf(("PyThread_exit_prog(%d) called\n", status
));
223 PyThread_exit_prog(int status
)
225 do_PyThread_exit_prog(status
, 0);
229 PyThread__exit_prog(int status
)
231 do_PyThread_exit_prog(status
, 1);
233 #endif /* NO_EXIT_PROG */
235 #ifdef USE_SEMAPHORES
242 PyThread_allocate_lock(void)
245 int status
, error
= 0;
247 dprintf(("PyThread_allocate_lock called\n"));
249 PyThread_init_thread();
251 lock
= (sem_t
*)malloc(sizeof(sem_t
));
254 status
= sem_init(lock
,0,1);
255 CHECK_STATUS("sem_init");
263 dprintf(("PyThread_allocate_lock() -> %p\n", lock
));
264 return (PyThread_type_lock
)lock
;
268 PyThread_free_lock(PyThread_type_lock lock
)
270 sem_t
*thelock
= (sem_t
*)lock
;
271 int status
, error
= 0;
273 dprintf(("PyThread_free_lock(%p) called\n", lock
));
278 status
= sem_destroy(thelock
);
279 CHECK_STATUS("sem_destroy");
281 free((void *)thelock
);
285 * As of February 2002, Cygwin thread implementations mistakenly report error
286 * codes in the return value of the sem_ calls (like the pthread_ functions).
287 * Correct implementations return -1 and put the code in errno. This supports
291 fix_status(int status
)
293 return (status
== -1) ? errno
: status
;
297 PyThread_acquire_lock(PyThread_type_lock lock
, int waitflag
)
300 sem_t
*thelock
= (sem_t
*)lock
;
301 int status
, error
= 0;
303 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock
, waitflag
));
307 status
= fix_status(sem_wait(thelock
));
309 status
= fix_status(sem_trywait(thelock
));
310 } while (status
== EINTR
); /* Retry if interrupted by a signal */
313 CHECK_STATUS("sem_wait");
314 } else if (status
!= EAGAIN
) {
315 CHECK_STATUS("sem_trywait");
318 success
= (status
== 0) ? 1 : 0;
320 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock
, waitflag
, success
));
325 PyThread_release_lock(PyThread_type_lock lock
)
327 sem_t
*thelock
= (sem_t
*)lock
;
328 int status
, error
= 0;
330 dprintf(("PyThread_release_lock(%p) called\n", lock
));
332 status
= sem_post(thelock
);
333 CHECK_STATUS("sem_post");
336 #else /* USE_SEMAPHORES */
342 PyThread_allocate_lock(void)
345 int status
, error
= 0;
347 dprintf(("PyThread_allocate_lock called\n"));
349 PyThread_init_thread();
351 lock
= (pthread_lock
*) malloc(sizeof(pthread_lock
));
352 memset((void *)lock
, '\0', sizeof(pthread_lock
));
356 status
= pthread_mutex_init(&lock
->mut
,
357 pthread_mutexattr_default
);
358 CHECK_STATUS("pthread_mutex_init");
360 status
= pthread_cond_init(&lock
->lock_released
,
361 pthread_condattr_default
);
362 CHECK_STATUS("pthread_cond_init");
370 dprintf(("PyThread_allocate_lock() -> %p\n", lock
));
371 return (PyThread_type_lock
) lock
;
375 PyThread_free_lock(PyThread_type_lock lock
)
377 pthread_lock
*thelock
= (pthread_lock
*)lock
;
378 int status
, error
= 0;
380 dprintf(("PyThread_free_lock(%p) called\n", lock
));
382 status
= pthread_mutex_destroy( &thelock
->mut
);
383 CHECK_STATUS("pthread_mutex_destroy");
385 status
= pthread_cond_destroy( &thelock
->lock_released
);
386 CHECK_STATUS("pthread_cond_destroy");
388 free((void *)thelock
);
392 PyThread_acquire_lock(PyThread_type_lock lock
, int waitflag
)
395 pthread_lock
*thelock
= (pthread_lock
*)lock
;
396 int status
, error
= 0;
398 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock
, waitflag
));
400 status
= pthread_mutex_lock( &thelock
->mut
);
401 CHECK_STATUS("pthread_mutex_lock[1]");
402 success
= thelock
->locked
== 0;
404 if ( !success
&& waitflag
) {
405 /* continue trying until we get the lock */
407 /* mut must be locked by me -- part of the condition
409 while ( thelock
->locked
) {
410 status
= pthread_cond_wait(&thelock
->lock_released
,
412 CHECK_STATUS("pthread_cond_wait");
416 if (success
) thelock
->locked
= 1;
417 status
= pthread_mutex_unlock( &thelock
->mut
);
418 CHECK_STATUS("pthread_mutex_unlock[1]");
420 if (error
) success
= 0;
421 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock
, waitflag
, success
));
426 PyThread_release_lock(PyThread_type_lock lock
)
428 pthread_lock
*thelock
= (pthread_lock
*)lock
;
429 int status
, error
= 0;
431 dprintf(("PyThread_release_lock(%p) called\n", lock
));
433 status
= pthread_mutex_lock( &thelock
->mut
);
434 CHECK_STATUS("pthread_mutex_lock[3]");
438 status
= pthread_mutex_unlock( &thelock
->mut
);
439 CHECK_STATUS("pthread_mutex_unlock[3]");
441 /* wake up someone (anyone, if any) waiting on the lock */
442 status
= pthread_cond_signal( &thelock
->lock_released
);
443 CHECK_STATUS("pthread_cond_signal");
446 #endif /* USE_SEMAPHORES */