148
148
PyThread_start_new_thread(void (*func)(void *), void *arg)
152
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
153
pthread_attr_t attrs;
155
#if defined(THREAD_STACK_SIZE)
159
dprintf(("PyThread_start_new_thread called\n"));
161
PyThread_init_thread();
163
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
164
if (pthread_attr_init(&attrs) != 0)
167
#if defined(THREAD_STACK_SIZE)
168
tss = (_pythread_stacksize != 0) ? _pythread_stacksize
171
if (pthread_attr_setstacksize(&attrs, tss) != 0) {
172
pthread_attr_destroy(&attrs);
152
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
153
pthread_attr_t attrs;
155
#if defined(THREAD_STACK_SIZE)
159
dprintf(("PyThread_start_new_thread called\n"));
161
PyThread_init_thread();
163
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
164
if (pthread_attr_init(&attrs) != 0)
167
#if defined(THREAD_STACK_SIZE)
168
tss = (_pythread_stacksize != 0) ? _pythread_stacksize
171
if (pthread_attr_setstacksize(&attrs, tss) != 0) {
172
pthread_attr_destroy(&attrs);
177
177
#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
178
pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
178
pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
181
status = pthread_create(&th,
181
status = pthread_create(&th,
182
182
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
185
(pthread_attr_t*)NULL,
185
(pthread_attr_t*)NULL,
187
(void* (*)(void *))func,
187
(void* (*)(void *))func,
191
191
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
192
pthread_attr_destroy(&attrs);
192
pthread_attr_destroy(&attrs);
199
199
#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
202
return (long) *(long *) &th;
202
return (long) *(long *) &th;
210
210
- It is not clear that the 'volatile' (for AIX?) and ugly casting in the
211
211
latter return statement (for Alpha OSF/1) are any longer necessary.
214
214
PyThread_get_thread_ident(void)
216
volatile pthread_t threadid;
218
PyThread_init_thread();
219
/* Jump through some hoops for Alpha OSF/1 */
220
threadid = pthread_self();
216
volatile pthread_t threadid;
218
PyThread_init_thread();
219
/* Jump through some hoops for Alpha OSF/1 */
220
threadid = pthread_self();
221
221
#if SIZEOF_PTHREAD_T <= SIZEOF_LONG
222
return (long) threadid;
222
return (long) threadid;
224
return (long) *(long *) &threadid;
224
return (long) *(long *) &threadid;
229
229
do_PyThread_exit_thread(int no_cleanup)
231
dprintf(("PyThread_exit_thread called\n"));
231
dprintf(("PyThread_exit_thread called\n"));
241
241
PyThread_exit_thread(void)
243
do_PyThread_exit_thread(0);
243
do_PyThread_exit_thread(0);
247
247
PyThread__exit_thread(void)
249
do_PyThread_exit_thread(1);
249
do_PyThread_exit_thread(1);
252
252
#ifndef NO_EXIT_PROG
254
254
do_PyThread_exit_prog(int status, int no_cleanup)
256
dprintf(("PyThread_exit_prog(%d) called\n", status));
256
dprintf(("PyThread_exit_prog(%d) called\n", status));
265
265
PyThread_exit_prog(int status)
267
do_PyThread_exit_prog(status, 0);
267
do_PyThread_exit_prog(status, 0);
271
271
PyThread__exit_prog(int status)
273
do_PyThread_exit_prog(status, 1);
273
do_PyThread_exit_prog(status, 1);
275
275
#endif /* NO_EXIT_PROG */
284
284
PyThread_allocate_lock(void)
287
int status, error = 0;
289
dprintf(("PyThread_allocate_lock called\n"));
291
PyThread_init_thread();
293
lock = (sem_t *)malloc(sizeof(sem_t));
296
status = sem_init(lock,0,1);
297
CHECK_STATUS("sem_init");
305
dprintf(("PyThread_allocate_lock() -> %p\n", lock));
306
return (PyThread_type_lock)lock;
287
int status, error = 0;
289
dprintf(("PyThread_allocate_lock called\n"));
291
PyThread_init_thread();
293
lock = (sem_t *)malloc(sizeof(sem_t));
296
status = sem_init(lock,0,1);
297
CHECK_STATUS("sem_init");
305
dprintf(("PyThread_allocate_lock() -> %p\n", lock));
306
return (PyThread_type_lock)lock;
310
310
PyThread_free_lock(PyThread_type_lock lock)
312
sem_t *thelock = (sem_t *)lock;
313
int status, error = 0;
315
dprintf(("PyThread_free_lock(%p) called\n", lock));
320
status = sem_destroy(thelock);
321
CHECK_STATUS("sem_destroy");
323
free((void *)thelock);
312
sem_t *thelock = (sem_t *)lock;
313
int status, error = 0;
315
dprintf(("PyThread_free_lock(%p) called\n", lock));
320
status = sem_destroy(thelock);
321
CHECK_STATUS("sem_destroy");
323
free((void *)thelock);
333
333
fix_status(int status)
335
return (status == -1) ? errno : status;
335
return (status == -1) ? errno : status;
339
339
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
342
sem_t *thelock = (sem_t *)lock;
343
int status, error = 0;
345
dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
349
status = fix_status(sem_wait(thelock));
351
status = fix_status(sem_trywait(thelock));
352
} while (status == EINTR); /* Retry if interrupted by a signal */
355
CHECK_STATUS("sem_wait");
356
} else if (status != EAGAIN) {
357
CHECK_STATUS("sem_trywait");
360
success = (status == 0) ? 1 : 0;
362
dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
342
sem_t *thelock = (sem_t *)lock;
343
int status, error = 0;
345
dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
349
status = fix_status(sem_wait(thelock));
351
status = fix_status(sem_trywait(thelock));
352
} while (status == EINTR); /* Retry if interrupted by a signal */
355
CHECK_STATUS("sem_wait");
356
} else if (status != EAGAIN) {
357
CHECK_STATUS("sem_trywait");
360
success = (status == 0) ? 1 : 0;
362
dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
367
367
PyThread_release_lock(PyThread_type_lock lock)
369
sem_t *thelock = (sem_t *)lock;
370
int status, error = 0;
372
dprintf(("PyThread_release_lock(%p) called\n", lock));
374
status = sem_post(thelock);
375
CHECK_STATUS("sem_post");
369
sem_t *thelock = (sem_t *)lock;
370
int status, error = 0;
372
dprintf(("PyThread_release_lock(%p) called\n", lock));
374
status = sem_post(thelock);
375
CHECK_STATUS("sem_post");
378
378
#else /* USE_SEMAPHORES */
384
384
PyThread_allocate_lock(void)
387
int status, error = 0;
389
dprintf(("PyThread_allocate_lock called\n"));
391
PyThread_init_thread();
393
lock = (pthread_lock *) malloc(sizeof(pthread_lock));
395
memset((void *)lock, '\0', sizeof(pthread_lock));
398
status = pthread_mutex_init(&lock->mut,
399
pthread_mutexattr_default);
400
CHECK_STATUS("pthread_mutex_init");
402
status = pthread_cond_init(&lock->lock_released,
403
pthread_condattr_default);
404
CHECK_STATUS("pthread_cond_init");
412
dprintf(("PyThread_allocate_lock() -> %p\n", lock));
413
return (PyThread_type_lock) lock;
387
int status, error = 0;
389
dprintf(("PyThread_allocate_lock called\n"));
391
PyThread_init_thread();
393
lock = (pthread_lock *) malloc(sizeof(pthread_lock));
395
memset((void *)lock, '\0', sizeof(pthread_lock));
398
status = pthread_mutex_init(&lock->mut,
399
pthread_mutexattr_default);
400
CHECK_STATUS("pthread_mutex_init");
402
status = pthread_cond_init(&lock->lock_released,
403
pthread_condattr_default);
404
CHECK_STATUS("pthread_cond_init");
412
dprintf(("PyThread_allocate_lock() -> %p\n", lock));
413
return (PyThread_type_lock) lock;
417
417
PyThread_free_lock(PyThread_type_lock lock)
419
pthread_lock *thelock = (pthread_lock *)lock;
420
int status, error = 0;
422
dprintf(("PyThread_free_lock(%p) called\n", lock));
424
status = pthread_mutex_destroy( &thelock->mut );
425
CHECK_STATUS("pthread_mutex_destroy");
427
status = pthread_cond_destroy( &thelock->lock_released );
428
CHECK_STATUS("pthread_cond_destroy");
430
free((void *)thelock);
419
pthread_lock *thelock = (pthread_lock *)lock;
420
int status, error = 0;
422
dprintf(("PyThread_free_lock(%p) called\n", lock));
424
status = pthread_mutex_destroy( &thelock->mut );
425
CHECK_STATUS("pthread_mutex_destroy");
427
status = pthread_cond_destroy( &thelock->lock_released );
428
CHECK_STATUS("pthread_cond_destroy");
430
free((void *)thelock);
434
434
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
437
pthread_lock *thelock = (pthread_lock *)lock;
438
int status, error = 0;
440
dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
442
status = pthread_mutex_lock( &thelock->mut );
443
CHECK_STATUS("pthread_mutex_lock[1]");
444
success = thelock->locked == 0;
446
if ( !success && waitflag ) {
447
/* continue trying until we get the lock */
449
/* mut must be locked by me -- part of the condition
451
while ( thelock->locked ) {
452
status = pthread_cond_wait(&thelock->lock_released,
454
CHECK_STATUS("pthread_cond_wait");
458
if (success) thelock->locked = 1;
459
status = pthread_mutex_unlock( &thelock->mut );
460
CHECK_STATUS("pthread_mutex_unlock[1]");
462
if (error) success = 0;
463
dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
437
pthread_lock *thelock = (pthread_lock *)lock;
438
int status, error = 0;
440
dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
442
status = pthread_mutex_lock( &thelock->mut );
443
CHECK_STATUS("pthread_mutex_lock[1]");
444
success = thelock->locked == 0;
446
if ( !success && waitflag ) {
447
/* continue trying until we get the lock */
449
/* mut must be locked by me -- part of the condition
451
while ( thelock->locked ) {
452
status = pthread_cond_wait(&thelock->lock_released,
454
CHECK_STATUS("pthread_cond_wait");
458
if (success) thelock->locked = 1;
459
status = pthread_mutex_unlock( &thelock->mut );
460
CHECK_STATUS("pthread_mutex_unlock[1]");
462
if (error) success = 0;
463
dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
468
468
PyThread_release_lock(PyThread_type_lock lock)
470
pthread_lock *thelock = (pthread_lock *)lock;
471
int status, error = 0;
473
dprintf(("PyThread_release_lock(%p) called\n", lock));
475
status = pthread_mutex_lock( &thelock->mut );
476
CHECK_STATUS("pthread_mutex_lock[3]");
480
status = pthread_mutex_unlock( &thelock->mut );
481
CHECK_STATUS("pthread_mutex_unlock[3]");
483
/* wake up someone (anyone, if any) waiting on the lock */
484
status = pthread_cond_signal( &thelock->lock_released );
485
CHECK_STATUS("pthread_cond_signal");
470
pthread_lock *thelock = (pthread_lock *)lock;
471
int status, error = 0;
473
dprintf(("PyThread_release_lock(%p) called\n", lock));
475
status = pthread_mutex_lock( &thelock->mut );
476
CHECK_STATUS("pthread_mutex_lock[3]");
480
status = pthread_mutex_unlock( &thelock->mut );
481
CHECK_STATUS("pthread_mutex_unlock[3]");
483
/* wake up someone (anyone, if any) waiting on the lock */
484
status = pthread_cond_signal( &thelock->lock_released );
485
CHECK_STATUS("pthread_cond_signal");
488
488
#endif /* USE_SEMAPHORES */
495
495
_pythread_pthread_set_stacksize(size_t size)
497
497
#if defined(THREAD_STACK_SIZE)
498
pthread_attr_t attrs;
498
pthread_attr_t attrs;
505
_pythread_stacksize = 0;
505
_pythread_stacksize = 0;
509
509
#if defined(THREAD_STACK_SIZE)
510
510
#if defined(PTHREAD_STACK_MIN)
511
tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
511
tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
514
tss_min = THREAD_STACK_MIN;
514
tss_min = THREAD_STACK_MIN;
516
if (size >= tss_min) {
517
/* validate stack size by setting thread attribute */
518
if (pthread_attr_init(&attrs) == 0) {
519
rc = pthread_attr_setstacksize(&attrs, size);
520
pthread_attr_destroy(&attrs);
522
_pythread_stacksize = size;
516
if (size >= tss_min) {
517
/* validate stack size by setting thread attribute */
518
if (pthread_attr_init(&attrs) == 0) {
519
rc = pthread_attr_setstacksize(&attrs, size);
520
pthread_attr_destroy(&attrs);
522
_pythread_stacksize = size;
533
#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
533
#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)