Mercurial > cpython-lru_cache-weakref
annotate Python/thread_nt.h @ 6139:a287c5014e66 legacy-trunk
Reverse the search order for the Don Beaudry hook so that the first
class wins. Makes more sense.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 22 Aug 1997 16:56:16 +0000 |
| parents | 68f2d7704180 |
| children | 877d23bd5bdc |
| rev | line source |
|---|---|
| 2152 | 1 /*********************************************************** |
| 2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, | |
| 3 The Netherlands. | |
| 4 | |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 2152 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 2152 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
15 permission. |
| 2152 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4371
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 2152 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 32 /* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */ | |
| 33 | |
|
6029
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
34 #include <windows.h> |
|
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
35 #include <limits.h> |
|
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
36 #include <process.h> |
| 2152 | 37 |
| 38 long get_thread_ident(void); | |
| 39 | |
| 40 /* | |
| 41 * Change all headers to pure ANSI as no one will use K&R style on an | |
| 42 * NT | |
| 43 */ | |
| 44 | |
| 45 /* | |
| 46 * Initialization of the C package, should not be needed. | |
| 47 */ | |
| 48 static void _init_thread(void) | |
| 49 { | |
| 50 } | |
| 51 | |
| 52 /* | |
| 53 * Thread support. | |
| 54 */ | |
| 55 int start_new_thread(void (*func)(void *), void *arg) | |
| 56 { | |
|
6029
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
57 long rv; |
|
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
58 int success = 0; |
| 2152 | 59 |
| 60 dprintf(("%ld: start_new_thread called\n", get_thread_ident())); | |
| 61 if (!initialized) | |
| 62 init_thread(); | |
| 63 | |
|
6029
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
64 rv = _beginthread(func, 0, arg); /* use default stack size */ |
| 2152 | 65 |
|
6029
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
66 if (rv != -1) { |
| 2152 | 67 success = 1; |
| 68 dprintf(("%ld: start_new_thread succeeded: %ld\n", get_thread_ident(), aThreadId)); | |
| 69 } | |
| 70 | |
| 71 return success; | |
| 72 } | |
| 73 | |
| 74 /* | |
| 75 * Return the thread Id instead of an handle. The Id is said to uniquely identify the | |
| 76 * thread in the system | |
| 77 */ | |
| 78 long get_thread_ident(void) | |
| 79 { | |
| 80 if (!initialized) | |
| 81 init_thread(); | |
| 82 | |
| 83 return GetCurrentThreadId(); | |
| 84 } | |
| 85 | |
| 86 static void do_exit_thread(int no_cleanup) | |
| 87 { | |
| 88 dprintf(("%ld: exit_thread called\n", get_thread_ident())); | |
| 89 if (!initialized) | |
| 90 if (no_cleanup) | |
| 91 _exit(0); | |
| 92 else | |
| 93 exit(0); | |
|
6029
68f2d7704180
Use _beginthread() and _endthread() in favor of CreateThread() and
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
94 _endthread(); |
| 2152 | 95 } |
| 96 | |
| 97 void exit_thread(void) | |
| 98 { | |
| 99 do_exit_thread(0); | |
| 100 } | |
| 101 | |
| 102 void _exit_thread(void) | |
| 103 { | |
| 104 do_exit_thread(1); | |
| 105 } | |
| 106 | |
| 107 #ifndef NO_EXIT_PROG | |
| 108 static void do_exit_prog(int status, int no_cleanup) | |
| 109 { | |
| 110 dprintf(("exit_prog(%d) called\n", status)); | |
| 111 if (!initialized) | |
| 112 if (no_cleanup) | |
| 113 _exit(status); | |
| 114 else | |
| 115 exit(status); | |
| 116 } | |
| 117 | |
| 118 void exit_prog(int status) | |
| 119 { | |
| 120 do_exit_prog(status, 0); | |
| 121 } | |
| 122 | |
| 123 void _exit_prog _P1(int status) | |
| 124 { | |
| 125 do_exit_prog(status, 1); | |
| 126 } | |
| 127 #endif /* NO_EXIT_PROG */ | |
| 128 | |
| 129 /* | |
| 130 * Lock support. It has too be implemented as semaphores. | |
| 131 * I [Dag] tried to implement it with mutex but I could find a way to | |
| 132 * tell whether a thread already own the lock or not. | |
| 133 */ | |
| 134 type_lock allocate_lock(void) | |
| 135 { | |
| 136 HANDLE aLock; | |
| 137 | |
| 138 dprintf(("allocate_lock called\n")); | |
| 139 if (!initialized) | |
| 140 init_thread(); | |
| 141 | |
| 142 aLock = CreateSemaphore(NULL, /* Security attributes */ | |
| 143 1, /* Initial value */ | |
| 144 1, /* Maximum value */ | |
| 145 NULL); | |
| 146 /* Name of semaphore */ | |
| 147 | |
| 148 dprintf(("%ld: allocate_lock() -> %lx\n", get_thread_ident(), (long)aLock)); | |
| 149 | |
| 150 return (type_lock) aLock; | |
| 151 } | |
| 152 | |
| 153 void free_lock(type_lock aLock) | |
| 154 { | |
| 155 dprintf(("%ld: free_lock(%lx) called\n", get_thread_ident(),(long)aLock)); | |
| 156 | |
| 157 CloseHandle((HANDLE) aLock); | |
| 158 } | |
| 159 | |
| 160 /* | |
| 161 * Return 1 on success if the lock was acquired | |
| 162 * | |
| 163 * and 0 if the lock was not acquired. This means a 0 is returned | |
| 164 * if the lock has already been acquired by this thread! | |
| 165 */ | |
| 166 int acquire_lock(type_lock aLock, int waitflag) | |
| 167 { | |
| 168 int success = 1; | |
| 169 DWORD waitResult; | |
| 170 | |
| 171 dprintf(("%ld: acquire_lock(%lx, %d) called\n", get_thread_ident(),(long)aLock, waitflag)); | |
| 172 | |
| 173 waitResult = WaitForSingleObject((HANDLE) aLock, (waitflag == 1 ? INFINITE : 0)); | |
| 174 | |
| 175 if (waitResult != WAIT_OBJECT_0) { | |
| 176 success = 0; /* We failed */ | |
| 177 } | |
| 178 | |
| 179 dprintf(("%ld: acquire_lock(%lx, %d) -> %d\n", get_thread_ident(),(long)aLock, waitflag, success)); | |
| 180 | |
| 181 return success; | |
| 182 } | |
| 183 | |
| 184 void release_lock(type_lock aLock) | |
| 185 { | |
| 186 dprintf(("%ld: release_lock(%lx) called\n", get_thread_ident(),(long)aLock)); | |
| 187 | |
| 188 if (!ReleaseSemaphore( | |
| 189 (HANDLE) aLock, /* Handle of semaphore */ | |
| 190 1, /* increment count by one */ | |
| 191 NULL)) /* not interested in previous count */ | |
| 192 { | |
| 193 dprintf(("%ld: Could not release_lock(%lx) error: %l\n", get_thread_ident(), (long)aLock, GetLastError())); | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 /* | |
| 198 * Semaphore support. | |
| 199 */ | |
| 200 type_sema allocate_sema(int value) | |
| 201 { | |
| 202 HANDLE aSemaphore; | |
| 203 | |
| 204 dprintf(("%ld: allocate_sema called\n", get_thread_ident())); | |
| 205 if (!initialized) | |
| 206 init_thread(); | |
| 207 | |
| 208 aSemaphore = CreateSemaphore( NULL, /* Security attributes */ | |
| 209 value, /* Initial value */ | |
| 210 INT_MAX, /* Maximum value */ | |
| 211 NULL); /* Name of semaphore */ | |
| 212 | |
| 213 dprintf(("%ld: allocate_sema() -> %lx\n", get_thread_ident(), (long)aSemaphore)); | |
| 214 | |
| 215 return (type_sema) aSemaphore; | |
| 216 } | |
| 217 | |
| 218 void free_sema(type_sema aSemaphore) | |
| 219 { | |
| 220 dprintf(("%ld: free_sema(%lx) called\n", get_thread_ident(), (long)aSemaphore)); | |
| 221 | |
| 222 CloseHandle((HANDLE) aSemaphore); | |
| 223 } | |
| 224 | |
|
4371
c277ee8c669c
Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents:
2152
diff
changeset
|
225 /* |
|
c277ee8c669c
Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents:
2152
diff
changeset
|
226 XXX must do something about waitflag |
|
c277ee8c669c
Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents:
2152
diff
changeset
|
227 */ |
|
c277ee8c669c
Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents:
2152
diff
changeset
|
228 int down_sema(type_sema aSemaphore, int waitflag) |
| 2152 | 229 { |
| 230 DWORD waitResult; | |
| 231 | |
| 232 dprintf(("%ld: down_sema(%lx) called\n", get_thread_ident(), (long)aSemaphore)); | |
| 233 | |
| 234 waitResult = WaitForSingleObject( (HANDLE) aSemaphore, INFINITE); | |
| 235 | |
| 236 dprintf(("%ld: down_sema(%lx) return: %l\n", get_thread_ident(),(long) aSemaphore, waitResult)); | |
|
4371
c277ee8c669c
Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents:
2152
diff
changeset
|
237 return 0; |
| 2152 | 238 } |
| 239 | |
| 240 void up_sema(type_sema aSemaphore) | |
| 241 { | |
| 242 ReleaseSemaphore( | |
| 243 (HANDLE) aSemaphore, /* Handle of semaphore */ | |
| 244 1, /* increment count by one */ | |
| 245 NULL); /* not interested in previous count */ | |
| 246 | |
| 247 dprintf(("%ld: up_sema(%lx)\n", get_thread_ident(), (long)aSemaphore)); | |
| 248 } |
