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 #  define __NEED_pthread_cond_t 1
25 #  define __NEED_pthread_condattr_t 1
26 #  define __NEED_pthread_mutex_t 1
27 #  define __NEED_pthread_mutexattr_t 1
28 #  define __NEED_pthread_key_t 1
29 #  define __NEED_pthread_t 1
30 #  define __NEED_pthread_attr_t 1
31 #  include <bits/alltypes.h>
32 #else
33 typedef struct { void *__x; } pthread_cond_t;
34 typedef struct { unsigned __attr; } pthread_condattr_t;
35 typedef struct { void *__x; } pthread_mutex_t;
36 typedef struct { unsigned __attr; } pthread_mutexattr_t;
37 typedef unsigned pthread_key_t;
38 typedef unsigned pthread_t;
39 typedef struct { unsigned __attr; } pthread_attr_t;
40 #endif
41 
42 // mutex
43 PyAPI_FUNC(int) pthread_mutex_init(pthread_mutex_t *restrict mutex,
44                                    const pthread_mutexattr_t *restrict attr);
45 PyAPI_FUNC(int) pthread_mutex_destroy(pthread_mutex_t *mutex);
46 PyAPI_FUNC(int) pthread_mutex_trylock(pthread_mutex_t *mutex);
47 PyAPI_FUNC(int) pthread_mutex_lock(pthread_mutex_t *mutex);
48 PyAPI_FUNC(int) pthread_mutex_unlock(pthread_mutex_t *mutex);
49 
50 // condition
51 PyAPI_FUNC(int) pthread_cond_init(pthread_cond_t *restrict cond,
52                                   const pthread_condattr_t *restrict attr);
53 PyAPI_FUNC(int) pthread_cond_destroy(pthread_cond_t *cond);
54 PyAPI_FUNC(int) pthread_cond_wait(pthread_cond_t *restrict cond,
55                                   pthread_mutex_t *restrict mutex);
56 PyAPI_FUNC(int) pthread_cond_timedwait(pthread_cond_t *restrict cond,
57                                        pthread_mutex_t *restrict mutex,
58                                        const struct timespec *restrict abstime);
59 PyAPI_FUNC(int) pthread_cond_signal(pthread_cond_t *cond);
60 PyAPI_FUNC(int) pthread_condattr_init(pthread_condattr_t *attr);
61 PyAPI_FUNC(int) pthread_condattr_setclock(
62     pthread_condattr_t *attr, clockid_t clock_id);
63 
64 // pthread
65 PyAPI_FUNC(int) pthread_create(pthread_t *restrict thread,
66                                const pthread_attr_t *restrict attr,
67                                void *(*start_routine)(void *),
68                                void *restrict arg);
69 PyAPI_FUNC(int) pthread_detach(pthread_t thread);
70 PyAPI_FUNC(pthread_t) pthread_self(void);
71 PyAPI_FUNC(int) pthread_exit(void *retval) __attribute__ ((__noreturn__));
72 PyAPI_FUNC(int) pthread_attr_init(pthread_attr_t *attr);
73 PyAPI_FUNC(int) pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
74 PyAPI_FUNC(int) pthread_attr_destroy(pthread_attr_t *attr);
75 
76 
77 // pthread_key
78 #ifndef PTHREAD_KEYS_MAX
79 #  define PTHREAD_KEYS_MAX 128
80 #endif
81 
82 PyAPI_FUNC(int) pthread_key_create(pthread_key_t *key,
83                                    void (*destr_function)(void *));
84 PyAPI_FUNC(int) pthread_key_delete(pthread_key_t key);
85 PyAPI_FUNC(void *) pthread_getspecific(pthread_key_t key);
86 PyAPI_FUNC(int) pthread_setspecific(pthread_key_t key, const void *value);
87 
88 #endif // Py_CPYTHON_PTRHEAD_STUBS_H
89