thread_pthread_stubs.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "cpython/pthread_stubs.h"
  2. // mutex
  3. int
  4. pthread_mutex_init(pthread_mutex_t *restrict mutex,
  5. const pthread_mutexattr_t *restrict attr)
  6. {
  7. return 0;
  8. }
  9. int
  10. pthread_mutex_destroy(pthread_mutex_t *mutex)
  11. {
  12. return 0;
  13. }
  14. int
  15. pthread_mutex_trylock(pthread_mutex_t *mutex)
  16. {
  17. return 0;
  18. }
  19. int
  20. pthread_mutex_lock(pthread_mutex_t *mutex)
  21. {
  22. return 0;
  23. }
  24. int
  25. pthread_mutex_unlock(pthread_mutex_t *mutex)
  26. {
  27. return 0;
  28. }
  29. // condition
  30. int
  31. pthread_cond_init(pthread_cond_t *restrict cond,
  32. const pthread_condattr_t *restrict attr)
  33. {
  34. return 0;
  35. }
  36. PyAPI_FUNC(int)pthread_cond_destroy(pthread_cond_t *cond)
  37. {
  38. return 0;
  39. }
  40. int
  41. pthread_cond_wait(pthread_cond_t *restrict cond,
  42. pthread_mutex_t *restrict mutex)
  43. {
  44. return 0;
  45. }
  46. int
  47. pthread_cond_timedwait(pthread_cond_t *restrict cond,
  48. pthread_mutex_t *restrict mutex,
  49. const struct timespec *restrict abstime)
  50. {
  51. return 0;
  52. }
  53. int
  54. pthread_cond_signal(pthread_cond_t *cond)
  55. {
  56. return 0;
  57. }
  58. int
  59. pthread_condattr_init(pthread_condattr_t *attr)
  60. {
  61. return 0;
  62. }
  63. int
  64. pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)
  65. {
  66. return 0;
  67. }
  68. // pthread
  69. int
  70. pthread_create(pthread_t *restrict thread,
  71. const pthread_attr_t *restrict attr,
  72. void *(*start_routine)(void *),
  73. void *restrict arg)
  74. {
  75. return EAGAIN;
  76. }
  77. int
  78. pthread_detach(pthread_t thread)
  79. {
  80. return 0;
  81. }
  82. PyAPI_FUNC(pthread_t) pthread_self(void)
  83. {
  84. return 0;
  85. }
  86. int
  87. pthread_exit(void *retval)
  88. {
  89. exit(0);
  90. }
  91. int
  92. pthread_attr_init(pthread_attr_t *attr)
  93. {
  94. return 0;
  95. }
  96. int
  97. pthread_attr_setstacksize(
  98. pthread_attr_t *attr, size_t stacksize)
  99. {
  100. return 0;
  101. }
  102. int
  103. pthread_attr_destroy(pthread_attr_t *attr)
  104. {
  105. return 0;
  106. }
  107. typedef struct py_stub_tls_entry py_tls_entry;
  108. #define py_tls_entries (_PyRuntime.threads.stubs.tls_entries)
  109. int
  110. pthread_key_create(pthread_key_t *key, void (*destr_function)(void *))
  111. {
  112. if (!key) {
  113. return EINVAL;
  114. }
  115. if (destr_function != NULL) {
  116. Py_FatalError("pthread_key_create destructor is not supported");
  117. }
  118. for (pthread_key_t idx = 0; idx < PTHREAD_KEYS_MAX; idx++) {
  119. if (!py_tls_entries[idx].in_use) {
  120. py_tls_entries[idx].in_use = true;
  121. *key = idx;
  122. return 0;
  123. }
  124. }
  125. return EAGAIN;
  126. }
  127. int
  128. pthread_key_delete(pthread_key_t key)
  129. {
  130. if (key < 0 || key >= PTHREAD_KEYS_MAX || !py_tls_entries[key].in_use) {
  131. return EINVAL;
  132. }
  133. py_tls_entries[key].in_use = false;
  134. py_tls_entries[key].value = NULL;
  135. return 0;
  136. }
  137. void *
  138. pthread_getspecific(pthread_key_t key) {
  139. if (key < 0 || key >= PTHREAD_KEYS_MAX || !py_tls_entries[key].in_use) {
  140. return NULL;
  141. }
  142. return py_tls_entries[key].value;
  143. }
  144. int
  145. pthread_setspecific(pthread_key_t key, const void *value)
  146. {
  147. if (key < 0 || key >= PTHREAD_KEYS_MAX || !py_tls_entries[key].in_use) {
  148. return EINVAL;
  149. }
  150. py_tls_entries[key].value = (void *)value;
  151. return 0;
  152. }
  153. // let thread_pthread define the Python API
  154. #include "thread_pthread.h"