Debian Codegraph @

pypy/module/cpyext/src/pythread_nt.c

raw symbols SCIP
1 #include <Python.h>
2 #include "pythread.h"
3 #include "src/thread.h"
4 #include <windows.h>
5
6 /* ------------------------------------------------------------------------
7 Per-thread data ("key") support.
8
9 Use PyThread_create_key() to create a new key. This is typically shared
10 across threads.
11
12 Use PyThread_set_key_value(thekey, value) to associate void* value with
13 thekey in the current thread. Each thread has a distinct mapping of thekey
14 to a void* value. Caution: if the current thread already has a mapping
15 for thekey, value is ignored.
16
17 Use PyThread_get_key_value(thekey) to retrieve the void* value associated
18 with thekey in the current thread. This returns NULL if no value is
19 associated with thekey in the current thread.
20
21 Use PyThread_delete_key_value(thekey) to forget the current thread's associated
22 value for thekey. PyThread_delete_key(thekey) forgets the values associated
23 with thekey across *all* threads.
24
25 While some of these functions have error-return values, none set any
26 Python exception.
27
28 None of the functions does memory management on behalf of the void* values.
29 You need to allocate and deallocate them yourself. If the void* values
30 happen to be PyObject*, these functions don't do refcount operations on
31 them either.
32
33 The GIL does not need to be held when calling these functions; they supply
34 their own locking. This isn't true of PyThread_create_key(), though (see
35 next paragraph).
36
37 There's a hidden assumption that PyThread_create_key() will be called before
38 any of the other functions are called. There's also a hidden assumption
39 that calls to PyThread_create_key() are serialized externally.
40 ------------------------------------------------------------------------ */
41
42
43 /* use native Windows TLS functions */
44 #define Py_HAVE_NATIVE_TLS
45
46 int
47 PyThread_create_key(void)
48 {
49 return (int) TlsAlloc();
50 }
51
52 void
53 PyThread_delete_key(int key)
54 {
55 TlsFree(key);
56 }
57
58 /* We must be careful to emulate the strange semantics implemented in thread.c,
59 * where the value is only set if it hasn't been set before.
60 */
61 int
62 PyThread_set_key_value(int key, void *value)
63 {
64 BOOL ok;
65 void *oldvalue;
66
67 assert(value != NULL);
68 oldvalue = TlsGetValue(key);
69 if (oldvalue != NULL)
70 /* ignore value if already set */
71 return 0;
72 ok = TlsSetValue(key, value);
73 if (!ok)
74 return -1;
75 return 0;
76 }
77
78 void *
79 PyThread_get_key_value(int key)
80 {
81 /* because TLS is used in the Py_END_ALLOW_THREAD macro,
82 * it is necessary to preserve the windows error state, because
83 * it is assumed to be preserved across the call to the macro.
84 * Ideally, the macro should be fixed, but it is simpler to
85 * do it here.
86 */
87 DWORD error = GetLastError();
88 void *result = TlsGetValue(key);
89 SetLastError(error);
90 return result;
91 }
92
93 void
94 PyThread_delete_key_value(int key)
95 {
96 /* NULL is used as "key missing", and it is also the default
97 * given by TlsGetValue() if nothing has been set yet.
98 */
99 TlsSetValue(key, NULL);
100 }
101
102 /* reinitialization of TLS is not necessary after fork when using
103 * the native TLS functions. And forking isn't supported on Windows either.
104 */
105 void
106 PyThread_ReInitTLS(void)
107 {}
108
109 /*
110 Platform-specific components of TSS API implementation.
111 */
112
113 int
114 PyThread_tss_create(Py_tss_t *key)
115 {
116 assert(key != NULL);
117 /* If the key has been created, function is silently skipped. */
118 if (key->_is_initialized) {
119 return 0;
120 }
121
122 DWORD result = TlsAlloc();
123 if (result == TLS_OUT_OF_INDEXES) {
124 return -1;
125 }
126 /* In Windows, platform-specific key type is DWORD. */
127 key->_key = result;
128 key->_is_initialized = 1;
129 return 0;
130 }
131
132 void
133 PyThread_tss_delete(Py_tss_t *key)
134 {
135 assert(key != NULL);
136 /* If the key has not been created, function is silently skipped. */
137 if (!key->_is_initialized) {
138 return;
139 }
140
141 TlsFree(key->_key);
142 key->_key = TLS_OUT_OF_INDEXES;
143 key->_is_initialized = 0;
144 }
145
146 int
147 PyThread_tss_set(Py_tss_t *key, void *value)
148 {
149 assert(key != NULL);
150 BOOL ok = TlsSetValue(key->_key, value);
151 return ok ? 0 : -1;
152 }
153
154 void *
155 PyThread_tss_get(Py_tss_t *key)
156 {
157 assert(key != NULL);
158 /* because TSS is used in the Py_END_ALLOW_THREAD macro,
159 * it is necessary to preserve the windows error state, because
160 * it is assumed to be preserved across the call to the macro.
161 * Ideally, the macro should be fixed, but it is simpler to
162 * do it here.
163 */
164 DWORD error = GetLastError();
165 void *result = TlsGetValue(key->_key);
166 SetLastError(error);
167 return result;
168 }
169