You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.3 KiB

  1. /* GNU pth threads interface
  2. http://www.gnu.org/software/pth
  3. 2000-05-03 Andy Dustman <andy@dustman.net>
  4. Adapted from Posix threads interface
  5. 12 May 1997 -- david arnold <davida@pobox.com>
  6. */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <pth.h>
  10. /* A pth mutex isn't sufficient to model the Python lock type
  11. * because pth mutexes can be acquired multiple times by the
  12. * same thread.
  13. *
  14. * The pth_lock struct implements a Python lock as a "locked?" bit
  15. * and a <condition, mutex> pair. In general, if the bit can be acquired
  16. * instantly, it is, else the pair is used to block the thread until the
  17. * bit is cleared.
  18. */
  19. typedef struct {
  20. char locked; /* 0=unlocked, 1=locked */
  21. /* a <cond, mutex> pair to handle an acquire of a locked lock */
  22. pth_cond_t lock_released;
  23. pth_mutex_t mut;
  24. } pth_lock;
  25. #define CHECK_STATUS(name) if (status == -1) { printf("%d ", status); perror(name); error = 1; }
  26. pth_attr_t PyThread_attr;
  27. /*
  28. * Initialization.
  29. */
  30. static void PyThread__init_thread(void)
  31. {
  32. pth_init();
  33. PyThread_attr = pth_attr_new();
  34. pth_attr_set(PyThread_attr, PTH_ATTR_STACK_SIZE, 1<<18);
  35. pth_attr_set(PyThread_attr, PTH_ATTR_JOINABLE, FALSE);
  36. }
  37. /*
  38. * Thread support.
  39. */
  40. long PyThread_start_new_thread(void (*func)(void *), void *arg)
  41. {
  42. pth_t th;
  43. dprintf(("PyThread_start_new_thread called\n"));
  44. if (!initialized)
  45. PyThread_init_thread();
  46. th = pth_spawn(PyThread_attr,
  47. (void* (*)(void *))func,
  48. (void *)arg
  49. );
  50. return th;
  51. }
  52. long PyThread_get_thread_ident(void)
  53. {
  54. volatile pth_t threadid;
  55. if (!initialized)
  56. PyThread_init_thread();
  57. /* Jump through some hoops for Alpha OSF/1 */
  58. threadid = pth_self();
  59. return (long) *(long *) &threadid;
  60. }
  61. static void do_PyThread_exit_thread(int no_cleanup)
  62. {
  63. dprintf(("PyThread_exit_thread called\n"));
  64. if (!initialized) {
  65. if (no_cleanup)
  66. _exit(0);
  67. else
  68. exit(0);
  69. }
  70. }
  71. void PyThread_exit_thread(void)
  72. {
  73. do_PyThread_exit_thread(0);
  74. }
  75. void PyThread__exit_thread(void)
  76. {
  77. do_PyThread_exit_thread(1);
  78. }
  79. #ifndef NO_EXIT_PROG
  80. static void do_PyThread_exit_prog(int status, int no_cleanup)
  81. {
  82. dprintf(("PyThread_exit_prog(%d) called\n", status));
  83. if (!initialized)
  84. if (no_cleanup)
  85. _exit(status);
  86. else
  87. exit(status);
  88. }
  89. void PyThread_exit_prog(int status)
  90. {
  91. do_PyThread_exit_prog(status, 0);
  92. }
  93. void PyThread__exit_prog(int status)
  94. {
  95. do_PyThread_exit_prog(status, 1);
  96. }
  97. #endif /* NO_EXIT_PROG */
  98. /*
  99. * Lock support.
  100. */
  101. PyThread_type_lock PyThread_allocate_lock(void)
  102. {
  103. pth_lock *lock;
  104. int status, error = 0;
  105. dprintf(("PyThread_allocate_lock called\n"));
  106. if (!initialized)
  107. PyThread_init_thread();
  108. lock = (pth_lock *) malloc(sizeof(pth_lock));
  109. memset((void *)lock, '\0', sizeof(pth_lock));
  110. if (lock) {
  111. lock->locked = 0;
  112. status = pth_mutex_init(&lock->mut);
  113. CHECK_STATUS("pth_mutex_init");
  114. status = pth_cond_init(&lock->lock_released);
  115. CHECK_STATUS("pth_cond_init");
  116. if (error) {
  117. free((void *)lock);
  118. lock = NULL;
  119. }
  120. }
  121. dprintf(("PyThread_allocate_lock() -> %p\n", lock));
  122. return (PyThread_type_lock) lock;
  123. }
  124. void PyThread_free_lock(PyThread_type_lock lock)
  125. {
  126. pth_lock *thelock = (pth_lock *)lock;
  127. dprintf(("PyThread_free_lock(%p) called\n", lock));
  128. free((void *)thelock);
  129. }
  130. int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
  131. {
  132. int success;
  133. pth_lock *thelock = (pth_lock *)lock;
  134. int status, error = 0;
  135. dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
  136. status = pth_mutex_acquire(&thelock->mut, !waitflag, NULL);
  137. CHECK_STATUS("pth_mutex_acquire[1]");
  138. success = thelock->locked == 0;
  139. if (success) thelock->locked = 1;
  140. status = pth_mutex_release( &thelock->mut );
  141. CHECK_STATUS("pth_mutex_release[1]");
  142. if ( !success && waitflag ) {
  143. /* continue trying until we get the lock */
  144. /* mut must be locked by me -- part of the condition
  145. * protocol */
  146. status = pth_mutex_acquire( &thelock->mut, !waitflag, NULL );
  147. CHECK_STATUS("pth_mutex_acquire[2]");
  148. while ( thelock->locked ) {
  149. status = pth_cond_await(&thelock->lock_released,
  150. &thelock->mut, NULL);
  151. CHECK_STATUS("pth_cond_await");
  152. }
  153. thelock->locked = 1;
  154. status = pth_mutex_release( &thelock->mut );
  155. CHECK_STATUS("pth_mutex_release[2]");
  156. success = 1;
  157. }
  158. if (error) success = 0;
  159. dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
  160. return success;
  161. }
  162. void PyThread_release_lock(PyThread_type_lock lock)
  163. {
  164. pth_lock *thelock = (pth_lock *)lock;
  165. int status, error = 0;
  166. dprintf(("PyThread_release_lock(%p) called\n", lock));
  167. status = pth_mutex_acquire( &thelock->mut, 0, NULL );
  168. CHECK_STATUS("pth_mutex_acquire[3]");
  169. thelock->locked = 0;
  170. status = pth_mutex_release( &thelock->mut );
  171. CHECK_STATUS("pth_mutex_release[3]");
  172. /* wake up someone (anyone, if any) waiting on the lock */
  173. status = pth_cond_notify( &thelock->lock_released, 0 );
  174. CHECK_STATUS("pth_cond_notify");
  175. }