Debian Codegraph @

cpython27/Python/thread_pthread.h

raw symbols SCIP
1
2 /* Posix threads interface */
3
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>
14
15 /* The POSIX spec requires that use of pthread_attr_setstacksize
16 be conditional on _POSIX_THREAD_ATTR_STACKSIZE being defined. */
17 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
18 #ifndef THREAD_STACK_SIZE
19 #define THREAD_STACK_SIZE 0 /* use default stack size */
20 #endif
21
22 #if (defined(__APPLE__) || defined(__FreeBSD__)) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0
23 /* The default stack size for new threads on OSX is small enough that
24 * we'll get hard crashes instead of 'maximum recursion depth exceeded'
25 * exceptions.
26 *
27 * The default stack size below is the minimal stack size where a
28 * simple recursive function doesn't cause a hard crash.
29 */
30 #undef THREAD_STACK_SIZE
31 #define THREAD_STACK_SIZE 0x400000
32 #endif
33 /* for safety, ensure a viable minimum stacksize */
34 #define THREAD_STACK_MIN 0x8000 /* 32kB */
35 #else /* !_POSIX_THREAD_ATTR_STACKSIZE */
36 #ifdef THREAD_STACK_SIZE
37 #error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined"
38 #endif
39 #endif
40
41 /* The POSIX spec says that implementations supporting the sem_*
42 family of functions must indicate this by defining
43 _POSIX_SEMAPHORES. */
44 #ifdef _POSIX_SEMAPHORES
45 /* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
46 we need to add 0 to make it work there as well. */
47 #if (_POSIX_SEMAPHORES+0) == -1
48 #define HAVE_BROKEN_POSIX_SEMAPHORES
49 #else
50 #include <semaphore.h>
51 #include <errno.h>
52 #endif
53 #endif
54
55 /* Before FreeBSD 5.4, system scope threads was very limited resource
56 in default setting. So the process scope is preferred to get
57 enough number of threads to work. */
58 #ifdef __FreeBSD__
59 #include <osreldate.h>
60 #if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101
61 #undef PTHREAD_SYSTEM_SCHED_SUPPORTED
62 #endif
63 #endif
64
65 #if !defined(pthread_attr_default)
66 # define pthread_attr_default ((pthread_attr_t *)NULL)
67 #endif
68 #if !defined(pthread_mutexattr_default)
69 # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
70 #endif
71 #if !defined(pthread_condattr_default)
72 # define pthread_condattr_default ((pthread_condattr_t *)NULL)
73 #endif
74
75
76 /* Whether or not to use semaphores directly rather than emulating them with
77 * mutexes and condition variables:
78 */
79 #if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
80 # define USE_SEMAPHORES
81 #else
82 # undef USE_SEMAPHORES
83 #endif
84
85
86 /* On platforms that don't use standard POSIX threads pthread_sigmask()
87 * isn't present. DEC threads uses sigprocmask() instead as do most
88 * other UNIX International compliant systems that don't have the full
89 * pthread implementation.
90 */
91 #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
92 # define SET_THREAD_SIGMASK pthread_sigmask
93 #else
94 # define SET_THREAD_SIGMASK sigprocmask
95 #endif
96
97
98 /* A pthread mutex isn't sufficient to model the Python lock type
99 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
100 * following are undefined:
101 * -> a thread tries to lock a mutex it already has locked
102 * -> a thread tries to unlock a mutex locked by a different thread
103 * pthread mutexes are designed for serializing threads over short pieces
104 * of code anyway, so wouldn't be an appropriate implementation of
105 * Python's locks regardless.
106 *
107 * The pthread_lock struct implements a Python lock as a "locked?" bit
108 * and a <condition, mutex> pair. In general, if the bit can be acquired
109 * instantly, it is, else the pair is used to block the thread until the
110 * bit is cleared. 9 May 1994 [email protected]
111 */
112
113 typedef struct {
114 char locked; /* 0=unlocked, 1=locked */
115 /* a <cond, mutex> pair to handle an acquire of a locked lock */
116 pthread_cond_t lock_released;
117 pthread_mutex_t mut;
118 } pthread_lock;
119
120 #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
121
122 /*
123 * Initialization.
124 */
125
126 #ifdef _HAVE_BSDI
127 static
128 void _noop(void)
129 {
130 }
131
132 static void
133 PyThread__init_thread(void)
134 {
135 /* DO AN INIT BY STARTING THE THREAD */
136 static int dummy = 0;
137 pthread_t thread1;
138 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
139 pthread_join(thread1, NULL);
140 }
141
142 #else /* !_HAVE_BSDI */
143
144 static void
145 PyThread__init_thread(void)
146 {
147 #if defined(_AIX) && defined(__GNUC__)
148 extern void pthread_init(void);
149 pthread_init();
150 #endif
151 }
152
153 #endif /* !_HAVE_BSDI */
154
155 /*
156 * Thread support.
157 */
158
159 /* bpo-33015: pythread_callback struct and pythread_wrapper() cast
160 "void func(void *)" to "void* func(void *)": always return NULL.
161
162 PyThread_start_new_thread() uses "void func(void *)" type, whereas
163 pthread_create() requires a void* return value. */
164 typedef struct {
165 void (*func) (void *);
166 void *arg;
167 } pythread_callback;
168
169 static void *
170 pythread_wrapper(void *arg)
171 {
172 /* copy func and func_arg and free the temporary structure */
173 pythread_callback *callback = arg;
174 void (*func)(void *) = callback->func;
175 void *func_arg = callback->arg;
176 free(arg);
177
178 func(func_arg);
179 return NULL;
180 }
181
182 long
183 PyThread_start_new_thread(void (*func)(void *), void *arg)
184 {
185 pthread_t th;
186 int status;
187 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
188 pthread_attr_t attrs;
189 #endif
190 #if defined(THREAD_STACK_SIZE)
191 size_t tss;
192 #endif
193
194 dprintf(("PyThread_start_new_thread called\n"));
195 if (!initialized)
196 PyThread_init_thread();
197
198 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
199 if (pthread_attr_init(&attrs) != 0)
200 return -1;
201 #endif
202 #if defined(THREAD_STACK_SIZE)
203 tss = (_pythread_stacksize != 0) ? _pythread_stacksize
204 : THREAD_STACK_SIZE;
205 if (tss != 0) {
206 if (pthread_attr_setstacksize(&attrs, tss) != 0) {
207 pthread_attr_destroy(&attrs);
208 return -1;
209 }
210 }
211 #endif
212 #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
213 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
214 #endif
215
216 pythread_callback *callback = malloc(sizeof(pythread_callback));
217
218 if (callback == NULL) {
219 return -1;
220 }
221
222 callback->func = func;
223 callback->arg = arg;
224
225 status = pthread_create(&th,
226 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
227 &attrs,
228 #else
229 (pthread_attr_t*)NULL,
230 #endif
231 pythread_wrapper, callback);
232
233 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
234 pthread_attr_destroy(&attrs);
235 #endif
236
237 if (status != 0) {
238 free(callback);
239 return -1;
240 }
241
242 pthread_detach(th);
243
244 #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
245 return (long) th;
246 #else
247 return (long) *(long *) &th;
248 #endif
249 }
250
251 /* XXX This implementation is considered (to quote Tim Peters) "inherently
252 hosed" because:
253 - It does not guarantee the promise that a non-zero integer is returned.
254 - The cast to long is inherently unsafe.
255 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
256 latter return statement (for Alpha OSF/1) are any longer necessary.
257 */
258 long
259 PyThread_get_thread_ident(void)
260 {
261 volatile pthread_t threadid;
262 if (!initialized)
263 PyThread_init_thread();
264 /* Jump through some hoops for Alpha OSF/1 */
265 threadid = pthread_self();
266 #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
267 return (long) threadid;
268 #else
269 return (long) *(long *) &threadid;
270 #endif
271 }
272
273 void
274 PyThread_exit_thread(void)
275 {
276 dprintf(("PyThread_exit_thread called\n"));
277 if (!initialized) {
278 exit(0);
279 }
280 }
281
282 #ifdef USE_SEMAPHORES
283
284 /*
285 * Lock support.
286 */
287
288 PyThread_type_lock
289 PyThread_allocate_lock(void)
290 {
291 sem_t *lock;
292 int status, error = 0;
293
294 dprintf(("PyThread_allocate_lock called\n"));
295 if (!initialized)
296 PyThread_init_thread();
297
298 lock = (sem_t *)malloc(sizeof(sem_t));
299
300 if (lock) {
301 status = sem_init(lock,0,1);
302 CHECK_STATUS("sem_init");
303
304 if (error) {
305 free((void *)lock);
306 lock = NULL;
307 }
308 }
309
310 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
311 return (PyThread_type_lock)lock;
312 }
313
314 void
315 PyThread_free_lock(PyThread_type_lock lock)
316 {
317 sem_t *thelock = (sem_t *)lock;
318 int status, error = 0;
319
320 (void) error; /* silence unused-but-set-variable warning */
321 dprintf(("PyThread_free_lock(%p) called\n", lock));
322
323 if (!thelock)
324 return;
325
326 status = sem_destroy(thelock);
327 CHECK_STATUS("sem_destroy");
328
329 free((void *)thelock);
330 }
331
332 /*
333 * As of February 2002, Cygwin thread implementations mistakenly report error
334 * codes in the return value of the sem_ calls (like the pthread_ functions).
335 * Correct implementations return -1 and put the code in errno. This supports
336 * either.
337 */
338 static int
339 fix_status(int status)
340 {
341 return (status == -1) ? errno : status;
342 }
343
344 int
345 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
346 {
347 int success;
348 sem_t *thelock = (sem_t *)lock;
349 int status, error = 0;
350
351 (void) error; /* silence unused-but-set-variable warning */
352 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
353
354 do {
355 if (waitflag)
356 status = fix_status(sem_wait(thelock));
357 else
358 status = fix_status(sem_trywait(thelock));
359 } while (status == EINTR); /* Retry if interrupted by a signal */
360
361 if (waitflag) {
362 CHECK_STATUS("sem_wait");
363 } else if (status != EAGAIN) {
364 CHECK_STATUS("sem_trywait");
365 }
366
367 success = (status == 0) ? 1 : 0;
368
369 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
370 return success;
371 }
372
373 void
374 PyThread_release_lock(PyThread_type_lock lock)
375 {
376 sem_t *thelock = (sem_t *)lock;
377 int status, error = 0;
378
379 (void) error; /* silence unused-but-set-variable warning */
380 dprintf(("PyThread_release_lock(%p) called\n", lock));
381
382 status = sem_post(thelock);
383 CHECK_STATUS("sem_post");
384 }
385
386 #else /* USE_SEMAPHORES */
387
388 /*
389 * Lock support.
390 */
391 PyThread_type_lock
392 PyThread_allocate_lock(void)
393 {
394 pthread_lock *lock;
395 int status, error = 0;
396
397 dprintf(("PyThread_allocate_lock called\n"));
398 if (!initialized)
399 PyThread_init_thread();
400
401 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
402 if (lock) {
403 memset((void *)lock, '\0', sizeof(pthread_lock));
404 lock->locked = 0;
405
406 status = pthread_mutex_init(&lock->mut,
407 pthread_mutexattr_default);
408 CHECK_STATUS("pthread_mutex_init");
409
410 status = pthread_cond_init(&lock->lock_released,
411 pthread_condattr_default);
412 CHECK_STATUS("pthread_cond_init");
413
414 if (error) {
415 free((void *)lock);
416 lock = 0;
417 }
418 }
419
420 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
421 return (PyThread_type_lock) lock;
422 }
423
424 void
425 PyThread_free_lock(PyThread_type_lock lock)
426 {
427 pthread_lock *thelock = (pthread_lock *)lock;
428 int status, error = 0;
429
430 (void) error; /* silence unused-but-set-variable warning */
431 dprintf(("PyThread_free_lock(%p) called\n", lock));
432
433 /* some pthread-like implementations tie the mutex to the cond
434 * and must have the cond destroyed first.
435 */
436 status = pthread_cond_destroy( &thelock->lock_released );
437 CHECK_STATUS("pthread_cond_destroy");
438
439 status = pthread_mutex_destroy( &thelock->mut );
440 CHECK_STATUS("pthread_mutex_destroy");
441
442 free((void *)thelock);
443 }
444
445 int
446 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
447 {
448 int success = 0;
449 pthread_lock *thelock = (pthread_lock *)lock;
450 int status, error = 0;
451
452 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
453
454 if (waitflag) {
455 status = pthread_mutex_lock( &thelock->mut );
456 CHECK_STATUS("pthread_mutex_lock[1]");
457 }
458 else {
459 status = pthread_mutex_trylock( &thelock->mut );
460 if (status != EBUSY)
461 CHECK_STATUS("pthread_mutex_trylock[1]");
462 }
463 if (status == 0) {
464 success = thelock->locked == 0;
465
466 if ( !success && waitflag ) {
467 /* continue trying until we get the lock */
468
469 /* mut must be locked by me -- part of the condition
470 * protocol */
471 while ( thelock->locked ) {
472 status = pthread_cond_wait(&thelock->lock_released,
473 &thelock->mut);
474 CHECK_STATUS("pthread_cond_wait");
475 }
476 success = 1;
477 }
478
479 if (success) thelock->locked = 1;
480 status = pthread_mutex_unlock( &thelock->mut );
481 CHECK_STATUS("pthread_mutex_unlock[1]");
482 }
483
484 if (error) success = 0;
485 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
486 return success;
487 }
488
489 void
490 PyThread_release_lock(PyThread_type_lock lock)
491 {
492 pthread_lock *thelock = (pthread_lock *)lock;
493 int status, error = 0;
494
495 (void) error; /* silence unused-but-set-variable warning */
496 dprintf(("PyThread_release_lock(%p) called\n", lock));
497
498 status = pthread_mutex_lock( &thelock->mut );
499 CHECK_STATUS("pthread_mutex_lock[3]");
500
501 thelock->locked = 0;
502
503 /* wake up someone (anyone, if any) waiting on the lock */
504 status = pthread_cond_signal( &thelock->lock_released );
505 CHECK_STATUS("pthread_cond_signal");
506
507 status = pthread_mutex_unlock( &thelock->mut );
508 CHECK_STATUS("pthread_mutex_unlock[3]");
509 }
510
511 #endif /* USE_SEMAPHORES */
512
513 /* set the thread stack size.
514 * Return 0 if size is valid, -1 if size is invalid,
515 * -2 if setting stack size is not supported.
516 */
517 static int
518 _pythread_pthread_set_stacksize(size_t size)
519 {
520 #if defined(THREAD_STACK_SIZE)
521 pthread_attr_t attrs;
522 size_t tss_min;
523 int rc = 0;
524 #endif
525
526 /* set to default */
527 if (size == 0) {
528 _pythread_stacksize = 0;
529 return 0;
530 }
531
532 #if defined(THREAD_STACK_SIZE)
533 #if defined(PTHREAD_STACK_MIN)
534 tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
535 : THREAD_STACK_MIN;
536 #else
537 tss_min = THREAD_STACK_MIN;
538 #endif
539 if (size >= tss_min) {
540 /* validate stack size by setting thread attribute */
541 if (pthread_attr_init(&attrs) == 0) {
542 rc = pthread_attr_setstacksize(&attrs, size);
543 pthread_attr_destroy(&attrs);
544 if (rc == 0) {
545 _pythread_stacksize = size;
546 return 0;
547 }
548 }
549 }
550 return -1;
551 #else
552 return -2;
553 #endif
554 }
555
556 #define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)