annotate Python/thread_nt.h @ 8358:da4bdceeaad7 legacy-trunk

Another veeeeeery old patch... Date: Thu, 14 Sep 1995 12:18:20 -0400 From: Alan Morse <alan@dvcorp.com> To: python-list@cwi.nl Subject: getargs bug in 1.2 and 1.3 BETA We have found a bug in the part of the getargs code that we added and submitted, and which was incorporated into 1.1. The parsing of "O?" format specifiers is not handled correctly; there is no "else" for the "if" and therefore it can never fail. What's worse, the advancing of the varargs pointer is not handled properly, so from then on it is out of sync, wreaking all sorts of havoc. (If it had failed properly, then the out-of-sync varargs would not have been an issue.) Below is the context diff for the change. Note that I have made a few stylistic changes beyond adding the else case, namely: 1) Making the "O" case follow the convention established by the other format specifiers of getting all their vararg arguments before performing the test, rather than getting some before and some after the test passes. 2) Making the logic of the tests parallel, so the "if" part indicates that the format is accepted and the "else" part indicates that the format has failed. They were inconsistent with each other and with the the other format specifiers. -Alan Morse (amorse@dvcorp.com)
author Guido van Rossum <guido@python.org>
date Fri, 15 May 1998 22:04:07 +0000
parents 68f2d7704180
children 877d23bd5bdc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /***********************************************************
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 The Netherlands.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 ******************************************************************/
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 long get_thread_ident(void);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 * Change all headers to pure ANSI as no one will use K&R style on an
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 * NT
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 * Initialization of the C package, should not be needed.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 static void _init_thread(void)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 * Thread support.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 int start_new_thread(void (*func)(void *), void *arg)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 dprintf(("%ld: start_new_thread called\n", get_thread_ident()));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 init_thread();
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 success = 1;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 dprintf(("%ld: start_new_thread succeeded: %ld\n", get_thread_ident(), aThreadId));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 return success;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 * Return the thread Id instead of an handle. The Id is said to uniquely identify the
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 * thread in the system
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 long get_thread_ident(void)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 init_thread();
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 return GetCurrentThreadId();
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 static void do_exit_thread(int no_cleanup)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 dprintf(("%ld: exit_thread called\n", get_thread_ident()));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 if (no_cleanup)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 _exit(0);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 else
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 void exit_thread(void)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 do_exit_thread(0);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 void _exit_thread(void)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 do_exit_thread(1);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 #ifndef NO_EXIT_PROG
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 static void do_exit_prog(int status, int no_cleanup)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 dprintf(("exit_prog(%d) called\n", status));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 if (no_cleanup)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 _exit(status);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 else
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 exit(status);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 void exit_prog(int status)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 do_exit_prog(status, 0);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 void _exit_prog _P1(int status)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 do_exit_prog(status, 1);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 #endif /* NO_EXIT_PROG */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 * Lock support. It has too be implemented as semaphores.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 * I [Dag] tried to implement it with mutex but I could find a way to
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 * tell whether a thread already own the lock or not.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 type_lock allocate_lock(void)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 HANDLE aLock;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 dprintf(("allocate_lock called\n"));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 init_thread();
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 aLock = CreateSemaphore(NULL, /* Security attributes */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 1, /* Initial value */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 1, /* Maximum value */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 NULL);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 /* Name of semaphore */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 dprintf(("%ld: allocate_lock() -> %lx\n", get_thread_ident(), (long)aLock));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 return (type_lock) aLock;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 void free_lock(type_lock aLock)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 dprintf(("%ld: free_lock(%lx) called\n", get_thread_ident(),(long)aLock));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 CloseHandle((HANDLE) aLock);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 * Return 1 on success if the lock was acquired
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 *
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 * and 0 if the lock was not acquired. This means a 0 is returned
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 * if the lock has already been acquired by this thread!
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 int acquire_lock(type_lock aLock, int waitflag)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 int success = 1;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 DWORD waitResult;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 dprintf(("%ld: acquire_lock(%lx, %d) called\n", get_thread_ident(),(long)aLock, waitflag));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 waitResult = WaitForSingleObject((HANDLE) aLock, (waitflag == 1 ? INFINITE : 0));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
174
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 if (waitResult != WAIT_OBJECT_0) {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 success = 0; /* We failed */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 dprintf(("%ld: acquire_lock(%lx, %d) -> %d\n", get_thread_ident(),(long)aLock, waitflag, success));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 return success;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 void release_lock(type_lock aLock)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 dprintf(("%ld: release_lock(%lx) called\n", get_thread_ident(),(long)aLock));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 if (!ReleaseSemaphore(
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 (HANDLE) aLock, /* Handle of semaphore */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 1, /* increment count by one */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 NULL)) /* not interested in previous count */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 dprintf(("%ld: Could not release_lock(%lx) error: %l\n", get_thread_ident(), (long)aLock, GetLastError()));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 * Semaphore support.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 type_sema allocate_sema(int value)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 HANDLE aSemaphore;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
203
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 dprintf(("%ld: allocate_sema called\n", get_thread_ident()));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 init_thread();
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 aSemaphore = CreateSemaphore( NULL, /* Security attributes */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 value, /* Initial value */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 INT_MAX, /* Maximum value */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 NULL); /* Name of semaphore */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
212
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 dprintf(("%ld: allocate_sema() -> %lx\n", get_thread_ident(), (long)aSemaphore));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
214
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 return (type_sema) aSemaphore;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 void free_sema(type_sema aSemaphore)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 dprintf(("%ld: free_sema(%lx) called\n", get_thread_ident(), (long)aSemaphore));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 CloseHandle((HANDLE) aSemaphore);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 DWORD waitResult;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 dprintf(("%ld: down_sema(%lx) called\n", get_thread_ident(), (long)aSemaphore));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
233
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 waitResult = WaitForSingleObject( (HANDLE) aSemaphore, INFINITE);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
235
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 void up_sema(type_sema aSemaphore)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 ReleaseSemaphore(
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 (HANDLE) aSemaphore, /* Handle of semaphore */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 1, /* increment count by one */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 NULL); /* not interested in previous count */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
246
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 dprintf(("%ld: up_sema(%lx)\n", get_thread_ident(), (long)aSemaphore));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 }