annotate Python/thread_nt.h @ 5758:bfe9ba74c19e legacy-trunk

Fix problem discovered by Greg McFarlane: when an imported module replaces its own entry in sys.module, reference count errors ensue; even if there is no reference count problem, it would be preferable for the import to yield the new thing in sys.modules anyway (if only because that's what later imports will yield). This opens the road to an official hack to implement a __getattr__ like feature for modules: stick an instance in sys.modules[__name__].
author Guido van Rossum <guido@python.org>
date Thu, 10 Jul 1997 18:00:45 +0000
parents 65d2d8e67da3
children 68f2d7704180
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 #include "windows.h"
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 #include "limits.h"
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 long get_thread_ident(void);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
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 * 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
41 * NT
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 */
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 * Initialization of the C package, should not be needed.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 static void _init_thread(void)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 {
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 * Thread support.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 int start_new_thread(void (*func)(void *), void *arg)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 HANDLE aThread;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 DWORD aThreadId;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 int success = 0; /* init not needed when SOLARIS_THREADS and */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 /* C_THREADS implemented properly */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 dprintf(("%ld: start_new_thread called\n", get_thread_ident()));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 init_thread();
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 aThread = CreateThread(
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 NULL, /* No security attributes */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 0, /* use default stack size */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 (LPTHREAD_START_ROUTINE) func, /* thread function */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 (LPVOID) arg, /* argument to thread function */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 0, /* use the default creation flags */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 &aThreadId); /* returns the thread identifier */
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 if (aThread != NULL) {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 CloseHandle(aThread); /* We do not want to have any zombies hanging around */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 success = 1;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 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
77 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 return success;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
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 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
84 * thread in the system
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 long get_thread_ident(void)
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 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 init_thread();
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 return GetCurrentThreadId();
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 static void do_exit_thread(int no_cleanup)
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 dprintf(("%ld: exit_thread called\n", get_thread_ident()));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 if (no_cleanup)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 _exit(0);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 else
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 exit(0);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 ExitThread(0);
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 void exit_thread(void)
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 do_exit_thread(0);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 }
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 void _exit_thread(void)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 do_exit_thread(1);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 #ifndef NO_EXIT_PROG
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 static void do_exit_prog(int status, int no_cleanup)
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 dprintf(("exit_prog(%d) called\n", status));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 if (no_cleanup)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 _exit(status);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 else
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 exit(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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 void exit_prog(int status)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 do_exit_prog(status, 0);
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 void _exit_prog _P1(int status)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 do_exit_prog(status, 1);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 #endif /* NO_EXIT_PROG */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
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 * Lock support. It has too be implemented as semaphores.
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 * 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
140 * tell whether a thread already own the lock or not.
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 type_lock allocate_lock(void)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 HANDLE aLock;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
145
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 dprintf(("allocate_lock called\n"));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 init_thread();
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 aLock = CreateSemaphore(NULL, /* Security attributes */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 1, /* Initial value */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 1, /* Maximum value */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 NULL);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 /* Name of semaphore */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 dprintf(("%ld: allocate_lock() -> %lx\n", get_thread_ident(), (long)aLock));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 return (type_lock) aLock;
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 void free_lock(type_lock aLock)
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 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
164
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 CloseHandle((HANDLE) aLock);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 }
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 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 * Return 1 on success if the lock was acquired
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 * 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
172 * if the lock has already been acquired by this thread!
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 int acquire_lock(type_lock aLock, int waitflag)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 int success = 1;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 DWORD waitResult;
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) called\n", get_thread_ident(),(long)aLock, waitflag));
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 waitResult = WaitForSingleObject((HANDLE) aLock, (waitflag == 1 ? INFINITE : 0));
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 if (waitResult != WAIT_OBJECT_0) {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 success = 0; /* We failed */
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 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
188
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 return success;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 void release_lock(type_lock aLock)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 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
195
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 if (!ReleaseSemaphore(
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 (HANDLE) aLock, /* Handle of semaphore */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 1, /* increment count by one */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 NULL)) /* not interested in previous count */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 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
202 }
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 /*
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 * Semaphore support.
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 type_sema allocate_sema(int value)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 HANDLE aSemaphore;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 dprintf(("%ld: allocate_sema called\n", get_thread_ident()));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 if (!initialized)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 init_thread();
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 aSemaphore = CreateSemaphore( NULL, /* Security attributes */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 value, /* Initial value */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 INT_MAX, /* Maximum value */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 NULL); /* Name of semaphore */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
220
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 dprintf(("%ld: allocate_sema() -> %lx\n", get_thread_ident(), (long)aSemaphore));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 return (type_sema) aSemaphore;
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 }
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 void free_sema(type_sema aSemaphore)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 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
229
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 CloseHandle((HANDLE) aSemaphore);
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
4371
c277ee8c669c Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents: 2152
diff changeset
233 /*
c277ee8c669c Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents: 2152
diff changeset
234 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
235 */
c277ee8c669c Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents: 2152
diff changeset
236 int down_sema(type_sema aSemaphore, int waitflag)
2152
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 DWORD waitResult;
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 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
241
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 waitResult = WaitForSingleObject( (HANDLE) aSemaphore, INFINITE);
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
243
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 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
245 return 0;
2152
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
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 void up_sema(type_sema aSemaphore)
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 {
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 ReleaseSemaphore(
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 (HANDLE) aSemaphore, /* Handle of semaphore */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 1, /* increment count by one */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 NULL); /* not interested in previous count */
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
254
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 dprintf(("%ld: up_sema(%lx)\n", get_thread_ident(), (long)aSemaphore));
1a52351a00b2 nt thread support
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 }