* fix DBCursor.pget() bug with keyword argument names when no data= is
[python.git] / Python / thread_pthread.h
blobc29a61c809a2a2ca110ee290b90e663f12f51d02
2 /* Posix threads interface */
4 #include <stdlib.h>
5 #include <string.h>
6 #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
7 #define destructor xxdestructor
8 #endif
9 #include <pthread.h>
10 #if defined(__APPLE__) || defined(HAVE_PTHREAD_DESTRUCTOR)
11 #undef destructor
12 #endif
13 #include <signal.h>
15 /* The POSIX spec says that implementations supporting the sem_*
16 family of functions must indicate this by defining
17 _POSIX_SEMAPHORES. */
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
23 #else
24 #include <semaphore.h>
25 #include <errno.h>
26 #endif
27 #endif
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. */
32 #ifdef __FreeBSD__
33 #include <osreldate.h>
34 #if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101
35 #undef PTHREAD_SYSTEM_SCHED_SUPPORTED
36 #endif
37 #endif
39 #if !defined(pthread_attr_default)
40 # define pthread_attr_default ((pthread_attr_t *)NULL)
41 #endif
42 #if !defined(pthread_mutexattr_default)
43 # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
44 #endif
45 #if !defined(pthread_condattr_default)
46 # define pthread_condattr_default ((pthread_condattr_t *)NULL)
47 #endif
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
55 #else
56 # undef USE_SEMAPHORES
57 #endif
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
67 #else
68 # define SET_THREAD_SIGMASK sigprocmask
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 long
134 PyThread_start_new_thread(void (*func)(void *), void *arg)
136 pthread_t th;
137 int status;
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 #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
152 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
153 #endif
155 status = pthread_create(&th,
156 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
157 &attrs,
158 #else
159 (pthread_attr_t*)NULL,
160 #endif
161 (void* (*)(void *))func,
162 (void *)arg
165 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
166 pthread_attr_destroy(&attrs);
167 #endif
168 if (status != 0)
169 return -1;
171 pthread_detach(th);
173 #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
174 return (long) th;
175 #else
176 return (long) *(long *) &th;
177 #endif
180 /* XXX This implementation is considered (to quote Tim Peters) "inherently
181 hosed" because:
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.
187 long
188 PyThread_get_thread_ident(void)
190 volatile pthread_t threadid;
191 if (!initialized)
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;
197 #else
198 return (long) *(long *) &threadid;
199 #endif
202 static void
203 do_PyThread_exit_thread(int no_cleanup)
205 dprintf(("PyThread_exit_thread called\n"));
206 if (!initialized) {
207 if (no_cleanup)
208 _exit(0);
209 else
210 exit(0);
214 void
215 PyThread_exit_thread(void)
217 do_PyThread_exit_thread(0);
220 void
221 PyThread__exit_thread(void)
223 do_PyThread_exit_thread(1);
226 #ifndef NO_EXIT_PROG
227 static void
228 do_PyThread_exit_prog(int status, int no_cleanup)
230 dprintf(("PyThread_exit_prog(%d) called\n", status));
231 if (!initialized)
232 if (no_cleanup)
233 _exit(status);
234 else
235 exit(status);
238 void
239 PyThread_exit_prog(int status)
241 do_PyThread_exit_prog(status, 0);
244 void
245 PyThread__exit_prog(int status)
247 do_PyThread_exit_prog(status, 1);
249 #endif /* NO_EXIT_PROG */
251 #ifdef USE_SEMAPHORES
254 * Lock support.
257 PyThread_type_lock
258 PyThread_allocate_lock(void)
260 sem_t *lock;
261 int status, error = 0;
263 dprintf(("PyThread_allocate_lock called\n"));
264 if (!initialized)
265 PyThread_init_thread();
267 lock = (sem_t *)malloc(sizeof(sem_t));
269 if (lock) {
270 status = sem_init(lock,0,1);
271 CHECK_STATUS("sem_init");
273 if (error) {
274 free((void *)lock);
275 lock = NULL;
279 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
280 return (PyThread_type_lock)lock;
283 void
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));
291 if (!thelock)
292 return;
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
304 * either.
306 static int
307 fix_status(int status)
309 return (status == -1) ? errno : status;
312 int
313 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
315 int success;
316 sem_t *thelock = (sem_t *)lock;
317 int status, error = 0;
319 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
321 do {
322 if (waitflag)
323 status = fix_status(sem_wait(thelock));
324 else
325 status = fix_status(sem_trywait(thelock));
326 } while (status == EINTR); /* Retry if interrupted by a signal */
328 if (waitflag) {
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));
337 return success;
340 void
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 */
355 * Lock support.
357 PyThread_type_lock
358 PyThread_allocate_lock(void)
360 pthread_lock *lock;
361 int status, error = 0;
363 dprintf(("PyThread_allocate_lock called\n"));
364 if (!initialized)
365 PyThread_init_thread();
367 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
368 if (lock) {
369 memset((void *)lock, '\0', sizeof(pthread_lock));
370 lock->locked = 0;
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");
380 if (error) {
381 free((void *)lock);
382 lock = 0;
386 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
387 return (PyThread_type_lock) lock;
390 void
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);
407 int
408 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
410 int success;
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
424 * protocol */
425 while ( thelock->locked ) {
426 status = pthread_cond_wait(&thelock->lock_released,
427 &thelock->mut);
428 CHECK_STATUS("pthread_cond_wait");
430 success = 1;
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));
438 return success;
441 void
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]");
452 thelock->locked = 0;
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 */