pthread_stubs.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef Py_CPYTHON_PTRHEAD_STUBS_H
  2. #define Py_CPYTHON_PTRHEAD_STUBS_H
  3. #if !defined(HAVE_PTHREAD_STUBS)
  4. # error "this header file requires stubbed pthreads."
  5. #endif
  6. #ifndef _POSIX_THREADS
  7. # define _POSIX_THREADS 1
  8. #endif
  9. /* Minimal pthread stubs for CPython.
  10. *
  11. * The stubs implement the minimum pthread API for CPython.
  12. * - pthread_create() fails.
  13. * - pthread_exit() calls exit(0).
  14. * - pthread_key_*() functions implement minimal TSS without destructor.
  15. * - all other functions do nothing and return 0.
  16. */
  17. #ifdef __wasi__
  18. // WASI's bits/alltypes.h provides type definitions when __NEED_ is set.
  19. // The header file can be included multiple times.
  20. # define __NEED_pthread_cond_t 1
  21. # define __NEED_pthread_condattr_t 1
  22. # define __NEED_pthread_mutex_t 1
  23. # define __NEED_pthread_mutexattr_t 1
  24. # define __NEED_pthread_key_t 1
  25. # define __NEED_pthread_t 1
  26. # define __NEED_pthread_attr_t 1
  27. # error #include <bits/alltypes.h>
  28. #else
  29. typedef struct { void *__x; } pthread_cond_t;
  30. typedef struct { unsigned __attr; } pthread_condattr_t;
  31. typedef struct { void *__x; } pthread_mutex_t;
  32. typedef struct { unsigned __attr; } pthread_mutexattr_t;
  33. typedef unsigned pthread_key_t;
  34. typedef unsigned pthread_t;
  35. typedef struct { unsigned __attr; } pthread_attr_t;
  36. #endif
  37. // mutex
  38. PyAPI_FUNC(int) pthread_mutex_init(pthread_mutex_t *restrict mutex,
  39. const pthread_mutexattr_t *restrict attr);
  40. PyAPI_FUNC(int) pthread_mutex_destroy(pthread_mutex_t *mutex);
  41. PyAPI_FUNC(int) pthread_mutex_trylock(pthread_mutex_t *mutex);
  42. PyAPI_FUNC(int) pthread_mutex_lock(pthread_mutex_t *mutex);
  43. PyAPI_FUNC(int) pthread_mutex_unlock(pthread_mutex_t *mutex);
  44. // condition
  45. PyAPI_FUNC(int) pthread_cond_init(pthread_cond_t *restrict cond,
  46. const pthread_condattr_t *restrict attr);
  47. PyAPI_FUNC(int) pthread_cond_destroy(pthread_cond_t *cond);
  48. PyAPI_FUNC(int) pthread_cond_wait(pthread_cond_t *restrict cond,
  49. pthread_mutex_t *restrict mutex);
  50. PyAPI_FUNC(int) pthread_cond_timedwait(pthread_cond_t *restrict cond,
  51. pthread_mutex_t *restrict mutex,
  52. const struct timespec *restrict abstime);
  53. PyAPI_FUNC(int) pthread_cond_signal(pthread_cond_t *cond);
  54. PyAPI_FUNC(int) pthread_condattr_init(pthread_condattr_t *attr);
  55. PyAPI_FUNC(int) pthread_condattr_setclock(
  56. pthread_condattr_t *attr, clockid_t clock_id);
  57. // pthread
  58. PyAPI_FUNC(int) pthread_create(pthread_t *restrict thread,
  59. const pthread_attr_t *restrict attr,
  60. void *(*start_routine)(void *),
  61. void *restrict arg);
  62. PyAPI_FUNC(int) pthread_detach(pthread_t thread);
  63. PyAPI_FUNC(pthread_t) pthread_self(void);
  64. PyAPI_FUNC(int) pthread_exit(void *retval) __attribute__ ((__noreturn__));
  65. PyAPI_FUNC(int) pthread_attr_init(pthread_attr_t *attr);
  66. PyAPI_FUNC(int) pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
  67. PyAPI_FUNC(int) pthread_attr_destroy(pthread_attr_t *attr);
  68. // pthread_key
  69. #ifndef PTHREAD_KEYS_MAX
  70. # define PTHREAD_KEYS_MAX 128
  71. #endif
  72. PyAPI_FUNC(int) pthread_key_create(pthread_key_t *key,
  73. void (*destr_function)(void *));
  74. PyAPI_FUNC(int) pthread_key_delete(pthread_key_t key);
  75. PyAPI_FUNC(void *) pthread_getspecific(pthread_key_t key);
  76. PyAPI_FUNC(int) pthread_setspecific(pthread_key_t key, const void *value);
  77. #endif // Py_CPYTHON_PTRHEAD_STUBS_H