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.

259 lines
5.8 KiB

  1. /* Threading for AtheOS.
  2. Based on thread_beos.h. */
  3. #include <atheos/threads.h>
  4. #include <atheos/semaphore.h>
  5. #include <atheos/atomic.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. /* Missing decl from threads.h */
  9. extern int exit_thread(int);
  10. /* Undefine FASTLOCK to play with simple semaphores. */
  11. #define FASTLOCK
  12. #ifdef FASTLOCK
  13. /* Use an atomic counter and a semaphore for maximum speed. */
  14. typedef struct fastmutex {
  15. sem_id sem;
  16. atomic_t count;
  17. } fastmutex_t;
  18. static int fastmutex_create(const char *name, fastmutex_t * mutex);
  19. static int fastmutex_destroy(fastmutex_t * mutex);
  20. static int fastmutex_lock(fastmutex_t * mutex);
  21. static int fastmutex_timedlock(fastmutex_t * mutex, bigtime_t timeout);
  22. static int fastmutex_unlock(fastmutex_t * mutex);
  23. static int fastmutex_create(const char *name, fastmutex_t * mutex)
  24. {
  25. mutex->count = 0;
  26. mutex->sem = create_semaphore(name, 0, 0);
  27. return (mutex->sem < 0) ? -1 : 0;
  28. }
  29. static int fastmutex_destroy(fastmutex_t * mutex)
  30. {
  31. if (fastmutex_timedlock(mutex, 0) == 0 || errno == EWOULDBLOCK) {
  32. return delete_semaphore(mutex->sem);
  33. }
  34. return 0;
  35. }
  36. static int fastmutex_lock(fastmutex_t * mutex)
  37. {
  38. atomic_t prev = atomic_add(&mutex->count, 1);
  39. if (prev > 0)
  40. return lock_semaphore(mutex->sem);
  41. return 0;
  42. }
  43. static int fastmutex_timedlock(fastmutex_t * mutex, bigtime_t timeout)
  44. {
  45. atomic_t prev = atomic_add(&mutex->count, 1);
  46. if (prev > 0)
  47. return lock_semaphore_x(mutex->sem, 1, 0, timeout);
  48. return 0;
  49. }
  50. static int fastmutex_unlock(fastmutex_t * mutex)
  51. {
  52. atomic_t prev = atomic_add(&mutex->count, -1);
  53. if (prev > 1)
  54. return unlock_semaphore(mutex->sem);
  55. return 0;
  56. }
  57. #endif /* FASTLOCK */
  58. /*
  59. * Initialization.
  60. *
  61. */
  62. static void PyThread__init_thread(void)
  63. {
  64. /* Do nothing. */
  65. return;
  66. }
  67. /*
  68. * Thread support.
  69. *
  70. */
  71. static atomic_t thread_count = 0;
  72. long PyThread_start_new_thread(void (*func) (void *), void *arg)
  73. {
  74. status_t success = -1;
  75. thread_id tid;
  76. char name[OS_NAME_LENGTH];
  77. atomic_t this_thread;
  78. dprintf(("PyThread_start_new_thread called\n"));
  79. this_thread = atomic_add(&thread_count, 1);
  80. PyOS_snprintf(name, sizeof(name), "python thread (%d)", this_thread);
  81. tid = spawn_thread(name, func, NORMAL_PRIORITY, 0, arg);
  82. if (tid < 0) {
  83. dprintf(("PyThread_start_new_thread spawn_thread failed: %s\n", strerror(errno)));
  84. } else {
  85. success = resume_thread(tid);
  86. if (success < 0) {
  87. dprintf(("PyThread_start_new_thread resume_thread failed: %s\n", strerror(errno)));
  88. }
  89. }
  90. return (success < 0 ? -1 : tid);
  91. }
  92. long PyThread_get_thread_ident(void)
  93. {
  94. return get_thread_id(NULL);
  95. }
  96. void PyThread_exit_thread(void)
  97. {
  98. dprintf(("PyThread_exit_thread called\n"));
  99. /* Thread-safe way to read a variable without a mutex: */
  100. if (atomic_add(&thread_count, 0) == 0) {
  101. /* No threads around, so exit main(). */
  102. exit(0);
  103. } else {
  104. /* We're a thread */
  105. exit_thread(0);
  106. }
  107. }
  108. /*
  109. * Lock support.
  110. *
  111. */
  112. static atomic_t lock_count = 0;
  113. PyThread_type_lock PyThread_allocate_lock(void)
  114. {
  115. #ifdef FASTLOCK
  116. fastmutex_t *lock;
  117. #else
  118. sem_id sema;
  119. #endif
  120. char name[OS_NAME_LENGTH];
  121. atomic_t this_lock;
  122. dprintf(("PyThread_allocate_lock called\n"));
  123. #ifdef FASTLOCK
  124. lock = (fastmutex_t *) malloc(sizeof(fastmutex_t));
  125. if (lock == NULL) {
  126. dprintf(("PyThread_allocate_lock failed: out of memory\n"));
  127. return (PyThread_type_lock) NULL;
  128. }
  129. #endif
  130. this_lock = atomic_add(&lock_count, 1);
  131. PyOS_snprintf(name, sizeof(name), "python lock (%d)", this_lock);
  132. #ifdef FASTLOCK
  133. if (fastmutex_create(name, lock) < 0) {
  134. dprintf(("PyThread_allocate_lock failed: %s\n",
  135. strerror(errno)));
  136. free(lock);
  137. lock = NULL;
  138. }
  139. dprintf(("PyThread_allocate_lock()-> %p\n", lock));
  140. return (PyThread_type_lock) lock;
  141. #else
  142. sema = create_semaphore(name, 1, 0);
  143. if (sema < 0) {
  144. dprintf(("PyThread_allocate_lock failed: %s\n",
  145. strerror(errno)));
  146. sema = 0;
  147. }
  148. dprintf(("PyThread_allocate_lock()-> %p\n", sema));
  149. return (PyThread_type_lock) sema;
  150. #endif
  151. }
  152. void PyThread_free_lock(PyThread_type_lock lock)
  153. {
  154. dprintf(("PyThread_free_lock(%p) called\n", lock));
  155. #ifdef FASTLOCK
  156. if (fastmutex_destroy((fastmutex_t *) lock) < 0) {
  157. dprintf(("PyThread_free_lock(%p) failed: %s\n", lock,
  158. strerror(errno)));
  159. }
  160. free(lock);
  161. #else
  162. if (delete_semaphore((sem_id) lock) < 0) {
  163. dprintf(("PyThread_free_lock(%p) failed: %s\n", lock,
  164. strerror(errno)));
  165. }
  166. #endif
  167. }
  168. int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
  169. {
  170. int retval;
  171. dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock,
  172. waitflag));
  173. #ifdef FASTLOCK
  174. if (waitflag)
  175. retval = fastmutex_lock((fastmutex_t *) lock);
  176. else
  177. retval = fastmutex_timedlock((fastmutex_t *) lock, 0);
  178. #else
  179. if (waitflag)
  180. retval = lock_semaphore((sem_id) lock);
  181. else
  182. retval = lock_semaphore_x((sem_id) lock, 1, 0, 0);
  183. #endif
  184. if (retval < 0) {
  185. dprintf(("PyThread_acquire_lock(%p, %d) failed: %s\n",
  186. lock, waitflag, strerror(errno)));
  187. }
  188. dprintf(("PyThread_acquire_lock(%p, %d)-> %d\n", lock, waitflag,
  189. retval));
  190. return retval < 0 ? 0 : 1;
  191. }
  192. void PyThread_release_lock(PyThread_type_lock lock)
  193. {
  194. dprintf(("PyThread_release_lock(%p) called\n", lock));
  195. #ifdef FASTLOCK
  196. if (fastmutex_unlock((fastmutex_t *) lock) < 0) {
  197. dprintf(("PyThread_release_lock(%p) failed: %s\n", lock,
  198. strerror(errno)));
  199. }
  200. #else
  201. if (unlock_semaphore((sem_id) lock) < 0) {
  202. dprintf(("PyThread_release_lock(%p) failed: %s\n", lock,
  203. strerror(errno)));
  204. }
  205. #endif
  206. }