/Python/thread_atheos.h

http://unladen-swallow.googlecode.com/ · C++ Header · 300 lines · 214 code · 64 blank · 22 comment · 26 complexity · c60a78e4a2310f9a8b25ef43f240617a MD5 · raw file

  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. static void do_PyThread_exit_thread(int no_cleanup)
  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. if (no_cleanup)
  103. _exit(0);
  104. else
  105. exit(0);
  106. } else {
  107. /* We're a thread */
  108. exit_thread(0);
  109. }
  110. }
  111. void PyThread_exit_thread(void)
  112. {
  113. do_PyThread_exit_thread(0);
  114. }
  115. void PyThread__exit_thread(void)
  116. {
  117. do_PyThread_exit_thread(1);
  118. }
  119. #ifndef NO_EXIT_PROG
  120. static void do_PyThread_exit_prog(int status, int no_cleanup)
  121. {
  122. dprintf(("PyThread_exit_prog(%d) called\n", status));
  123. /* No need to do anything, the threads get torn down if main()exits. */
  124. if (no_cleanup)
  125. _exit(status);
  126. else
  127. exit(status);
  128. }
  129. void PyThread_exit_prog(int status)
  130. {
  131. do_PyThread_exit_prog(status, 0);
  132. }
  133. void PyThread__exit_prog(int status)
  134. {
  135. do_PyThread_exit_prog(status, 1);
  136. }
  137. #endif /* NO_EXIT_PROG */
  138. /*
  139. * Lock support.
  140. *
  141. */
  142. static atomic_t lock_count = 0;
  143. PyThread_type_lock PyThread_allocate_lock(void)
  144. {
  145. #ifdef FASTLOCK
  146. fastmutex_t *lock;
  147. #else
  148. sem_id sema;
  149. #endif
  150. char name[OS_NAME_LENGTH];
  151. atomic_t this_lock;
  152. dprintf(("PyThread_allocate_lock called\n"));
  153. #ifdef FASTLOCK
  154. lock = (fastmutex_t *) malloc(sizeof(fastmutex_t));
  155. if (lock == NULL) {
  156. dprintf(("PyThread_allocate_lock failed: out of memory\n"));
  157. return (PyThread_type_lock) NULL;
  158. }
  159. #endif
  160. this_lock = atomic_add(&lock_count, 1);
  161. PyOS_snprintf(name, sizeof(name), "python lock (%d)", this_lock);
  162. #ifdef FASTLOCK
  163. if (fastmutex_create(name, lock) < 0) {
  164. dprintf(("PyThread_allocate_lock failed: %s\n",
  165. strerror(errno)));
  166. free(lock);
  167. lock = NULL;
  168. }
  169. dprintf(("PyThread_allocate_lock()-> %p\n", lock));
  170. return (PyThread_type_lock) lock;
  171. #else
  172. sema = create_semaphore(name, 1, 0);
  173. if (sema < 0) {
  174. dprintf(("PyThread_allocate_lock failed: %s\n",
  175. strerror(errno)));
  176. sema = 0;
  177. }
  178. dprintf(("PyThread_allocate_lock()-> %p\n", sema));
  179. return (PyThread_type_lock) sema;
  180. #endif
  181. }
  182. void PyThread_free_lock(PyThread_type_lock lock)
  183. {
  184. dprintf(("PyThread_free_lock(%p) called\n", lock));
  185. #ifdef FASTLOCK
  186. if (fastmutex_destroy((fastmutex_t *) lock) < 0) {
  187. dprintf(("PyThread_free_lock(%p) failed: %s\n", lock,
  188. strerror(errno)));
  189. }
  190. free(lock);
  191. #else
  192. if (delete_semaphore((sem_id) lock) < 0) {
  193. dprintf(("PyThread_free_lock(%p) failed: %s\n", lock,
  194. strerror(errno)));
  195. }
  196. #endif
  197. }
  198. int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
  199. {
  200. int retval;
  201. dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock,
  202. waitflag));
  203. #ifdef FASTLOCK
  204. if (waitflag)
  205. retval = fastmutex_lock((fastmutex_t *) lock);
  206. else
  207. retval = fastmutex_timedlock((fastmutex_t *) lock, 0);
  208. #else
  209. if (waitflag)
  210. retval = lock_semaphore((sem_id) lock);
  211. else
  212. retval = lock_semaphore_x((sem_id) lock, 1, 0, 0);
  213. #endif
  214. if (retval < 0) {
  215. dprintf(("PyThread_acquire_lock(%p, %d) failed: %s\n",
  216. lock, waitflag, strerror(errno)));
  217. }
  218. dprintf(("PyThread_acquire_lock(%p, %d)-> %d\n", lock, waitflag,
  219. retval));
  220. return retval < 0 ? 0 : 1;
  221. }
  222. void PyThread_release_lock(PyThread_type_lock lock)
  223. {
  224. dprintf(("PyThread_release_lock(%p) called\n", lock));
  225. #ifdef FASTLOCK
  226. if (fastmutex_unlock((fastmutex_t *) lock) < 0) {
  227. dprintf(("PyThread_release_lock(%p) failed: %s\n", lock,
  228. strerror(errno)));
  229. }
  230. #else
  231. if (unlock_semaphore((sem_id) lock) < 0) {
  232. dprintf(("PyThread_release_lock(%p) failed: %s\n", lock,
  233. strerror(errno)));
  234. }
  235. #endif
  236. }