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