Updated to reflect change in logging.config to remove out-of-date comment in _install...
[python.git] / Python / thread_nt.h
blob944552992f4f442d8d445a5f953b9bb96ff3b6c9
2 /* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */
3 /* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */
4 /* Eliminated some memory leaks, gsw@agere.com */
6 #include <windows.h>
7 #include <limits.h>
8 #ifdef HAVE_PROCESS_H
9 #include <process.h>
10 #endif
12 typedef struct NRMUTEX {
13 LONG owned ;
14 DWORD thread_id ;
15 HANDLE hevent ;
16 } NRMUTEX, *PNRMUTEX ;
19 BOOL
20 InitializeNonRecursiveMutex(PNRMUTEX mutex)
22 mutex->owned = -1 ; /* No threads have entered NonRecursiveMutex */
23 mutex->thread_id = 0 ;
24 mutex->hevent = CreateEvent(NULL, FALSE, FALSE, NULL) ;
25 return mutex->hevent != NULL ; /* TRUE if the mutex is created */
28 VOID
29 DeleteNonRecursiveMutex(PNRMUTEX mutex)
31 /* No in-use check */
32 CloseHandle(mutex->hevent) ;
33 mutex->hevent = NULL ; /* Just in case */
36 DWORD
37 EnterNonRecursiveMutex(PNRMUTEX mutex, BOOL wait)
39 /* Assume that the thread waits successfully */
40 DWORD ret ;
42 /* InterlockedIncrement(&mutex->owned) == 0 means that no thread currently owns the mutex */
43 if (!wait)
45 if (InterlockedCompareExchange(&mutex->owned, 0, -1) != -1)
46 return WAIT_TIMEOUT ;
47 ret = WAIT_OBJECT_0 ;
49 else
50 ret = InterlockedIncrement(&mutex->owned) ?
51 /* Some thread owns the mutex, let's wait... */
52 WaitForSingleObject(mutex->hevent, INFINITE) : WAIT_OBJECT_0 ;
54 mutex->thread_id = GetCurrentThreadId() ; /* We own it */
55 return ret ;
58 BOOL
59 LeaveNonRecursiveMutex(PNRMUTEX mutex)
61 /* We don't own the mutex */
62 mutex->thread_id = 0 ;
63 return
64 InterlockedDecrement(&mutex->owned) < 0 ||
65 SetEvent(mutex->hevent) ; /* Other threads are waiting, wake one on them up */
68 PNRMUTEX
69 AllocNonRecursiveMutex(void)
71 PNRMUTEX mutex = (PNRMUTEX)malloc(sizeof(NRMUTEX)) ;
72 if (mutex && !InitializeNonRecursiveMutex(mutex))
74 free(mutex) ;
75 mutex = NULL ;
77 return mutex ;
80 void
81 FreeNonRecursiveMutex(PNRMUTEX mutex)
83 if (mutex)
85 DeleteNonRecursiveMutex(mutex) ;
86 free(mutex) ;
90 long PyThread_get_thread_ident(void);
93 * Initialization of the C package, should not be needed.
95 static void
96 PyThread__init_thread(void)
101 * Thread support.
104 typedef struct {
105 void (*func)(void*);
106 void *arg;
107 long id;
108 HANDLE done;
109 } callobj;
111 static int
112 bootstrap(void *call)
114 callobj *obj = (callobj*)call;
115 /* copy callobj since other thread might free it before we're done */
116 void (*func)(void*) = obj->func;
117 void *arg = obj->arg;
119 obj->id = PyThread_get_thread_ident();
120 ReleaseSemaphore(obj->done, 1, NULL);
121 func(arg);
122 return 0;
125 long
126 PyThread_start_new_thread(void (*func)(void *), void *arg)
128 Py_uintptr_t rv;
129 callobj obj;
131 dprintf(("%ld: PyThread_start_new_thread called\n",
132 PyThread_get_thread_ident()));
133 if (!initialized)
134 PyThread_init_thread();
136 obj.id = -1; /* guilty until proved innocent */
137 obj.func = func;
138 obj.arg = arg;
139 obj.done = CreateSemaphore(NULL, 0, 1, NULL);
140 if (obj.done == NULL)
141 return -1;
143 rv = _beginthread(bootstrap,
144 Py_SAFE_DOWNCAST(_pythread_stacksize,
145 Py_ssize_t, int),
146 &obj);
147 if (rv == (Py_uintptr_t)-1) {
148 /* I've seen errno == EAGAIN here, which means "there are
149 * too many threads".
151 dprintf(("%ld: PyThread_start_new_thread failed: %p errno %d\n",
152 PyThread_get_thread_ident(), (void*)rv, errno));
153 obj.id = -1;
155 else {
156 dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n",
157 PyThread_get_thread_ident(), (void*)rv));
158 /* wait for thread to initialize, so we can get its id */
159 WaitForSingleObject(obj.done, INFINITE);
160 assert(obj.id != -1);
162 CloseHandle((HANDLE)obj.done);
163 return obj.id;
167 * Return the thread Id instead of an handle. The Id is said to uniquely identify the
168 * thread in the system
170 long
171 PyThread_get_thread_ident(void)
173 if (!initialized)
174 PyThread_init_thread();
176 return GetCurrentThreadId();
179 static void
180 do_PyThread_exit_thread(int no_cleanup)
182 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
183 if (!initialized)
184 if (no_cleanup)
185 _exit(0);
186 else
187 exit(0);
188 _endthread();
191 void
192 PyThread_exit_thread(void)
194 do_PyThread_exit_thread(0);
197 void
198 PyThread__exit_thread(void)
200 do_PyThread_exit_thread(1);
203 #ifndef NO_EXIT_PROG
204 static void
205 do_PyThread_exit_prog(int status, int no_cleanup)
207 dprintf(("PyThread_exit_prog(%d) called\n", status));
208 if (!initialized)
209 if (no_cleanup)
210 _exit(status);
211 else
212 exit(status);
215 void
216 PyThread_exit_prog(int status)
218 do_PyThread_exit_prog(status, 0);
221 void
222 PyThread__exit_prog(int status)
224 do_PyThread_exit_prog(status, 1);
226 #endif /* NO_EXIT_PROG */
229 * Lock support. It has too be implemented as semaphores.
230 * I [Dag] tried to implement it with mutex but I could find a way to
231 * tell whether a thread already own the lock or not.
233 PyThread_type_lock
234 PyThread_allocate_lock(void)
236 PNRMUTEX aLock;
238 dprintf(("PyThread_allocate_lock called\n"));
239 if (!initialized)
240 PyThread_init_thread();
242 aLock = AllocNonRecursiveMutex() ;
244 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
246 return (PyThread_type_lock) aLock;
249 void
250 PyThread_free_lock(PyThread_type_lock aLock)
252 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
254 FreeNonRecursiveMutex(aLock) ;
258 * Return 1 on success if the lock was acquired
260 * and 0 if the lock was not acquired. This means a 0 is returned
261 * if the lock has already been acquired by this thread!
264 PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
266 int success ;
268 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag));
270 success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag ? INFINITE : 0)) == WAIT_OBJECT_0 ;
272 dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success));
274 return success;
277 void
278 PyThread_release_lock(PyThread_type_lock aLock)
280 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
282 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
283 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %ld\n", PyThread_get_thread_ident(), aLock, GetLastError()));
286 /* minimum/maximum thread stack sizes supported */
287 #define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */
288 #define THREAD_MAX_STACKSIZE 0x10000000 /* 256MB */
290 /* set the thread stack size.
291 * Return 0 if size is valid, -1 otherwise.
293 static int
294 _pythread_nt_set_stacksize(size_t size)
296 /* set to default */
297 if (size == 0) {
298 _pythread_stacksize = 0;
299 return 0;
302 /* valid range? */
303 if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
304 _pythread_stacksize = size;
305 return 0;
308 return -1;
311 #define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x)