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.

178 lines
4.6 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. void PyThread_exit_thread(void)
  62. {
  63. dprintf(("PyThread_exit_thread called\n"));
  64. if (!initialized) {
  65. exit(0);
  66. }
  67. }
  68. /*
  69. * Lock support.
  70. */
  71. PyThread_type_lock PyThread_allocate_lock(void)
  72. {
  73. pth_lock *lock;
  74. int status, error = 0;
  75. dprintf(("PyThread_allocate_lock called\n"));
  76. if (!initialized)
  77. PyThread_init_thread();
  78. lock = (pth_lock *) malloc(sizeof(pth_lock));
  79. memset((void *)lock, '\0', sizeof(pth_lock));
  80. if (lock) {
  81. lock->locked = 0;
  82. status = pth_mutex_init(&lock->mut);
  83. CHECK_STATUS("pth_mutex_init");
  84. status = pth_cond_init(&lock->lock_released);
  85. CHECK_STATUS("pth_cond_init");
  86. if (error) {
  87. free((void *)lock);
  88. lock = NULL;
  89. }
  90. }
  91. dprintf(("PyThread_allocate_lock() -> %p\n", lock));
  92. return (PyThread_type_lock) lock;
  93. }
  94. void PyThread_free_lock(PyThread_type_lock lock)
  95. {
  96. pth_lock *thelock = (pth_lock *)lock;
  97. dprintf(("PyThread_free_lock(%p) called\n", lock));
  98. free((void *)thelock);
  99. }
  100. int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
  101. {
  102. int success;
  103. pth_lock *thelock = (pth_lock *)lock;
  104. int status, error = 0;
  105. dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
  106. status = pth_mutex_acquire(&thelock->mut, !waitflag, NULL);
  107. CHECK_STATUS("pth_mutex_acquire[1]");
  108. success = thelock->locked == 0;
  109. if (success) thelock->locked = 1;
  110. status = pth_mutex_release( &thelock->mut );
  111. CHECK_STATUS("pth_mutex_release[1]");
  112. if ( !success && waitflag ) {
  113. /* continue trying until we get the lock */
  114. /* mut must be locked by me -- part of the condition
  115. * protocol */
  116. status = pth_mutex_acquire( &thelock->mut, !waitflag, NULL );
  117. CHECK_STATUS("pth_mutex_acquire[2]");
  118. while ( thelock->locked ) {
  119. status = pth_cond_await(&thelock->lock_released,
  120. &thelock->mut, NULL);
  121. CHECK_STATUS("pth_cond_await");
  122. }
  123. thelock->locked = 1;
  124. status = pth_mutex_release( &thelock->mut );
  125. CHECK_STATUS("pth_mutex_release[2]");
  126. success = 1;
  127. }
  128. if (error) success = 0;
  129. dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
  130. return success;
  131. }
  132. void PyThread_release_lock(PyThread_type_lock lock)
  133. {
  134. pth_lock *thelock = (pth_lock *)lock;
  135. int status, error = 0;
  136. dprintf(("PyThread_release_lock(%p) called\n", lock));
  137. status = pth_mutex_acquire( &thelock->mut, 0, NULL );
  138. CHECK_STATUS("pth_mutex_acquire[3]");
  139. thelock->locked = 0;
  140. status = pth_mutex_release( &thelock->mut );
  141. CHECK_STATUS("pth_mutex_release[3]");
  142. /* wake up someone (anyone, if any) waiting on the lock */
  143. status = pth_cond_notify( &thelock->lock_released, 0 );
  144. CHECK_STATUS("pth_cond_notify");
  145. }