Fix argument order in pure python version of nsmallest() and nlargest().
[python/dscho.git] / Python / thread_pthread.h
blobdd1616c71642e9f74d1329d042256affb411ec64
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 #include <semaphore.h>
20 #include <errno.h>
21 #endif
23 #if !defined(pthread_attr_default)
24 # define pthread_attr_default ((pthread_attr_t *)NULL)
25 #endif
26 #if !defined(pthread_mutexattr_default)
27 # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
28 #endif
29 #if !defined(pthread_condattr_default)
30 # define pthread_condattr_default ((pthread_condattr_t *)NULL)
31 #endif
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
39 #else
40 # undef USE_SEMAPHORES
41 #endif
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
51 #else
52 # define SET_THREAD_SIGMASK sigprocmask
53 #endif
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
71 typedef struct {
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;
75 pthread_mutex_t mut;
76 } pthread_lock;
78 #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
81 * Initialization.
84 #ifdef _HAVE_BSDI
85 static
86 void _noop(void)
90 static void
91 PyThread__init_thread(void)
93 /* DO AN INIT BY STARTING THE THREAD */
94 static int dummy = 0;
95 pthread_t thread1;
96 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
97 pthread_join(thread1, NULL);
100 #else /* !_HAVE_BSDI */
102 static void
103 PyThread__init_thread(void)
105 #if defined(_AIX) && defined(__GNUC__)
106 pthread_init();
107 #endif
110 #endif /* !_HAVE_BSDI */
113 * Thread support.
117 long
118 PyThread_start_new_thread(void (*func)(void *), void *arg)
120 pthread_t th;
121 int status;
122 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
123 pthread_attr_t attrs;
124 #endif
125 dprintf(("PyThread_start_new_thread called\n"));
126 if (!initialized)
127 PyThread_init_thread();
129 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
130 pthread_attr_init(&attrs);
131 #endif
132 #ifdef THREAD_STACK_SIZE
133 pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
134 #endif
135 #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) && !defined(__FreeBSD__)
136 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
137 #endif
139 status = pthread_create(&th,
140 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
141 &attrs,
142 #else
143 (pthread_attr_t*)NULL,
144 #endif
145 (void* (*)(void *))func,
146 (void *)arg
149 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
150 pthread_attr_destroy(&attrs);
151 #endif
152 if (status != 0)
153 return -1;
155 pthread_detach(th);
157 #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
158 return (long) th;
159 #else
160 return (long) *(long *) &th;
161 #endif
164 /* XXX This implementation is considered (to quote Tim Peters) "inherently
165 hosed" because:
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.
171 long
172 PyThread_get_thread_ident(void)
174 volatile pthread_t threadid;
175 if (!initialized)
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;
181 #else
182 return (long) *(long *) &threadid;
183 #endif
186 static void
187 do_PyThread_exit_thread(int no_cleanup)
189 dprintf(("PyThread_exit_thread called\n"));
190 if (!initialized) {
191 if (no_cleanup)
192 _exit(0);
193 else
194 exit(0);
198 void
199 PyThread_exit_thread(void)
201 do_PyThread_exit_thread(0);
204 void
205 PyThread__exit_thread(void)
207 do_PyThread_exit_thread(1);
210 #ifndef NO_EXIT_PROG
211 static void
212 do_PyThread_exit_prog(int status, int no_cleanup)
214 dprintf(("PyThread_exit_prog(%d) called\n", status));
215 if (!initialized)
216 if (no_cleanup)
217 _exit(status);
218 else
219 exit(status);
222 void
223 PyThread_exit_prog(int status)
225 do_PyThread_exit_prog(status, 0);
228 void
229 PyThread__exit_prog(int status)
231 do_PyThread_exit_prog(status, 1);
233 #endif /* NO_EXIT_PROG */
235 #ifdef USE_SEMAPHORES
238 * Lock support.
241 PyThread_type_lock
242 PyThread_allocate_lock(void)
244 sem_t *lock;
245 int status, error = 0;
247 dprintf(("PyThread_allocate_lock called\n"));
248 if (!initialized)
249 PyThread_init_thread();
251 lock = (sem_t *)malloc(sizeof(sem_t));
253 if (lock) {
254 status = sem_init(lock,0,1);
255 CHECK_STATUS("sem_init");
257 if (error) {
258 free((void *)lock);
259 lock = NULL;
263 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
264 return (PyThread_type_lock)lock;
267 void
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));
275 if (!thelock)
276 return;
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
288 * either.
290 static int
291 fix_status(int status)
293 return (status == -1) ? errno : status;
296 int
297 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
299 int success;
300 sem_t *thelock = (sem_t *)lock;
301 int status, error = 0;
303 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
305 do {
306 if (waitflag)
307 status = fix_status(sem_wait(thelock));
308 else
309 status = fix_status(sem_trywait(thelock));
310 } while (status == EINTR); /* Retry if interrupted by a signal */
312 if (waitflag) {
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));
321 return success;
324 void
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 */
339 * Lock support.
341 PyThread_type_lock
342 PyThread_allocate_lock(void)
344 pthread_lock *lock;
345 int status, error = 0;
347 dprintf(("PyThread_allocate_lock called\n"));
348 if (!initialized)
349 PyThread_init_thread();
351 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
352 memset((void *)lock, '\0', sizeof(pthread_lock));
353 if (lock) {
354 lock->locked = 0;
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");
364 if (error) {
365 free((void *)lock);
366 lock = 0;
370 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
371 return (PyThread_type_lock) lock;
374 void
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);
391 int
392 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
394 int success;
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
408 * protocol */
409 while ( thelock->locked ) {
410 status = pthread_cond_wait(&thelock->lock_released,
411 &thelock->mut);
412 CHECK_STATUS("pthread_cond_wait");
414 success = 1;
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));
422 return success;
425 void
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]");
436 thelock->locked = 0;
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 */