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 /* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
20 we need to add 0 to make it work there as well. */
21 #if (_POSIX_SEMAPHORES+0) == -1
22 #define HAVE_BROKEN_POSIX_SEMAPHORES
24 #include <semaphore.h>
29 /* Before FreeBSD 5.4, system scope threads was very limited resource
30 in default setting. So the process scope is preferred to get
31 enough number of threads to work. */
33 #include <osreldate.h>
34 #if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101
35 #undef PTHREAD_SYSTEM_SCHED_SUPPORTED
39 #if !defined(pthread_attr_default)
40 # define pthread_attr_default ((pthread_attr_t *)NULL)
42 #if !defined(pthread_mutexattr_default)
43 # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
45 #if !defined(pthread_condattr_default)
46 # define pthread_condattr_default ((pthread_condattr_t *)NULL)
50 /* Whether or not to use semaphores directly rather than emulating them with
51 * mutexes and condition variables:
53 #if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
54 # define USE_SEMAPHORES
56 # undef USE_SEMAPHORES
60 /* On platforms that don't use standard POSIX threads pthread_sigmask()
61 * isn't present. DEC threads uses sigprocmask() instead as do most
62 * other UNIX International compliant systems that don't have the full
63 * pthread implementation.
65 #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
66 # define SET_THREAD_SIGMASK pthread_sigmask
68 # define SET_THREAD_SIGMASK sigprocmask
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 #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
152 pthread_attr_setscope(&attrs
, PTHREAD_SCOPE_SYSTEM
);
155 status
= pthread_create(&th
,
156 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
159 (pthread_attr_t
*)NULL
,
161 (void* (*)(void *))func
,
165 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
166 pthread_attr_destroy(&attrs
);
173 #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
176 return (long) *(long *) &th
;
180 /* XXX This implementation is considered (to quote Tim Peters) "inherently
182 - It does not guarantee the promise that a non-zero integer is returned.
183 - The cast to long is inherently unsafe.
184 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
185 latter return statement (for Alpha OSF/1) are any longer necessary.
188 PyThread_get_thread_ident(void)
190 volatile pthread_t threadid
;
192 PyThread_init_thread();
193 /* Jump through some hoops for Alpha OSF/1 */
194 threadid
= pthread_self();
195 #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
196 return (long) threadid
;
198 return (long) *(long *) &threadid
;
203 do_PyThread_exit_thread(int no_cleanup
)
205 dprintf(("PyThread_exit_thread called\n"));
215 PyThread_exit_thread(void)
217 do_PyThread_exit_thread(0);
221 PyThread__exit_thread(void)
223 do_PyThread_exit_thread(1);
228 do_PyThread_exit_prog(int status
, int no_cleanup
)
230 dprintf(("PyThread_exit_prog(%d) called\n", status
));
239 PyThread_exit_prog(int status
)
241 do_PyThread_exit_prog(status
, 0);
245 PyThread__exit_prog(int status
)
247 do_PyThread_exit_prog(status
, 1);
249 #endif /* NO_EXIT_PROG */
251 #ifdef USE_SEMAPHORES
258 PyThread_allocate_lock(void)
261 int status
, error
= 0;
263 dprintf(("PyThread_allocate_lock called\n"));
265 PyThread_init_thread();
267 lock
= (sem_t
*)malloc(sizeof(sem_t
));
270 status
= sem_init(lock
,0,1);
271 CHECK_STATUS("sem_init");
279 dprintf(("PyThread_allocate_lock() -> %p\n", lock
));
280 return (PyThread_type_lock
)lock
;
284 PyThread_free_lock(PyThread_type_lock lock
)
286 sem_t
*thelock
= (sem_t
*)lock
;
287 int status
, error
= 0;
289 dprintf(("PyThread_free_lock(%p) called\n", lock
));
294 status
= sem_destroy(thelock
);
295 CHECK_STATUS("sem_destroy");
297 free((void *)thelock
);
301 * As of February 2002, Cygwin thread implementations mistakenly report error
302 * codes in the return value of the sem_ calls (like the pthread_ functions).
303 * Correct implementations return -1 and put the code in errno. This supports
307 fix_status(int status
)
309 return (status
== -1) ? errno
: status
;
313 PyThread_acquire_lock(PyThread_type_lock lock
, int waitflag
)
316 sem_t
*thelock
= (sem_t
*)lock
;
317 int status
, error
= 0;
319 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock
, waitflag
));
323 status
= fix_status(sem_wait(thelock
));
325 status
= fix_status(sem_trywait(thelock
));
326 } while (status
== EINTR
); /* Retry if interrupted by a signal */
329 CHECK_STATUS("sem_wait");
330 } else if (status
!= EAGAIN
) {
331 CHECK_STATUS("sem_trywait");
334 success
= (status
== 0) ? 1 : 0;
336 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock
, waitflag
, success
));
341 PyThread_release_lock(PyThread_type_lock lock
)
343 sem_t
*thelock
= (sem_t
*)lock
;
344 int status
, error
= 0;
346 dprintf(("PyThread_release_lock(%p) called\n", lock
));
348 status
= sem_post(thelock
);
349 CHECK_STATUS("sem_post");
352 #else /* USE_SEMAPHORES */
358 PyThread_allocate_lock(void)
361 int status
, error
= 0;
363 dprintf(("PyThread_allocate_lock called\n"));
365 PyThread_init_thread();
367 lock
= (pthread_lock
*) malloc(sizeof(pthread_lock
));
369 memset((void *)lock
, '\0', sizeof(pthread_lock
));
372 status
= pthread_mutex_init(&lock
->mut
,
373 pthread_mutexattr_default
);
374 CHECK_STATUS("pthread_mutex_init");
376 status
= pthread_cond_init(&lock
->lock_released
,
377 pthread_condattr_default
);
378 CHECK_STATUS("pthread_cond_init");
386 dprintf(("PyThread_allocate_lock() -> %p\n", lock
));
387 return (PyThread_type_lock
) lock
;
391 PyThread_free_lock(PyThread_type_lock lock
)
393 pthread_lock
*thelock
= (pthread_lock
*)lock
;
394 int status
, error
= 0;
396 dprintf(("PyThread_free_lock(%p) called\n", lock
));
398 status
= pthread_mutex_destroy( &thelock
->mut
);
399 CHECK_STATUS("pthread_mutex_destroy");
401 status
= pthread_cond_destroy( &thelock
->lock_released
);
402 CHECK_STATUS("pthread_cond_destroy");
404 free((void *)thelock
);
408 PyThread_acquire_lock(PyThread_type_lock lock
, int waitflag
)
411 pthread_lock
*thelock
= (pthread_lock
*)lock
;
412 int status
, error
= 0;
414 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock
, waitflag
));
416 status
= pthread_mutex_lock( &thelock
->mut
);
417 CHECK_STATUS("pthread_mutex_lock[1]");
418 success
= thelock
->locked
== 0;
420 if ( !success
&& waitflag
) {
421 /* continue trying until we get the lock */
423 /* mut must be locked by me -- part of the condition
425 while ( thelock
->locked
) {
426 status
= pthread_cond_wait(&thelock
->lock_released
,
428 CHECK_STATUS("pthread_cond_wait");
432 if (success
) thelock
->locked
= 1;
433 status
= pthread_mutex_unlock( &thelock
->mut
);
434 CHECK_STATUS("pthread_mutex_unlock[1]");
436 if (error
) success
= 0;
437 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock
, waitflag
, success
));
442 PyThread_release_lock(PyThread_type_lock lock
)
444 pthread_lock
*thelock
= (pthread_lock
*)lock
;
445 int status
, error
= 0;
447 dprintf(("PyThread_release_lock(%p) called\n", lock
));
449 status
= pthread_mutex_lock( &thelock
->mut
);
450 CHECK_STATUS("pthread_mutex_lock[3]");
454 status
= pthread_mutex_unlock( &thelock
->mut
);
455 CHECK_STATUS("pthread_mutex_unlock[3]");
457 /* wake up someone (anyone, if any) waiting on the lock */
458 status
= pthread_cond_signal( &thelock
->lock_released
);
459 CHECK_STATUS("pthread_cond_signal");
462 #endif /* USE_SEMAPHORES */