/Python/thread_pthread.h

http://unladen-swallow.googlecode.com/ · C Header · 533 lines · 372 code · 89 blank · 72 comment · 57 complexity · 0f6b61b34ddeef59b07a41d73ad97f2d MD5 · raw file

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