[2] | 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
|
---|
[391] | 19 | #define THREAD_STACK_SIZE 0 /* use default stack size */
|
---|
[2] | 20 | #endif
|
---|
[391] | 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
|
---|
[2] | 33 | /* for safety, ensure a viable minimum stacksize */
|
---|
[391] | 34 | #define THREAD_STACK_MIN 0x8000 /* 32kB */
|
---|
[2] | 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
|
---|
[391] | 43 | _POSIX_SEMAPHORES. */
|
---|
[2] | 44 | #ifdef _POSIX_SEMAPHORES
|
---|
[391] | 45 | /* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
|
---|
[2] | 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 tim@ksr.com
|
---|
| 111 | */
|
---|
| 112 |
|
---|
| 113 | typedef struct {
|
---|
[391] | 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;
|
---|
[2] | 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 | {
|
---|
[391] | 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);
|
---|
[2] | 140 | }
|
---|
| 141 |
|
---|
| 142 | #else /* !_HAVE_BSDI */
|
---|
| 143 |
|
---|
| 144 | static void
|
---|
| 145 | PyThread__init_thread(void)
|
---|
| 146 | {
|
---|
| 147 | #if defined(_AIX) && defined(__GNUC__)
|
---|
[391] | 148 | extern void pthread_init(void);
|
---|
| 149 | pthread_init();
|
---|
[2] | 150 | #endif
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | #endif /* !_HAVE_BSDI */
|
---|
| 154 |
|
---|
| 155 | /*
|
---|
| 156 | * Thread support.
|
---|
| 157 | */
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 | long
|
---|
| 161 | PyThread_start_new_thread(void (*func)(void *), void *arg)
|
---|
| 162 | {
|
---|
[391] | 163 | pthread_t th;
|
---|
| 164 | int status;
|
---|
[2] | 165 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
|
---|
[391] | 166 | pthread_attr_t attrs;
|
---|
[2] | 167 | #endif
|
---|
| 168 | #if defined(THREAD_STACK_SIZE)
|
---|
[391] | 169 | size_t tss;
|
---|
[2] | 170 | #endif
|
---|
| 171 |
|
---|
[391] | 172 | dprintf(("PyThread_start_new_thread called\n"));
|
---|
| 173 | if (!initialized)
|
---|
| 174 | PyThread_init_thread();
|
---|
[2] | 175 |
|
---|
| 176 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
|
---|
[391] | 177 | if (pthread_attr_init(&attrs) != 0)
|
---|
| 178 | return -1;
|
---|
[2] | 179 | #endif
|
---|
| 180 | #if defined(THREAD_STACK_SIZE)
|
---|
[391] | 181 | tss = (_pythread_stacksize != 0) ? _pythread_stacksize
|
---|
| 182 | : THREAD_STACK_SIZE;
|
---|
| 183 | if (tss != 0) {
|
---|
| 184 | if (pthread_attr_setstacksize(&attrs, tss) != 0) {
|
---|
| 185 | pthread_attr_destroy(&attrs);
|
---|
| 186 | return -1;
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
[2] | 189 | #endif
|
---|
| 190 | #if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
|
---|
[391] | 191 | pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
|
---|
[2] | 192 | #endif
|
---|
| 193 |
|
---|
[391] | 194 | status = pthread_create(&th,
|
---|
[2] | 195 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
|
---|
[391] | 196 | &attrs,
|
---|
[2] | 197 | #else
|
---|
[391] | 198 | (pthread_attr_t*)NULL,
|
---|
[2] | 199 | #endif
|
---|
[391] | 200 | (void* (*)(void *))func,
|
---|
| 201 | (void *)arg
|
---|
| 202 | );
|
---|
[2] | 203 |
|
---|
| 204 | #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
|
---|
[391] | 205 | pthread_attr_destroy(&attrs);
|
---|
[2] | 206 | #endif
|
---|
[391] | 207 | if (status != 0)
|
---|
| 208 | return -1;
|
---|
[2] | 209 |
|
---|
[391] | 210 | pthread_detach(th);
|
---|
[2] | 211 |
|
---|
| 212 | #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
|
---|
[391] | 213 | return (long) th;
|
---|
[2] | 214 | #else
|
---|
[391] | 215 | return (long) *(long *) &th;
|
---|
[2] | 216 | #endif
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /* XXX This implementation is considered (to quote Tim Peters) "inherently
|
---|
| 220 | hosed" because:
|
---|
| 221 | - It does not guarantee the promise that a non-zero integer is returned.
|
---|
| 222 | - The cast to long is inherently unsafe.
|
---|
| 223 | - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
|
---|
| 224 | latter return statement (for Alpha OSF/1) are any longer necessary.
|
---|
| 225 | */
|
---|
[391] | 226 | long
|
---|
[2] | 227 | PyThread_get_thread_ident(void)
|
---|
| 228 | {
|
---|
[391] | 229 | volatile pthread_t threadid;
|
---|
| 230 | if (!initialized)
|
---|
| 231 | PyThread_init_thread();
|
---|
| 232 | /* Jump through some hoops for Alpha OSF/1 */
|
---|
| 233 | threadid = pthread_self();
|
---|
[2] | 234 | #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
|
---|
[391] | 235 | return (long) threadid;
|
---|
[2] | 236 | #else
|
---|
[391] | 237 | return (long) *(long *) &threadid;
|
---|
[2] | 238 | #endif
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[391] | 241 | void
|
---|
[2] | 242 | PyThread_exit_thread(void)
|
---|
| 243 | {
|
---|
[391] | 244 | dprintf(("PyThread_exit_thread called\n"));
|
---|
| 245 | if (!initialized) {
|
---|
| 246 | exit(0);
|
---|
| 247 | }
|
---|
[2] | 248 | }
|
---|
| 249 |
|
---|
| 250 | #ifdef USE_SEMAPHORES
|
---|
| 251 |
|
---|
| 252 | /*
|
---|
| 253 | * Lock support.
|
---|
| 254 | */
|
---|
| 255 |
|
---|
[391] | 256 | PyThread_type_lock
|
---|
[2] | 257 | PyThread_allocate_lock(void)
|
---|
| 258 | {
|
---|
[391] | 259 | sem_t *lock;
|
---|
| 260 | int status, error = 0;
|
---|
[2] | 261 |
|
---|
[391] | 262 | dprintf(("PyThread_allocate_lock called\n"));
|
---|
| 263 | if (!initialized)
|
---|
| 264 | PyThread_init_thread();
|
---|
[2] | 265 |
|
---|
[391] | 266 | lock = (sem_t *)malloc(sizeof(sem_t));
|
---|
[2] | 267 |
|
---|
[391] | 268 | if (lock) {
|
---|
| 269 | status = sem_init(lock,0,1);
|
---|
| 270 | CHECK_STATUS("sem_init");
|
---|
[2] | 271 |
|
---|
[391] | 272 | if (error) {
|
---|
| 273 | free((void *)lock);
|
---|
| 274 | lock = NULL;
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
[2] | 277 |
|
---|
[391] | 278 | dprintf(("PyThread_allocate_lock() -> %p\n", lock));
|
---|
| 279 | return (PyThread_type_lock)lock;
|
---|
[2] | 280 | }
|
---|
| 281 |
|
---|
[391] | 282 | void
|
---|
[2] | 283 | PyThread_free_lock(PyThread_type_lock lock)
|
---|
| 284 | {
|
---|
[391] | 285 | sem_t *thelock = (sem_t *)lock;
|
---|
| 286 | int status, error = 0;
|
---|
[2] | 287 |
|
---|
[391] | 288 | (void) error; /* silence unused-but-set-variable warning */
|
---|
| 289 | dprintf(("PyThread_free_lock(%p) called\n", lock));
|
---|
[2] | 290 |
|
---|
[391] | 291 | if (!thelock)
|
---|
| 292 | return;
|
---|
[2] | 293 |
|
---|
[391] | 294 | status = sem_destroy(thelock);
|
---|
| 295 | CHECK_STATUS("sem_destroy");
|
---|
[2] | 296 |
|
---|
[391] | 297 | free((void *)thelock);
|
---|
[2] | 298 | }
|
---|
| 299 |
|
---|
| 300 | /*
|
---|
| 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.
|
---|
| 305 | */
|
---|
| 306 | static int
|
---|
| 307 | fix_status(int status)
|
---|
| 308 | {
|
---|
[391] | 309 | return (status == -1) ? errno : status;
|
---|
[2] | 310 | }
|
---|
| 311 |
|
---|
[391] | 312 | int
|
---|
[2] | 313 | PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
|
---|
| 314 | {
|
---|
[391] | 315 | int success;
|
---|
| 316 | sem_t *thelock = (sem_t *)lock;
|
---|
| 317 | int status, error = 0;
|
---|
[2] | 318 |
|
---|
[391] | 319 | (void) error; /* silence unused-but-set-variable warning */
|
---|
| 320 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
|
---|
[2] | 321 |
|
---|
[391] | 322 | do {
|
---|
| 323 | if (waitflag)
|
---|
| 324 | status = fix_status(sem_wait(thelock));
|
---|
| 325 | else
|
---|
| 326 | status = fix_status(sem_trywait(thelock));
|
---|
| 327 | } while (status == EINTR); /* Retry if interrupted by a signal */
|
---|
[2] | 328 |
|
---|
[391] | 329 | if (waitflag) {
|
---|
| 330 | CHECK_STATUS("sem_wait");
|
---|
| 331 | } else if (status != EAGAIN) {
|
---|
| 332 | CHECK_STATUS("sem_trywait");
|
---|
| 333 | }
|
---|
[2] | 334 |
|
---|
[391] | 335 | success = (status == 0) ? 1 : 0;
|
---|
| 336 |
|
---|
| 337 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
|
---|
| 338 | return success;
|
---|
[2] | 339 | }
|
---|
| 340 |
|
---|
[391] | 341 | void
|
---|
[2] | 342 | PyThread_release_lock(PyThread_type_lock lock)
|
---|
| 343 | {
|
---|
[391] | 344 | sem_t *thelock = (sem_t *)lock;
|
---|
| 345 | int status, error = 0;
|
---|
[2] | 346 |
|
---|
[391] | 347 | (void) error; /* silence unused-but-set-variable warning */
|
---|
| 348 | dprintf(("PyThread_release_lock(%p) called\n", lock));
|
---|
[2] | 349 |
|
---|
[391] | 350 | status = sem_post(thelock);
|
---|
| 351 | CHECK_STATUS("sem_post");
|
---|
[2] | 352 | }
|
---|
| 353 |
|
---|
| 354 | #else /* USE_SEMAPHORES */
|
---|
| 355 |
|
---|
| 356 | /*
|
---|
| 357 | * Lock support.
|
---|
| 358 | */
|
---|
[391] | 359 | PyThread_type_lock
|
---|
[2] | 360 | PyThread_allocate_lock(void)
|
---|
| 361 | {
|
---|
[391] | 362 | pthread_lock *lock;
|
---|
| 363 | int status, error = 0;
|
---|
[2] | 364 |
|
---|
[391] | 365 | dprintf(("PyThread_allocate_lock called\n"));
|
---|
| 366 | if (!initialized)
|
---|
| 367 | PyThread_init_thread();
|
---|
[2] | 368 |
|
---|
[391] | 369 | lock = (pthread_lock *) malloc(sizeof(pthread_lock));
|
---|
| 370 | if (lock) {
|
---|
| 371 | memset((void *)lock, '\0', sizeof(pthread_lock));
|
---|
| 372 | lock->locked = 0;
|
---|
[2] | 373 |
|
---|
[391] | 374 | status = pthread_mutex_init(&lock->mut,
|
---|
| 375 | pthread_mutexattr_default);
|
---|
| 376 | CHECK_STATUS("pthread_mutex_init");
|
---|
[2] | 377 |
|
---|
[391] | 378 | status = pthread_cond_init(&lock->lock_released,
|
---|
| 379 | pthread_condattr_default);
|
---|
| 380 | CHECK_STATUS("pthread_cond_init");
|
---|
[2] | 381 |
|
---|
[391] | 382 | if (error) {
|
---|
| 383 | free((void *)lock);
|
---|
| 384 | lock = 0;
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
[2] | 387 |
|
---|
[391] | 388 | dprintf(("PyThread_allocate_lock() -> %p\n", lock));
|
---|
| 389 | return (PyThread_type_lock) lock;
|
---|
[2] | 390 | }
|
---|
| 391 |
|
---|
[391] | 392 | void
|
---|
[2] | 393 | PyThread_free_lock(PyThread_type_lock lock)
|
---|
| 394 | {
|
---|
[391] | 395 | pthread_lock *thelock = (pthread_lock *)lock;
|
---|
| 396 | int status, error = 0;
|
---|
[2] | 397 |
|
---|
[391] | 398 | (void) error; /* silence unused-but-set-variable warning */
|
---|
| 399 | dprintf(("PyThread_free_lock(%p) called\n", lock));
|
---|
[2] | 400 |
|
---|
[391] | 401 | status = pthread_mutex_destroy( &thelock->mut );
|
---|
| 402 | CHECK_STATUS("pthread_mutex_destroy");
|
---|
[2] | 403 |
|
---|
[391] | 404 | status = pthread_cond_destroy( &thelock->lock_released );
|
---|
| 405 | CHECK_STATUS("pthread_cond_destroy");
|
---|
[2] | 406 |
|
---|
[391] | 407 | free((void *)thelock);
|
---|
[2] | 408 | }
|
---|
| 409 |
|
---|
[391] | 410 | int
|
---|
[2] | 411 | PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
|
---|
| 412 | {
|
---|
[391] | 413 | int success;
|
---|
| 414 | pthread_lock *thelock = (pthread_lock *)lock;
|
---|
| 415 | int status, error = 0;
|
---|
[2] | 416 |
|
---|
[391] | 417 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
|
---|
[2] | 418 |
|
---|
[391] | 419 | status = pthread_mutex_lock( &thelock->mut );
|
---|
| 420 | CHECK_STATUS("pthread_mutex_lock[1]");
|
---|
| 421 | success = thelock->locked == 0;
|
---|
[2] | 422 |
|
---|
[391] | 423 | if ( !success && waitflag ) {
|
---|
| 424 | /* continue trying until we get the lock */
|
---|
[2] | 425 |
|
---|
[391] | 426 | /* mut must be locked by me -- part of the condition
|
---|
| 427 | * protocol */
|
---|
| 428 | while ( thelock->locked ) {
|
---|
| 429 | status = pthread_cond_wait(&thelock->lock_released,
|
---|
| 430 | &thelock->mut);
|
---|
| 431 | CHECK_STATUS("pthread_cond_wait");
|
---|
| 432 | }
|
---|
| 433 | success = 1;
|
---|
| 434 | }
|
---|
| 435 | if (success) thelock->locked = 1;
|
---|
| 436 | status = pthread_mutex_unlock( &thelock->mut );
|
---|
| 437 | CHECK_STATUS("pthread_mutex_unlock[1]");
|
---|
[2] | 438 |
|
---|
[391] | 439 | if (error) success = 0;
|
---|
| 440 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
|
---|
| 441 | return success;
|
---|
[2] | 442 | }
|
---|
| 443 |
|
---|
[391] | 444 | void
|
---|
[2] | 445 | PyThread_release_lock(PyThread_type_lock lock)
|
---|
| 446 | {
|
---|
[391] | 447 | pthread_lock *thelock = (pthread_lock *)lock;
|
---|
| 448 | int status, error = 0;
|
---|
[2] | 449 |
|
---|
[391] | 450 | (void) error; /* silence unused-but-set-variable warning */
|
---|
| 451 | dprintf(("PyThread_release_lock(%p) called\n", lock));
|
---|
[2] | 452 |
|
---|
[391] | 453 | status = pthread_mutex_lock( &thelock->mut );
|
---|
| 454 | CHECK_STATUS("pthread_mutex_lock[3]");
|
---|
[2] | 455 |
|
---|
[391] | 456 | thelock->locked = 0;
|
---|
[2] | 457 |
|
---|
[391] | 458 | status = pthread_mutex_unlock( &thelock->mut );
|
---|
| 459 | CHECK_STATUS("pthread_mutex_unlock[3]");
|
---|
[2] | 460 |
|
---|
[391] | 461 | /* wake up someone (anyone, if any) waiting on the lock */
|
---|
| 462 | status = pthread_cond_signal( &thelock->lock_released );
|
---|
| 463 | CHECK_STATUS("pthread_cond_signal");
|
---|
[2] | 464 | }
|
---|
| 465 |
|
---|
| 466 | #endif /* USE_SEMAPHORES */
|
---|
| 467 |
|
---|
| 468 | /* set the thread stack size.
|
---|
| 469 | * Return 0 if size is valid, -1 if size is invalid,
|
---|
| 470 | * -2 if setting stack size is not supported.
|
---|
| 471 | */
|
---|
| 472 | static int
|
---|
| 473 | _pythread_pthread_set_stacksize(size_t size)
|
---|
| 474 | {
|
---|
| 475 | #if defined(THREAD_STACK_SIZE)
|
---|
[391] | 476 | pthread_attr_t attrs;
|
---|
| 477 | size_t tss_min;
|
---|
| 478 | int rc = 0;
|
---|
[2] | 479 | #endif
|
---|
| 480 |
|
---|
[391] | 481 | /* set to default */
|
---|
| 482 | if (size == 0) {
|
---|
| 483 | _pythread_stacksize = 0;
|
---|
| 484 | return 0;
|
---|
| 485 | }
|
---|
[2] | 486 |
|
---|
| 487 | #if defined(THREAD_STACK_SIZE)
|
---|
| 488 | #if defined(PTHREAD_STACK_MIN)
|
---|
[391] | 489 | tss_min = PTHREAD_STACK_MIN > THREAD_STACK_MIN ? PTHREAD_STACK_MIN
|
---|
| 490 | : THREAD_STACK_MIN;
|
---|
[2] | 491 | #else
|
---|
[391] | 492 | tss_min = THREAD_STACK_MIN;
|
---|
[2] | 493 | #endif
|
---|
[391] | 494 | if (size >= tss_min) {
|
---|
| 495 | /* validate stack size by setting thread attribute */
|
---|
| 496 | if (pthread_attr_init(&attrs) == 0) {
|
---|
| 497 | rc = pthread_attr_setstacksize(&attrs, size);
|
---|
| 498 | pthread_attr_destroy(&attrs);
|
---|
| 499 | if (rc == 0) {
|
---|
| 500 | _pythread_stacksize = size;
|
---|
| 501 | return 0;
|
---|
| 502 | }
|
---|
| 503 | }
|
---|
| 504 | }
|
---|
| 505 | return -1;
|
---|
[2] | 506 | #else
|
---|
[391] | 507 | return -2;
|
---|
[2] | 508 | #endif
|
---|
| 509 | }
|
---|
| 510 |
|
---|
[391] | 511 | #define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
|
---|