annotate Python/ceval_gil.h @ 62799:dcc2e8bf64cf

Fix: Issue9091 Minor documentation clarification.
author Senthil Kumaran <orsenthil@gmail.com>
date Mon, 05 Jul 2010 11:41:42 +0000
parents 4fd3ab5fa71d
children 39092f86b0af
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1 /*
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2 * Implementation of the Global Interpreter Lock (GIL).
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
3 */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
4
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
5 #include <stdlib.h>
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
6 #include <errno.h>
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
7
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
8
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
9 /* First some general settings */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
10
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
11 /* microseconds (the Python API uses seconds, though) */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
12 #define DEFAULT_INTERVAL 5000
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
13 static unsigned long gil_interval = DEFAULT_INTERVAL;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
14 #define INTERVAL (gil_interval >= 1 ? gil_interval : 1)
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
15
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
16 /* Enable if you want to force the switching of threads at least every `gil_interval` */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
17 #undef FORCE_SWITCHING
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
18 #define FORCE_SWITCHING
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
19
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
20
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
21 /*
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
22 Notes about the implementation:
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
23
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
24 - The GIL is just a boolean variable (gil_locked) whose access is protected
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
25 by a mutex (gil_mutex), and whose changes are signalled by a condition
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
26 variable (gil_cond). gil_mutex is taken for short periods of time,
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
27 and therefore mostly uncontended.
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
28
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
29 - In the GIL-holding thread, the main loop (PyEval_EvalFrameEx) must be
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
30 able to release the GIL on demand by another thread. A volatile boolean
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
31 variable (gil_drop_request) is used for that purpose, which is checked
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
32 at every turn of the eval loop. That variable is set after a wait of
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
33 `interval` microseconds on `gil_cond` has timed out.
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
34
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
35 [Actually, another volatile boolean variable (eval_breaker) is used
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
36 which ORs several conditions into one. Volatile booleans are
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
37 sufficient as inter-thread signalling means since Python is run
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
38 on cache-coherent architectures only.]
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
39
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
40 - A thread wanting to take the GIL will first let pass a given amount of
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
41 time (`interval` microseconds) before setting gil_drop_request. This
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
42 encourages a defined switching period, but doesn't enforce it since
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
43 opcodes can take an arbitrary time to execute.
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
44
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
45 The `interval` value is available for the user to read and modify
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
46 using the Python API `sys.{get,set}switchinterval()`.
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
47
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
48 - When a thread releases the GIL and gil_drop_request is set, that thread
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
49 ensures that another GIL-awaiting thread gets scheduled.
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
50 It does so by waiting on a condition variable (switch_cond) until
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
51 the value of gil_last_holder is changed to something else than its
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
52 own thread state pointer, indicating that another thread was able to
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
53 take the GIL.
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
54
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
55 This is meant to prohibit the latency-adverse behaviour on multi-core
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
56 machines where one thread would speculatively release the GIL, but still
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
57 run and end up being the first to re-acquire it, making the "timeslices"
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
58 much longer than expected.
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
59 (Note: this mechanism is enabled with FORCE_SWITCHING above)
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
60 */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
61
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
62 #ifndef _POSIX_THREADS
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
63 /* This means pthreads are not implemented in libc headers, hence the macro
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
64 not present in unistd.h. But they still can be implemented as an external
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
65 library (e.g. gnu pth in pthread emulation) */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
66 # ifdef HAVE_PTHREAD_H
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
67 # include <pthread.h> /* _POSIX_THREADS */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
68 # endif
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
69 #endif
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
70
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
71
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
72 #ifdef _POSIX_THREADS
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
73
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
74 /*
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
75 * POSIX support
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
76 */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
77
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
78 #include <pthread.h>
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
79
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
80 #define ADD_MICROSECONDS(tv, interval) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
81 do { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
82 tv.tv_usec += (long) interval; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
83 tv.tv_sec += tv.tv_usec / 1000000; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
84 tv.tv_usec %= 1000000; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
85 } while (0)
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
86
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
87 /* We assume all modern POSIX systems have gettimeofday() */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
88 #ifdef GETTIMEOFDAY_NO_TZ
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
89 #define GETTIMEOFDAY(ptv) gettimeofday(ptv)
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
90 #else
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
91 #define GETTIMEOFDAY(ptv) gettimeofday(ptv, (struct timezone *)NULL)
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
92 #endif
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
93
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
94 #define MUTEX_T pthread_mutex_t
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
95 #define MUTEX_INIT(mut) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
96 if (pthread_mutex_init(&mut, NULL)) { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
97 Py_FatalError("pthread_mutex_init(" #mut ") failed"); };
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
98 #define MUTEX_LOCK(mut) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
99 if (pthread_mutex_lock(&mut)) { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
100 Py_FatalError("pthread_mutex_lock(" #mut ") failed"); };
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
101 #define MUTEX_UNLOCK(mut) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
102 if (pthread_mutex_unlock(&mut)) { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
103 Py_FatalError("pthread_mutex_unlock(" #mut ") failed"); };
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
104
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
105 #define COND_T pthread_cond_t
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
106 #define COND_INIT(cond) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
107 if (pthread_cond_init(&cond, NULL)) { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
108 Py_FatalError("pthread_cond_init(" #cond ") failed"); };
57203
f9a50cede6a9 Try to strengthen condition-waiting under Windows.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57191
diff changeset
109 #define COND_RESET(cond)
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
110 #define COND_SIGNAL(cond) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
111 if (pthread_cond_signal(&cond)) { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
112 Py_FatalError("pthread_cond_signal(" #cond ") failed"); };
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
113 #define COND_WAIT(cond, mut) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
114 if (pthread_cond_wait(&cond, &mut)) { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
115 Py_FatalError("pthread_cond_wait(" #cond ") failed"); };
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
116 #define COND_TIMED_WAIT(cond, mut, microseconds, timeout_result) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
117 { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
118 int r; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
119 struct timespec ts; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
120 struct timeval deadline; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
121 \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
122 GETTIMEOFDAY(&deadline); \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
123 ADD_MICROSECONDS(deadline, microseconds); \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
124 ts.tv_sec = deadline.tv_sec; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
125 ts.tv_nsec = deadline.tv_usec * 1000; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
126 \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
127 r = pthread_cond_timedwait(&cond, &mut, &ts); \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
128 if (r == ETIMEDOUT) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
129 timeout_result = 1; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
130 else if (r) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
131 Py_FatalError("pthread_cond_timedwait(" #cond ") failed"); \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
132 else \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
133 timeout_result = 0; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
134 } \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
135
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
136 #elif defined(NT_THREADS)
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
137
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
138 /*
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
139 * Windows (2000 and later, as well as (hopefully) CE) support
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
140 */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
141
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
142 #include <windows.h>
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
143
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
144 #define MUTEX_T HANDLE
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
145 #define MUTEX_INIT(mut) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
146 if (!(mut = CreateMutex(NULL, FALSE, NULL))) { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
147 Py_FatalError("CreateMutex(" #mut ") failed"); };
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
148 #define MUTEX_LOCK(mut) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
149 if (WaitForSingleObject(mut, INFINITE) != WAIT_OBJECT_0) { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
150 Py_FatalError("WaitForSingleObject(" #mut ") failed"); };
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
151 #define MUTEX_UNLOCK(mut) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
152 if (!ReleaseMutex(mut)) { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
153 Py_FatalError("ReleaseMutex(" #mut ") failed"); };
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
154
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
155 /* We emulate condition variables with events. It is sufficient here.
57191
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
156 WaitForMultipleObjects() allows the event to be caught and the mutex
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
157 to be taken atomically.
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
158 As for SignalObjectAndWait(), its semantics are unfortunately a bit
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
159 more foggy. Many sources on the Web define it as atomically releasing
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
160 the first object while starting to wait on the second, but MSDN states
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
161 it is *not* atomic...
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
162
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
163 In any case, the emulation here is tailored for our particular use case.
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
164 For example, we don't care how many threads are woken up when a condition
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
165 gets signalled. Generic emulations of the pthread_cond_* API using
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
166 Win32 functions can be found on the Web.
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
167 The following read can be edificating (or not):
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
168 http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
169 */
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
170 #define COND_T HANDLE
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
171 #define COND_INIT(cond) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
172 /* auto-reset, non-signalled */ \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
173 if (!(cond = CreateEvent(NULL, FALSE, FALSE, NULL))) { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
174 Py_FatalError("CreateMutex(" #cond ") failed"); };
57203
f9a50cede6a9 Try to strengthen condition-waiting under Windows.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57191
diff changeset
175 #define COND_RESET(cond) \
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
176 if (!ResetEvent(cond)) { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
177 Py_FatalError("ResetEvent(" #cond ") failed"); };
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
178 #define COND_SIGNAL(cond) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
179 if (!SetEvent(cond)) { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
180 Py_FatalError("SetEvent(" #cond ") failed"); };
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
181 #define COND_WAIT(cond, mut) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
182 { \
57191
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
183 if (SignalObjectAndWait(mut, cond, INFINITE, FALSE) != WAIT_OBJECT_0) \
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
184 Py_FatalError("SignalObjectAndWait(" #mut ", " #cond") failed"); \
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
185 MUTEX_LOCK(mut); \
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
186 }
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
187 #define COND_TIMED_WAIT(cond, mut, microseconds, timeout_result) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
188 { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
189 DWORD r; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
190 HANDLE objects[2] = { cond, mut }; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
191 MUTEX_UNLOCK(mut); \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
192 r = WaitForMultipleObjects(2, objects, TRUE, microseconds / 1000); \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
193 if (r == WAIT_TIMEOUT) { \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
194 MUTEX_LOCK(mut); \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
195 timeout_result = 1; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
196 } \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
197 else if (r != WAIT_OBJECT_0) \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
198 Py_FatalError("WaitForSingleObject(" #cond ") failed"); \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
199 else \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
200 timeout_result = 0; \
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
201 }
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
202
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
203 #else
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
204
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
205 #error You need either a POSIX-compatible or a Windows system!
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
206
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
207 #endif /* _POSIX_THREADS, NT_THREADS */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
208
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
209
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
210 /* Whether the GIL is already taken (-1 if uninitialized). This is atomic
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
211 because it can be read without any lock taken in ceval.c. */
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
212 static _Py_atomic_int gil_locked = {-1};
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
213 /* Number of GIL switches since the beginning. */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
214 static unsigned long gil_switch_number = 0;
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
215 /* Last PyThreadState holding / having held the GIL. This helps us know
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
216 whether anyone else was scheduled after we dropped the GIL. */
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
217 static _Py_atomic_address gil_last_holder = {NULL};
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
218
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
219 /* This condition variable allows one or several threads to wait until
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
220 the GIL is released. In addition, the mutex also protects the above
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
221 variables. */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
222 static COND_T gil_cond;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
223 static MUTEX_T gil_mutex;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
224
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
225 #ifdef FORCE_SWITCHING
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
226 /* This condition variable helps the GIL-releasing thread wait for
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
227 a GIL-awaiting thread to be scheduled and take the GIL. */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
228 static COND_T switch_cond;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
229 static MUTEX_T switch_mutex;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
230 #endif
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
231
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
232
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
233 static int gil_created(void)
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
234 {
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
235 return _Py_atomic_load_explicit(&gil_locked, _Py_memory_order_acquire) >= 0;
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
236 }
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
237
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
238 static void create_gil(void)
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
239 {
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
240 MUTEX_INIT(gil_mutex);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
241 #ifdef FORCE_SWITCHING
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
242 MUTEX_INIT(switch_mutex);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
243 #endif
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
244 COND_INIT(gil_cond);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
245 #ifdef FORCE_SWITCHING
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
246 COND_INIT(switch_cond);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
247 #endif
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
248 _Py_atomic_store_relaxed(&gil_last_holder, NULL);
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
249 _Py_ANNOTATE_RWLOCK_CREATE(&gil_locked);
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
250 _Py_atomic_store_explicit(&gil_locked, 0, _Py_memory_order_release);
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
251 }
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
252
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
253 static void recreate_gil(void)
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
254 {
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
255 _Py_ANNOTATE_RWLOCK_DESTROY(&gil_locked);
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
256 create_gil();
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
257 }
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
258
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
259 static void drop_gil(PyThreadState *tstate)
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
260 {
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
261 /* NOTE: tstate is allowed to be NULL. */
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
262 if (!_Py_atomic_load_relaxed(&gil_locked))
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
263 Py_FatalError("drop_gil: GIL is not locked");
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
264 if (tstate != NULL &&
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
265 tstate != _Py_atomic_load_relaxed(&gil_last_holder))
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
266 Py_FatalError("drop_gil: wrong thread state");
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
267
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
268 MUTEX_LOCK(gil_mutex);
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
269 _Py_ANNOTATE_RWLOCK_RELEASED(&gil_locked, /*is_write=*/1);
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
270 _Py_atomic_store_relaxed(&gil_locked, 0);
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
271 COND_SIGNAL(gil_cond);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
272 MUTEX_UNLOCK(gil_mutex);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
273
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
274 #ifdef FORCE_SWITCHING
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
275 if (_Py_atomic_load_relaxed(&gil_drop_request) && tstate != NULL) {
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
276 MUTEX_LOCK(switch_mutex);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
277 /* Not switched yet => wait */
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
278 if (_Py_atomic_load_relaxed(&gil_last_holder) == tstate) {
57203
f9a50cede6a9 Try to strengthen condition-waiting under Windows.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57191
diff changeset
279 RESET_GIL_DROP_REQUEST();
57191
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
280 /* NOTE: if COND_WAIT does not atomically start waiting when
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
281 releasing the mutex, another thread can run through, take
c717fe993085 Our condition variable emulation under Windows is imperfect, which
Antoine Pitrou <solipsis@pitrou.net>
parents: 57175
diff changeset
282 the GIL and drop it again, and reset the condition
57203
f9a50cede6a9 Try to strengthen condition-waiting under Windows.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57191
diff changeset
283 before we even had a chance to wait for it. */
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
284 COND_WAIT(switch_cond, switch_mutex);
57203
f9a50cede6a9 Try to strengthen condition-waiting under Windows.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57191
diff changeset
285 COND_RESET(switch_cond);
f9a50cede6a9 Try to strengthen condition-waiting under Windows.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57191
diff changeset
286 }
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
287 MUTEX_UNLOCK(switch_mutex);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
288 }
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
289 #endif
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
290 }
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
291
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
292 static void take_gil(PyThreadState *tstate)
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
293 {
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
294 int err;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
295 if (tstate == NULL)
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
296 Py_FatalError("take_gil: NULL tstate");
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
297
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
298 err = errno;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
299 MUTEX_LOCK(gil_mutex);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
300
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
301 if (!_Py_atomic_load_relaxed(&gil_locked))
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
302 goto _ready;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
303
57203
f9a50cede6a9 Try to strengthen condition-waiting under Windows.
Antoine Pitrou <solipsis@pitrou.net>
parents: 57191
diff changeset
304 COND_RESET(gil_cond);
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
305 while (_Py_atomic_load_relaxed(&gil_locked)) {
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
306 int timed_out = 0;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
307 unsigned long saved_switchnum;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
308
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
309 saved_switchnum = gil_switch_number;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
310 COND_TIMED_WAIT(gil_cond, gil_mutex, INTERVAL, timed_out);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
311 /* If we timed out and no switch occurred in the meantime, it is time
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
312 to ask the GIL-holding thread to drop it. */
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
313 if (timed_out &&
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
314 _Py_atomic_load_relaxed(&gil_locked) &&
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
315 gil_switch_number == saved_switchnum) {
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
316 SET_GIL_DROP_REQUEST();
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
317 }
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
318 }
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
319 _ready:
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
320 #ifdef FORCE_SWITCHING
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
321 /* This mutex must be taken before modifying gil_last_holder (see drop_gil()). */
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
322 MUTEX_LOCK(switch_mutex);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
323 #endif
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
324 /* We now hold the GIL */
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
325 _Py_atomic_store_relaxed(&gil_locked, 1);
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
326 _Py_ANNOTATE_RWLOCK_ACQUIRED(&gil_locked, /*is_write=*/1);
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
327
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
328 if (tstate != _Py_atomic_load_relaxed(&gil_last_holder)) {
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
329 _Py_atomic_store_relaxed(&gil_last_holder, tstate);
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
330 ++gil_switch_number;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
331 }
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
332
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
333 #ifdef FORCE_SWITCHING
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
334 COND_SIGNAL(switch_cond);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
335 MUTEX_UNLOCK(switch_mutex);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
336 #endif
61184
4fd3ab5fa71d Make (most of) Python's tests pass under Thread Sanitizer.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 57203
diff changeset
337 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
57175
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
338 RESET_GIL_DROP_REQUEST();
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
339 }
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
340 if (tstate->async_exc != NULL) {
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
341 _PyEval_SignalAsyncExc();
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
342 }
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
343
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
344 MUTEX_UNLOCK(gil_mutex);
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
345 errno = err;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
346 }
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
347
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
348 void _PyEval_SetSwitchInterval(unsigned long microseconds)
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
349 {
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
350 gil_interval = microseconds;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
351 }
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
352
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
353 unsigned long _PyEval_GetSwitchInterval()
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
354 {
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
355 return gil_interval;
fdd6484f1210 Merge in the new GIL.
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
356 }