/Python/thread_solaris.h

http://unladen-swallow.googlecode.com/ · C++ Header · 174 lines · 142 code · 23 blank · 9 comment · 13 complexity · 20b26997027afc745d08d0554fc18f6a MD5 · raw file

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include </usr/include/thread.h>
  5. #undef _POSIX_THREADS
  6. /*
  7. * Initialization.
  8. */
  9. static void PyThread__init_thread(void)
  10. {
  11. }
  12. /*
  13. * Thread support.
  14. */
  15. struct func_arg {
  16. void (*func)(void *);
  17. void *arg;
  18. };
  19. static void *
  20. new_func(void *funcarg)
  21. {
  22. void (*func)(void *);
  23. void *arg;
  24. func = ((struct func_arg *) funcarg)->func;
  25. arg = ((struct func_arg *) funcarg)->arg;
  26. free(funcarg);
  27. (*func)(arg);
  28. return 0;
  29. }
  30. long
  31. PyThread_start_new_thread(void (*func)(void *), void *arg)
  32. {
  33. thread_t tid;
  34. struct func_arg *funcarg;
  35. dprintf(("PyThread_start_new_thread called\n"));
  36. if (!initialized)
  37. PyThread_init_thread();
  38. funcarg = (struct func_arg *) malloc(sizeof(struct func_arg));
  39. funcarg->func = func;
  40. funcarg->arg = arg;
  41. if (thr_create(0, 0, new_func, funcarg,
  42. THR_DETACHED | THR_NEW_LWP, &tid)) {
  43. perror("thr_create");
  44. free((void *) funcarg);
  45. return -1;
  46. }
  47. return tid;
  48. }
  49. long
  50. PyThread_get_thread_ident(void)
  51. {
  52. if (!initialized)
  53. PyThread_init_thread();
  54. return thr_self();
  55. }
  56. static void
  57. do_PyThread_exit_thread(int no_cleanup)
  58. {
  59. dprintf(("PyThread_exit_thread called\n"));
  60. if (!initialized)
  61. if (no_cleanup)
  62. _exit(0);
  63. else
  64. exit(0);
  65. thr_exit(0);
  66. }
  67. void
  68. PyThread_exit_thread(void)
  69. {
  70. do_PyThread_exit_thread(0);
  71. }
  72. void
  73. PyThread__exit_thread(void)
  74. {
  75. do_PyThread_exit_thread(1);
  76. }
  77. #ifndef NO_EXIT_PROG
  78. static void
  79. do_PyThread_exit_prog(int status, int no_cleanup)
  80. {
  81. dprintf(("PyThread_exit_prog(%d) called\n", status));
  82. if (!initialized)
  83. if (no_cleanup)
  84. _exit(status);
  85. else
  86. exit(status);
  87. if (no_cleanup)
  88. _exit(status);
  89. else
  90. exit(status);
  91. }
  92. void
  93. PyThread_exit_prog(int status)
  94. {
  95. do_PyThread_exit_prog(status, 0);
  96. }
  97. void
  98. PyThread__exit_prog(int status)
  99. {
  100. do_PyThread_exit_prog(status, 1);
  101. }
  102. #endif /* NO_EXIT_PROG */
  103. /*
  104. * Lock support.
  105. */
  106. PyThread_type_lock
  107. PyThread_allocate_lock(void)
  108. {
  109. mutex_t *lock;
  110. dprintf(("PyThread_allocate_lock called\n"));
  111. if (!initialized)
  112. PyThread_init_thread();
  113. lock = (mutex_t *) malloc(sizeof(mutex_t));
  114. if (mutex_init(lock, USYNC_THREAD, 0)) {
  115. perror("mutex_init");
  116. free((void *) lock);
  117. lock = 0;
  118. }
  119. dprintf(("PyThread_allocate_lock() -> %p\n", lock));
  120. return (PyThread_type_lock) lock;
  121. }
  122. void
  123. PyThread_free_lock(PyThread_type_lock lock)
  124. {
  125. dprintf(("PyThread_free_lock(%p) called\n", lock));
  126. mutex_destroy((mutex_t *) lock);
  127. free((void *) lock);
  128. }
  129. int
  130. PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
  131. {
  132. int success;
  133. dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
  134. if (waitflag)
  135. success = mutex_lock((mutex_t *) lock);
  136. else
  137. success = mutex_trylock((mutex_t *) lock);
  138. if (success < 0)
  139. perror(waitflag ? "mutex_lock" : "mutex_trylock");
  140. else
  141. success = !success; /* solaris does it the other way round */
  142. dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
  143. return success;
  144. }
  145. void
  146. PyThread_release_lock(PyThread_type_lock lock)
  147. {
  148. dprintf(("PyThread_release_lock(%p) called\n", lock));
  149. if (mutex_unlock((mutex_t *) lock))
  150. perror("mutex_unlock");
  151. }