Mercurial > cpython-lru_cache-weakref
annotate Python/condvar.h @ 86301:500b4b62c19a
Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
is pre-initialized to that value. Test show a 5% speedup as a bonus.
| author | Kristján Valur Jónsson <kristjan@ccpgames.com> |
|---|---|
| date | Sun, 13 Oct 2013 13:41:59 +0000 |
| parents | c6af0b3de726 |
| children |
| rev | line source |
|---|---|
|
77513
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
1 /* |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
2 * Portable condition variable support for windows and pthreads. |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
3 * Everything is inline, this header can be included where needed. |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
4 * |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
5 * APIs generally return 0 on success and non-zero on error, |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
6 * and the caller needs to use its platform's error mechanism to |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
7 * discover the error (errno, or GetLastError()) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
8 * |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
9 * Note that some implementations cannot distinguish between a |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
10 * condition variable wait time-out and successful wait. Most often |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
11 * the difference is moot anyway since the wait condition must be |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
12 * re-checked. |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
13 * PyCOND_TIMEDWAIT, in addition to returning negative on error, |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
14 * thus returns 0 on regular success, 1 on timeout |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
15 * or 2 if it can't tell. |
|
77518
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
16 * |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
17 * There are at least two caveats with using these condition variables, |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
18 * due to the fact that they may be emulated with Semaphores on |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
19 * Windows: |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
20 * 1) While PyCOND_SIGNAL() will wake up at least one thread, we |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
21 * cannot currently guarantee that it will be one of the threads |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
22 * already waiting in a PyCOND_WAIT() call. It _could_ cause |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
23 * the wakeup of a subsequent thread to try a PyCOND_WAIT(), |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
24 * including the thread doing the PyCOND_SIGNAL() itself. |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
25 * The same applies to PyCOND_BROADCAST(), if N threads are waiting |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
26 * then at least N threads will be woken up, but not necessarily |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
27 * those already waiting. |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
28 * For this reason, don't make the scheduling assumption that a |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
29 * specific other thread will get the wakeup signal |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
30 * 2) The _mutex_ must be held when calling PyCOND_SIGNAL() and |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
31 * PyCOND_BROADCAST(). |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
32 * While e.g. the posix standard strongly recommends that the mutex |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
33 * associated with the condition variable is held when a |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
34 * pthread_cond_signal() call is made, this is not a hard requirement, |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
35 * although scheduling will not be "reliable" if it isn't. Here |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
36 * the mutex is used for internal synchronization of the emulated |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
37 * Condition Variable. |
|
77513
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
38 */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
39 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
40 #ifndef _CONDVAR_H_ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
41 #define _CONDVAR_H_ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
42 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
43 #include "Python.h" |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
44 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
45 #ifndef _POSIX_THREADS |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
46 /* This means pthreads are not implemented in libc headers, hence the macro |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
47 not present in unistd.h. But they still can be implemented as an external |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
48 library (e.g. gnu pth in pthread emulation) */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
49 # ifdef HAVE_PTHREAD_H |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
50 # include <pthread.h> /* _POSIX_THREADS */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
51 # endif |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
52 #endif |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
53 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
54 #ifdef _POSIX_THREADS |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
55 /* |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
56 * POSIX support |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
57 */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
58 #define Py_HAVE_CONDVAR |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
59 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
60 #include <pthread.h> |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
61 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
62 #define PyCOND_ADD_MICROSECONDS(tv, interval) \ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
63 do { \ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
64 tv.tv_usec += (long) interval; \ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
65 tv.tv_sec += tv.tv_usec / 1000000; \ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
66 tv.tv_usec %= 1000000; \ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
67 } while (0) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
68 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
69 /* We assume all modern POSIX systems have gettimeofday() */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
70 #ifdef GETTIMEOFDAY_NO_TZ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
71 #define PyCOND_GETTIMEOFDAY(ptv) gettimeofday(ptv) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
72 #else |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
73 #define PyCOND_GETTIMEOFDAY(ptv) gettimeofday(ptv, (struct timezone *)NULL) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
74 #endif |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
75 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
76 /* The following functions return 0 on success, nonzero on error */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
77 #define PyMUTEX_T pthread_mutex_t |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
78 #define PyMUTEX_INIT(mut) pthread_mutex_init((mut), NULL) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
79 #define PyMUTEX_FINI(mut) pthread_mutex_destroy(mut) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
80 #define PyMUTEX_LOCK(mut) pthread_mutex_lock(mut) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
81 #define PyMUTEX_UNLOCK(mut) pthread_mutex_unlock(mut) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
82 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
83 #define PyCOND_T pthread_cond_t |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
84 #define PyCOND_INIT(cond) pthread_cond_init((cond), NULL) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
85 #define PyCOND_FINI(cond) pthread_cond_destroy(cond) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
86 #define PyCOND_SIGNAL(cond) pthread_cond_signal(cond) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
87 #define PyCOND_BROADCAST(cond) pthread_cond_broadcast(cond) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
88 #define PyCOND_WAIT(cond, mut) pthread_cond_wait((cond), (mut)) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
89 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
90 /* return 0 for success, 1 on timeout, -1 on error */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
91 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
92 PyCOND_TIMEDWAIT(PyCOND_T *cond, PyMUTEX_T *mut, long us) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
93 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
94 int r; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
95 struct timespec ts; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
96 struct timeval deadline; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
97 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
98 PyCOND_GETTIMEOFDAY(&deadline); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
99 PyCOND_ADD_MICROSECONDS(deadline, us); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
100 ts.tv_sec = deadline.tv_sec; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
101 ts.tv_nsec = deadline.tv_usec * 1000; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
102 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
103 r = pthread_cond_timedwait((cond), (mut), &ts); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
104 if (r == ETIMEDOUT) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
105 return 1; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
106 else if (r) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
107 return -1; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
108 else |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
109 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
110 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
111 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
112 #elif defined(NT_THREADS) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
113 /* |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
114 * Windows (XP, 2003 server and later, as well as (hopefully) CE) support |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
115 * |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
116 * Emulated condition variables ones that work with XP and later, plus |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
117 * example native support on VISTA and onwards. |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
118 */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
119 #define Py_HAVE_CONDVAR |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
120 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
121 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
122 /* include windows if it hasn't been done before */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
123 #define WIN32_LEAN_AND_MEAN |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
124 #include <windows.h> |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
125 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
126 /* options */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
127 /* non-emulated condition variables are provided for those that want |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
128 * to target Windows Vista. Modify this macro to enable them. |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
129 */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
130 #ifndef _PY_EMULATED_WIN_CV |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
131 #define _PY_EMULATED_WIN_CV 1 /* use emulated condition variables */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
132 #endif |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
133 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
134 /* fall back to emulation if not targeting Vista */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
135 #if !defined NTDDI_VISTA || NTDDI_VERSION < NTDDI_VISTA |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
136 #undef _PY_EMULATED_WIN_CV |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
137 #define _PY_EMULATED_WIN_CV 1 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
138 #endif |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
139 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
140 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
141 #if _PY_EMULATED_WIN_CV |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
142 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
143 /* The mutex is a CriticalSection object and |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
144 The condition variables is emulated with the help of a semaphore. |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
145 Semaphores are available on Windows XP (2003 server) and later. |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
146 We use a Semaphore rather than an auto-reset event, because although |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
147 an auto-resent event might appear to solve the lost-wakeup bug (race |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
148 condition between releasing the outer lock and waiting) because it |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
149 maintains state even though a wait hasn't happened, there is still |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
150 a lost wakeup problem if more than one thread are interrupted in the |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
151 critical place. A semaphore solves that, because its state is counted, |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
152 not Boolean. |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
153 Because it is ok to signal a condition variable with no one |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
154 waiting, we need to keep track of the number of |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
155 waiting threads. Otherwise, the semaphore's state could rise |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
156 without bound. This also helps reduce the number of "spurious wakeups" |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
157 that would otherwise happen. |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
158 |
|
77518
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
159 This implementation still has the problem that the threads woken |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
160 with a "signal" aren't necessarily those that are already |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
161 waiting. It corresponds to listing 2 in: |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
162 http://birrell.org/andrew/papers/ImplementingCVs.pdf |
|
d7a72fdcc168
Issue #15038: Document caveats with the emulated condition variables.
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77515
diff
changeset
|
163 |
|
77513
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
164 Generic emulations of the pthread_cond_* API using |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
165 earlier Win32 functions can be found on the Web. |
|
82818
fde60d3f542e
Issue #15038 : Fixing the condition broadcast and docs.
Kristján Valur Jónsson <sweskman@gmail.com>
parents:
77518
diff
changeset
|
166 The following read can be give background information to these issues, |
|
fde60d3f542e
Issue #15038 : Fixing the condition broadcast and docs.
Kristján Valur Jónsson <sweskman@gmail.com>
parents:
77518
diff
changeset
|
167 but the implementations are all broken in some way. |
|
77513
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
168 http://www.cse.wustl.edu/~schmidt/win32-cv-1.html |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
169 */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
170 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
171 typedef CRITICAL_SECTION PyMUTEX_T; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
172 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
173 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
174 PyMUTEX_INIT(PyMUTEX_T *cs) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
175 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
176 InitializeCriticalSection(cs); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
177 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
178 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
179 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
180 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
181 PyMUTEX_FINI(PyMUTEX_T *cs) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
182 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
183 DeleteCriticalSection(cs); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
184 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
185 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
186 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
187 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
188 PyMUTEX_LOCK(PyMUTEX_T *cs) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
189 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
190 EnterCriticalSection(cs); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
191 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
192 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
193 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
194 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
195 PyMUTEX_UNLOCK(PyMUTEX_T *cs) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
196 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
197 LeaveCriticalSection(cs); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
198 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
199 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
200 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
201 /* The ConditionVariable object. From XP onwards it is easily emulated with |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
202 * a Semaphore |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
203 */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
204 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
205 typedef struct _PyCOND_T |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
206 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
207 HANDLE sem; |
|
77515
110b38c36a31
Issue #15038:
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77513
diff
changeset
|
208 int waiting; /* to allow PyCOND_SIGNAL to be a no-op */ |
|
77513
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
209 } PyCOND_T; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
210 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
211 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
212 PyCOND_INIT(PyCOND_T *cv) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
213 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
214 /* A semaphore with a "large" max value, The positive value |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
215 * is only needed to catch those "lost wakeup" events and |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
216 * race conditions when a timed wait elapses. |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
217 */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
218 cv->sem = CreateSemaphore(NULL, 0, 100000, NULL); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
219 if (cv->sem==NULL) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
220 return -1; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
221 cv->waiting = 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
222 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
223 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
224 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
225 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
226 PyCOND_FINI(PyCOND_T *cv) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
227 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
228 return CloseHandle(cv->sem) ? 0 : -1; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
229 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
230 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
231 /* this implementation can detect a timeout. Returns 1 on timeout, |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
232 * 0 otherwise (and -1 on error) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
233 */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
234 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
235 _PyCOND_WAIT_MS(PyCOND_T *cv, PyMUTEX_T *cs, DWORD ms) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
236 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
237 DWORD wait; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
238 cv->waiting++; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
239 PyMUTEX_UNLOCK(cs); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
240 /* "lost wakeup bug" would occur if the caller were interrupted here, |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
241 * but we are safe because we are using a semaphore wich has an internal |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
242 * count. |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
243 */ |
|
81712
b71b6517af63
Replace WaitForSingleObject with WaitForSingleObjectEx,
Martin v. Löwis <martin@v.loewis.de>
parents:
77518
diff
changeset
|
244 wait = WaitForSingleObjectEx(cv->sem, ms, FALSE); |
|
77513
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
245 PyMUTEX_LOCK(cs); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
246 if (wait != WAIT_OBJECT_0) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
247 --cv->waiting; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
248 /* Here we have a benign race condition with PyCOND_SIGNAL. |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
249 * When failure occurs or timeout, it is possible that |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
250 * PyCOND_SIGNAL also decrements this value |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
251 * and signals releases the mutex. This is benign because it |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
252 * just means an extra spurious wakeup for a waiting thread. |
|
77515
110b38c36a31
Issue #15038:
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77513
diff
changeset
|
253 * ('waiting' corresponds to the semaphore's "negative" count and |
|
110b38c36a31
Issue #15038:
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77513
diff
changeset
|
254 * we may end up with e.g. (waiting == -1 && sem.count == 1). When |
|
110b38c36a31
Issue #15038:
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77513
diff
changeset
|
255 * a new thread comes along, it will pass right throuhgh, having |
|
110b38c36a31
Issue #15038:
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77513
diff
changeset
|
256 * adjusted it to (waiting == 0 && sem.count == 0). |
|
77513
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
257 */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
258 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
259 if (wait == WAIT_FAILED) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
260 return -1; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
261 /* return 0 on success, 1 on timeout */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
262 return wait != WAIT_OBJECT_0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
263 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
264 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
265 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
266 PyCOND_WAIT(PyCOND_T *cv, PyMUTEX_T *cs) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
267 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
268 int result = _PyCOND_WAIT_MS(cv, cs, INFINITE); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
269 return result >= 0 ? 0 : result; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
270 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
271 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
272 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
273 PyCOND_TIMEDWAIT(PyCOND_T *cv, PyMUTEX_T *cs, long us) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
274 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
275 return _PyCOND_WAIT_MS(cv, cs, us/1000); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
276 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
277 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
278 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
279 PyCOND_SIGNAL(PyCOND_T *cv) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
280 { |
|
77515
110b38c36a31
Issue #15038:
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77513
diff
changeset
|
281 /* this test allows PyCOND_SIGNAL to be a no-op unless required |
|
110b38c36a31
Issue #15038:
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77513
diff
changeset
|
282 * to wake someone up, thus preventing an unbounded increase of |
|
110b38c36a31
Issue #15038:
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77513
diff
changeset
|
283 * the semaphore's internal counter. |
|
110b38c36a31
Issue #15038:
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77513
diff
changeset
|
284 */ |
|
110b38c36a31
Issue #15038:
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77513
diff
changeset
|
285 if (cv->waiting > 0) { |
|
77513
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
286 /* notifying thread decreases the cv->waiting count so that |
|
77515
110b38c36a31
Issue #15038:
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77513
diff
changeset
|
287 * a delay between notify and actual wakeup of the target thread |
|
110b38c36a31
Issue #15038:
Kristjan Valur Jonsson <sweskman@gmail.com>
parents:
77513
diff
changeset
|
288 * doesn't cause a number of extra ReleaseSemaphore calls. |
|
77513
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
289 */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
290 cv->waiting--; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
291 return ReleaseSemaphore(cv->sem, 1, NULL) ? 0 : -1; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
292 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
293 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
294 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
295 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
296 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
297 PyCOND_BROADCAST(PyCOND_T *cv) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
298 { |
|
82818
fde60d3f542e
Issue #15038 : Fixing the condition broadcast and docs.
Kristján Valur Jónsson <sweskman@gmail.com>
parents:
77518
diff
changeset
|
299 int waiting = cv->waiting; |
|
fde60d3f542e
Issue #15038 : Fixing the condition broadcast and docs.
Kristján Valur Jónsson <sweskman@gmail.com>
parents:
77518
diff
changeset
|
300 if (waiting > 0) { |
|
fde60d3f542e
Issue #15038 : Fixing the condition broadcast and docs.
Kristján Valur Jónsson <sweskman@gmail.com>
parents:
77518
diff
changeset
|
301 cv->waiting = 0; |
|
fde60d3f542e
Issue #15038 : Fixing the condition broadcast and docs.
Kristján Valur Jónsson <sweskman@gmail.com>
parents:
77518
diff
changeset
|
302 return ReleaseSemaphore(cv->sem, waiting, NULL) ? 0 : -1; |
|
77513
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
303 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
304 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
305 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
306 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
307 #else |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
308 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
309 /* Use native Win7 primitives if build target is Win7 or higher */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
310 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
311 /* SRWLOCK is faster and better than CriticalSection */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
312 typedef SRWLOCK PyMUTEX_T; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
313 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
314 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
315 PyMUTEX_INIT(PyMUTEX_T *cs) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
316 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
317 InitializeSRWLock(cs); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
318 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
319 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
320 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
321 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
322 PyMUTEX_FINI(PyMUTEX_T *cs) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
323 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
324 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
325 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
326 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
327 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
328 PyMUTEX_LOCK(PyMUTEX_T *cs) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
329 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
330 AcquireSRWLockExclusive(cs); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
331 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
332 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
333 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
334 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
335 PyMUTEX_UNLOCK(PyMUTEX_T *cs) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
336 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
337 ReleaseSRWLockExclusive(cs); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
338 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
339 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
340 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
341 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
342 typedef CONDITION_VARIABLE PyCOND_T; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
343 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
344 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
345 PyCOND_INIT(PyCOND_T *cv) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
346 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
347 InitializeConditionVariable(cv); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
348 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
349 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
350 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
351 PyCOND_FINI(PyCOND_T *cv) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
352 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
353 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
354 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
355 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
356 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
357 PyCOND_WAIT(PyCOND_T *cv, PyMUTEX_T *cs) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
358 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
359 return SleepConditionVariableSRW(cv, cs, INFINITE, 0) ? 0 : -1; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
360 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
361 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
362 /* This implementation makes no distinction about timeouts. Signal |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
363 * 2 to indicate that we don't know. |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
364 */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
365 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
366 PyCOND_TIMEDWAIT(PyCOND_T *cv, PyMUTEX_T *cs, long us) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
367 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
368 return SleepConditionVariableSRW(cv, cs, us/1000, 0) ? 2 : -1; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
369 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
370 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
371 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
372 PyCOND_SIGNAL(PyCOND_T *cv) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
373 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
374 WakeConditionVariable(cv); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
375 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
376 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
377 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
378 Py_LOCAL_INLINE(int) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
379 PyCOND_BROADCAST(PyCOND_T *cv) |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
380 { |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
381 WakeAllConditionVariable(cv); |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
382 return 0; |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
383 } |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
384 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
385 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
386 #endif /* _PY_EMULATED_WIN_CV */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
387 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
388 #endif /* _POSIX_THREADS, NT_THREADS */ |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
389 |
|
978326f98316
Issue #15038: Optimize python Locks on Windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
diff
changeset
|
390 #endif /* _CONDVAR_H_ */ |
