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.

156 lines
3.0 KiB

  1. #ifdef MACH_C_THREADS
  2. #include <mach/cthreads.h>
  3. #endif
  4. #ifdef HURD_C_THREADS
  5. #include <cthreads.h>
  6. #endif
  7. /*
  8. * Initialization.
  9. */
  10. static void
  11. PyThread__init_thread(void)
  12. {
  13. #ifndef HURD_C_THREADS
  14. /* Roland McGrath said this should not be used since this is
  15. done while linking to threads */
  16. cthread_init();
  17. #else
  18. /* do nothing */
  19. ;
  20. #endif
  21. }
  22. /*
  23. * Thread support.
  24. */
  25. long
  26. PyThread_start_new_thread(void (*func)(void *), void *arg)
  27. {
  28. int success = 0; /* init not needed when SOLARIS_THREADS and */
  29. /* C_THREADS implemented properly */
  30. dprintf(("PyThread_start_new_thread called\n"));
  31. if (!initialized)
  32. PyThread_init_thread();
  33. /* looks like solaris detaches the thread to never rejoin
  34. * so well do it here
  35. */
  36. cthread_detach(cthread_fork((cthread_fn_t) func, arg));
  37. return success < 0 ? -1 : 0;
  38. }
  39. long
  40. PyThread_get_thread_ident(void)
  41. {
  42. if (!initialized)
  43. PyThread_init_thread();
  44. return (long) cthread_self();
  45. }
  46. static void
  47. do_PyThread_exit_thread(int no_cleanup)
  48. {
  49. dprintf(("PyThread_exit_thread called\n"));
  50. if (!initialized)
  51. if (no_cleanup)
  52. _exit(0);
  53. else
  54. exit(0);
  55. cthread_exit(0);
  56. }
  57. void
  58. PyThread_exit_thread(void)
  59. {
  60. do_PyThread_exit_thread(0);
  61. }
  62. void
  63. PyThread__exit_thread(void)
  64. {
  65. do_PyThread_exit_thread(1);
  66. }
  67. #ifndef NO_EXIT_PROG
  68. static
  69. void do_PyThread_exit_prog(int status, int no_cleanup)
  70. {
  71. dprintf(("PyThread_exit_prog(%d) called\n", status));
  72. if (!initialized)
  73. if (no_cleanup)
  74. _exit(status);
  75. else
  76. exit(status);
  77. if (no_cleanup)
  78. _exit(status);
  79. else
  80. exit(status);
  81. }
  82. void
  83. PyThread_exit_prog(int status)
  84. {
  85. do_PyThread_exit_prog(status, 0);
  86. }
  87. void
  88. PyThread__exit_prog(int status)
  89. {
  90. do_PyThread_exit_prog(status, 1);
  91. }
  92. #endif /* NO_EXIT_PROG */
  93. /*
  94. * Lock support.
  95. */
  96. PyThread_type_lock
  97. PyThread_allocate_lock(void)
  98. {
  99. mutex_t lock;
  100. dprintf(("PyThread_allocate_lock called\n"));
  101. if (!initialized)
  102. PyThread_init_thread();
  103. lock = mutex_alloc();
  104. if (mutex_init(lock)) {
  105. perror("mutex_init");
  106. free((void *) lock);
  107. lock = 0;
  108. }
  109. dprintf(("PyThread_allocate_lock() -> %p\n", lock));
  110. return (PyThread_type_lock) lock;
  111. }
  112. void
  113. PyThread_free_lock(PyThread_type_lock lock)
  114. {
  115. dprintf(("PyThread_free_lock(%p) called\n", lock));
  116. mutex_free(lock);
  117. }
  118. int
  119. PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
  120. {
  121. int success = FALSE;
  122. dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
  123. if (waitflag) { /* blocking */
  124. mutex_lock((mutex_t)lock);
  125. success = TRUE;
  126. } else { /* non blocking */
  127. success = mutex_try_lock((mutex_t)lock);
  128. }
  129. dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
  130. return success;
  131. }
  132. void
  133. PyThread_release_lock(PyThread_type_lock lock)
  134. {
  135. dprintf(("PyThread_release_lock(%p) called\n", lock));
  136. mutex_unlock((mutex_t )lock);
  137. }