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.

112 lines
2.3 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. void
  47. PyThread_exit_thread(void)
  48. {
  49. dprintf(("PyThread_exit_thread called\n"));
  50. if (!initialized)
  51. exit(0);
  52. cthread_exit(0);
  53. }
  54. /*
  55. * Lock support.
  56. */
  57. PyThread_type_lock
  58. PyThread_allocate_lock(void)
  59. {
  60. mutex_t lock;
  61. dprintf(("PyThread_allocate_lock called\n"));
  62. if (!initialized)
  63. PyThread_init_thread();
  64. lock = mutex_alloc();
  65. if (mutex_init(lock)) {
  66. perror("mutex_init");
  67. free((void *) lock);
  68. lock = 0;
  69. }
  70. dprintf(("PyThread_allocate_lock() -> %p\n", lock));
  71. return (PyThread_type_lock) lock;
  72. }
  73. void
  74. PyThread_free_lock(PyThread_type_lock lock)
  75. {
  76. dprintf(("PyThread_free_lock(%p) called\n", lock));
  77. mutex_free(lock);
  78. }
  79. int
  80. PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
  81. {
  82. int success = FALSE;
  83. dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
  84. if (waitflag) { /* blocking */
  85. mutex_lock((mutex_t)lock);
  86. success = TRUE;
  87. } else { /* non blocking */
  88. success = mutex_try_lock((mutex_t)lock);
  89. }
  90. dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
  91. return success;
  92. }
  93. void
  94. PyThread_release_lock(PyThread_type_lock lock)
  95. {
  96. dprintf(("PyThread_release_lock(%p) called\n", lock));
  97. mutex_unlock((mutex_t )lock);
  98. }