Debian Codegraph @

rpython/translator/c/src/threadlocal.h

raw symbols SCIP
1 /* Thread-local storage */
2 #ifndef _SRC_THREADLOCAL_H
3 #define _SRC_THREADLOCAL_H
4
5 #include "src/precommondefs.h"
6 #include "src/support.h"
7
8
9 /* RPython_ThreadLocals_ProgramInit() is called once at program start-up. */
10 RPY_EXTERN void RPython_ThreadLocals_ProgramInit(void);
11
12 /* RPython_ThreadLocals_ThreadDie() is called in a thread that is about
13 to die. */
14 RPY_EXTERN void RPython_ThreadLocals_ThreadDie(void);
15
16 /* 'threadlocalref_addr' returns the address of the thread-local
17 structure (of the C type 'struct pypy_threadlocal_s'). It first
18 checks if we have initialized this thread-local structure in the
19 current thread, and if not, calls the following helper. */
20 RPY_EXTERN char *_RPython_ThreadLocals_Build(void);
21
22 RPY_EXTERN void _RPython_ThreadLocals_Acquire(void);
23 RPY_EXTERN void _RPython_ThreadLocals_Release(void);
24 RPY_EXTERN int _RPython_ThreadLocals_AcquireTimeout(int max_wait_iterations);
25
26 /* Must acquire/release the thread-local lock around a series of calls
27 to the following function */
28 RPY_EXTERN struct pypy_threadlocal_s *
29 _RPython_ThreadLocals_Enum(struct pypy_threadlocal_s *prev);
30
31 /* will return the head of the list */
32 RPY_EXTERN struct pypy_threadlocal_s *_RPython_ThreadLocals_Head(void);
33
34 #define OP_THREADLOCALREF_ACQUIRE() _RPython_ThreadLocals_Acquire()
35 #define OP_THREADLOCALREF_RELEASE() _RPython_ThreadLocals_Release()
36 #define OP_THREADLOCALREF_ENUM(p, r) r = _RPython_ThreadLocals_Enum(p)
37
38
39 /* ------------------------------------------------------------ */
40 #ifdef USE___THREAD
41 /* ------------------------------------------------------------ */
42
43
44 /* Use the '__thread' specifier, so far only on Linux */
45
46 #include <pthread.h>
47
48 RPY_EXTERN __thread struct pypy_threadlocal_s pypy_threadlocal;
49
50 #define OP_THREADLOCALREF_ADDR(r) \
51 do { \
52 r = (void *)&pypy_threadlocal; \
53 if (pypy_threadlocal.ready != 42) \
54 r = _RPython_ThreadLocals_Build(); \
55 } while (0)
56
57 #define _OP_THREADLOCALREF_ADDR_SIGHANDLER(r) \
58 do { \
59 r = (void *)&pypy_threadlocal; \
60 if (pypy_threadlocal.ready != 42) \
61 r = NULL; \
62 } while (0)
63
64 #define RPY_THREADLOCALREF_ENSURE() \
65 if (pypy_threadlocal.ready != 42) \
66 (void)_RPython_ThreadLocals_Build();
67
68 #define RPY_THREADLOCALREF_GET(FIELD) pypy_threadlocal.FIELD
69
70 #define _RPy_ThreadLocals_Get() (&pypy_threadlocal)
71
72
73 /* ------------------------------------------------------------ */
74 #else
75 /* ------------------------------------------------------------ */
76
77
78 /* Don't use '__thread'. */
79
80 #ifdef _WIN32
81 # include <WinSock2.h>
82 # include <windows.h>
83 # define _RPy_ThreadLocals_Get() TlsGetValue(pypy_threadlocal_key)
84 # define _RPy_ThreadLocals_Set(x) TlsSetValue(pypy_threadlocal_key, x)
85 typedef DWORD pthread_key_t;
86 #else
87 # include <pthread.h>
88 # define _RPy_ThreadLocals_Get() pthread_getspecific(pypy_threadlocal_key)
89 # define _RPy_ThreadLocals_Set(x) pthread_setspecific(pypy_threadlocal_key, x)
90 #endif
91
92
93 #define OP_THREADLOCALREF_ADDR(r) \
94 do { \
95 r = (void *)_RPy_ThreadLocals_Get(); \
96 if (!r) \
97 r = _RPython_ThreadLocals_Build(); \
98 } while (0)
99
100 #define _OP_THREADLOCALREF_ADDR_SIGHANDLER(r) \
101 do { \
102 r = (void *)_RPy_ThreadLocals_Get(); \
103 } while (0)
104
105 #define RPY_THREADLOCALREF_ENSURE() \
106 if (!_RPy_ThreadLocals_Get()) \
107 (void)_RPython_ThreadLocals_Build();
108
109 #define RPY_THREADLOCALREF_GET(FIELD) \
110 ((struct pypy_threadlocal_s *)_RPy_ThreadLocals_Get())->FIELD
111
112
113 /* ------------------------------------------------------------ */
114 #endif
115 /* ------------------------------------------------------------ */
116
117
118 RPY_EXTERN pthread_key_t pypy_threadlocal_key;
119
120
121 #define OP_THREADLOCALREF_LOAD(RESTYPE, offset, r) \
122 do { \
123 char *a; \
124 OP_THREADLOCALREF_ADDR(a); \
125 r = *(RESTYPE *)(a + offset); \
126 } while (0)
127
128 #define OP_THREADLOCALREF_STORE(VALTYPE, offset, value) \
129 do { \
130 char *a; \
131 OP_THREADLOCALREF_ADDR(a); \
132 *(VALTYPE *)(a + offset) = value; \
133 } while (0)
134
135
136
137 // XXX hack: these functions are here instead of thread.h because
138 // we need pypy_threadlocal_s.
139 #include <src/thread.h>
140
141 static INLINE Signed _rpygil_get_my_ident(void)
142 {
143 #ifdef RPY_TLOFS_thread_ident
144 struct pypy_threadlocal_s *p = (struct pypy_threadlocal_s *)_RPy_ThreadLocals_Get();
145 assert(p->thread_ident != 0);
146 return p->thread_ident;
147 #else
148 // made-up thread identifier
149 return 1234;
150 #endif
151 }
152
153 static INLINE Signed _rpygil_acquire_fast_path(void)
154 {
155 return pypy_compare_and_swap(&rpy_fastgil, 0, _rpygil_get_my_ident());
156 }
157
158 static INLINE void _RPyGilAcquire(void) {
159 /* see thread_gil.c point (5) */
160 if (!_rpygil_acquire_fast_path())
161 RPyGilAcquireSlowPath();
162 }
163 static INLINE void _RPyGilRelease(void) {
164 assert(RPY_FASTGIL_LOCKED(rpy_fastgil));
165 pypy_lock_release(&rpy_fastgil);
166 RPyGilReleaseSignal();
167 }
168 static INLINE Signed *_RPyFetchFastGil(void) {
169 return &rpy_fastgil;
170 }
171 static INLINE Signed _RPyGilGetHolder(void) {
172 return rpy_fastgil;
173 }
174
175
176
177 #endif /* _SRC_THREADLOCAL_H */