Debian Codegraph @

rpython/translator/c/src/thread_pthread.h

raw symbols SCIP
1 /* Posix threads interface (from CPython) */
2
3 /* XXX needs to detect HAVE_BROKEN_POSIX_SEMAPHORES properly; currently
4 it is set only if _POSIX_SEMAPHORES == -1. Seems to be only for
5 SunOS/5.8 and AIX/5.
6 */
7
8 #include <unistd.h> /* for the _POSIX_xxx and _POSIX_THREAD_xxx defines */
9 #include <pthread.h>
10
11 /* The POSIX spec says that implementations supporting the sem_*
12 family of functions must indicate this by defining
13 _POSIX_SEMAPHORES. */
14 #ifdef _POSIX_SEMAPHORES
15 /* On FreeBSD 4.x, _POSIX_SEMAPHORES is defined empty, so
16 we need to add 0 to make it work there as well. */
17 #if (_POSIX_SEMAPHORES+0) == -1
18 #define HAVE_BROKEN_POSIX_SEMAPHORES
19 #else
20 #include <semaphore.h>
21 #endif
22 #endif
23
24 /* Whether or not to use semaphores directly rather than emulating them with
25 * mutexes and condition variables:
26 */
27 #if defined(_POSIX_SEMAPHORES) && !defined(HAVE_BROKEN_POSIX_SEMAPHORES)
28 # define USE_SEMAPHORES
29 #else
30 # undef USE_SEMAPHORES
31 #endif
32
33
34 /********************* structs ***********/
35
36 #ifdef USE_SEMAPHORES
37
38 #include <semaphore.h>
39
40 struct RPyOpaque_ThreadLock {
41 sem_t sem;
42 int initialized;
43 };
44
45 #else /* !USE_SEMAPHORE */
46
47 /* A pthread mutex isn't sufficient to model the Python lock type
48 (see explanations in CPython's Python/thread_pthread.h */
49 struct RPyOpaque_ThreadLock {
50 char locked; /* 0=unlocked, 1=locked */
51 char initialized;
52 /* a <cond, mutex> pair to handle an acquire of a locked lock */
53 pthread_cond_t lock_released;
54 pthread_mutex_t mut;
55 struct RPyOpaque_ThreadLock *prev, *next;
56 };
57
58 #endif /* USE_SEMAPHORE */
59
60 /* prototypes */
61
62 RPY_EXTERN
63 Signed RPyThreadStart(void (*func)(void));
64 RPY_EXTERN
65 Signed RPyThreadStartEx(void (*func)(void *), void *arg);
66 RPY_EXTERN
67 int RPyThreadLockInit(struct RPyOpaque_ThreadLock *lock);
68 RPY_EXTERN
69 void RPyOpaqueDealloc_ThreadLock(struct RPyOpaque_ThreadLock *lock);
70 RPY_EXTERN
71 int RPyThreadAcquireLock(struct RPyOpaque_ThreadLock *lock, int waitflag);
72 RPY_EXTERN
73 RPyLockStatus RPyThreadAcquireLockTimed(struct RPyOpaque_ThreadLock *lock,
74 RPY_TIMEOUT_T timeout, int intr_flag);
75 RPY_EXTERN
76 Signed RPyThreadReleaseLock(struct RPyOpaque_ThreadLock *lock);
77 RPY_EXTERN
78 Signed RPyThreadGetStackSize(void);
79 RPY_EXTERN
80 Signed RPyThreadSetStackSize(Signed);
81 RPY_EXTERN
82 void RPyThreadAfterFork(void);
83 RPY_EXTERN
84 int RPyThread_kill(Signed id, int signal);
85
86
87 #define pypy_compare_and_swap(ptr, oldval, newval) \
88 __sync_bool_compare_and_swap(ptr, oldval, newval)
89 #define pypy_lock_test_and_set(ptr, value) __sync_lock_test_and_set(ptr, value)
90 #define pypy_lock_release(ptr) __sync_lock_release(ptr)