| 1 |
|
| 2 |
|
| 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 |
|
| 16 |
|
| 17 |
#ifdef _POSIX_THREAD_ATTR_STACKSIZE |
| 18 |
#ifndef THREAD_STACK_SIZE |
| 19 |
#define THREAD_STACK_SIZE 0 |
| 20 |
#endif |
| 21 |
|
| 22 |
#if (defined(__APPLE__) || defined(__FreeBSD__)) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0 |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#undef THREAD_STACK_SIZE |
| 31 |
#define THREAD_STACK_SIZE 0x400000 |
| 32 |
#endif |
| 33 |
|
| 34 |
#define THREAD_STACK_MIN 0x8000 |
| 35 |
#else |
| 36 |
#ifdef THREAD_STACK_SIZE |
| 37 |
#error "THREAD_STACK_SIZE defined but _POSIX_THREAD_ATTR_STACKSIZE undefined" |
| 38 |
#endif |
| 39 |
#endif |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#ifdef _POSIX_SEMAPHORES |
| 45 |
|
| 46 |
|
| 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 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 77 |
|
| 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 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
typedef struct { |
| 114 |
char locked; |
| 115 |
|
| 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 |
|
| 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 |
|
| 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 |
| 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 |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
typedef struct { |
| 165 |
void (*func) (void *); |
| 166 |
void *arg; |
| 167 |
} pythread_callback; |
| 168 |
|
| 169 |
static void * |
| 170 |
pythread_wrapper(void *arg) |
| 171 |
{ |
| 172 |
|
| 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 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
long |
| 259 |
PyThread_get_thread_ident(void) |
| 260 |
{ |
| 261 |
volatile pthread_t threadid; |
| 262 |
if (!initialized) |
| 263 |
PyThread_init_thread(); |
| 264 |
|
| 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 |
|
| 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; |
| 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 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 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; |
| 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); |
| 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; |
| 380 |
dprintf(("PyThread_release_lock(%p) called\n", lock)); |
| 381 |
|
| 382 |
status = sem_post(thelock); |
| 383 |
CHECK_STATUS("sem_post"); |
| 384 |
} |
| 385 |
|
| 386 |
#else |
| 387 |
|
| 388 |
|
| 389 |
|
| 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; |
| 431 |
dprintf(("PyThread_free_lock(%p) called\n", lock)); |
| 432 |
|
| 433 |
|
| 434 |
|
| 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 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 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; |
| 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 |
|
| 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 |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 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 |
|
| 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 |
|
| 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) |