blob: e16f5d14155d387a84a15453a5cb2e2e6802f0b3 [file] [log] [blame]
Guido van Rossumb738d261999-04-08 13:57:06 +00001
2/* This code implemented by Mark Hammond (MHammond@skippinet.com.au) */
3
4#include <windows.h>
5#include <limits.h>
6#include <pydebug.h>
7
8long PyThread_get_thread_ident(void);
9
10/*
11 * Change all headers to pure ANSI as no one will use K&R style on an
12 * NT
13 */
14
15/*
16 * Initialization of the C package, should not be needed.
17 */
18static void PyThread__init_thread(void)
19{
20}
21
22/*
23 * Thread support.
24 */
Guido van Rossum3c288632001-10-16 21:13:49 +000025long PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossumb738d261999-04-08 13:57:06 +000026{
27 long rv;
Guido van Rossum3c288632001-10-16 21:13:49 +000028 int success = -1;
Guido van Rossumb738d261999-04-08 13:57:06 +000029
30 dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident()));
31 if (!initialized)
32 PyThread_init_thread();
33
34 rv = _beginthread(func, 0, arg); /* use default stack size */
35
36 if (rv != -1) {
Guido van Rossum3c288632001-10-16 21:13:49 +000037 success = 0;
Guido van Rossumb738d261999-04-08 13:57:06 +000038 dprintf(("%ld: PyThread_start_new_thread succeeded:\n", PyThread_get_thread_ident()));
39 }
40
41 return success;
42}
43
44/*
45 * Return the thread Id instead of an handle. The Id is said to uniquely identify the
46 * thread in the system
47 */
48long PyThread_get_thread_ident(void)
49{
50 if (!initialized)
51 PyThread_init_thread();
52
53 return GetCurrentThreadId();
54}
55
56static void do_PyThread_exit_thread(int no_cleanup)
57{
58 dprintf(("%ld: do_PyThread_exit_thread called\n", PyThread_get_thread_ident()));
59 if (!initialized)
60 if (no_cleanup)
61 exit(0); /* XXX - was _exit()!! */
62 else
63 exit(0);
64 _endthread();
65}
66
67void PyThread_exit_thread(void)
68{
69 do_PyThread_exit_thread(0);
70}
71
72void PyThread__exit_thread(void)
73{
74 do_PyThread_exit_thread(1);
75}
76
77#ifndef NO_EXIT_PROG
78static void do_PyThread_exit_prog(int status, int no_cleanup)
79{
80 dprintf(("PyThread_exit_prog(%d) called\n", status));
81 if (!initialized)
82 if (no_cleanup)
83 _exit(status);
84 else
85 exit(status);
86}
87
88void PyThread_exit_prog(int status)
89{
90 do_PyThread_exit_prog(status, 0);
91}
92
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000093void PyThread__exit_prog(int status)
Guido van Rossumb738d261999-04-08 13:57:06 +000094{
95 do_PyThread_exit_prog(status, 1);
96}
97#endif /* NO_EXIT_PROG */
98
99/*
100 * Lock support. It has to be implemented using Mutexes, as
101 * CE doesnt support semaphores. Therefore we use some hacks to
102 * simulate the non reentrant requirements of Python locks
103 */
104PyThread_type_lock PyThread_allocate_lock(void)
105{
106 HANDLE aLock;
107
108 dprintf(("PyThread_allocate_lock called\n"));
109 if (!initialized)
110 PyThread_init_thread();
111
112 aLock = CreateEvent(NULL, /* Security attributes */
113 0, /* Manual-Reset */
114 1, /* Is initially signalled */
115 NULL); /* Name of event */
116
Fred Drakea44d3532000-06-30 15:01:00 +0000117 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
Guido van Rossumb738d261999-04-08 13:57:06 +0000118
119 return (PyThread_type_lock) aLock;
120}
121
122void PyThread_free_lock(PyThread_type_lock aLock)
123{
Fred Drakea44d3532000-06-30 15:01:00 +0000124 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumb738d261999-04-08 13:57:06 +0000125
126 CloseHandle(aLock);
127}
128
129/*
130 * Return 1 on success if the lock was acquired
131 *
132 * and 0 if the lock was not acquired. This means a 0 is returned
133 * if the lock has already been acquired by this thread!
134 */
135int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
136{
137 int success = 1;
138 DWORD waitResult;
139
Fred Drakea44d3532000-06-30 15:01:00 +0000140 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag));
Guido van Rossumb738d261999-04-08 13:57:06 +0000141
142#ifndef DEBUG
Georg Brandl9a3240e2005-07-09 15:26:33 +0000143 waitResult = WaitForSingleObject(aLock, (waitflag ? INFINITE : 0));
Guido van Rossumb738d261999-04-08 13:57:06 +0000144#else
145 /* To aid in debugging, we regularly wake up. This allows us to
146 break into the debugger */
147 while (TRUE) {
148 waitResult = WaitForSingleObject(aLock, waitflag ? 3000 : 0);
Georg Brandl9a3240e2005-07-09 15:26:33 +0000149 if (waitflag==0 || (waitflag && waitResult == WAIT_OBJECT_0))
Guido van Rossumb738d261999-04-08 13:57:06 +0000150 break;
151 }
152#endif
153
154 if (waitResult != WAIT_OBJECT_0) {
155 success = 0; /* We failed */
156 }
157
Fred Drakea44d3532000-06-30 15:01:00 +0000158 dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success));
Guido van Rossumb738d261999-04-08 13:57:06 +0000159
160 return success;
161}
162
163void PyThread_release_lock(PyThread_type_lock aLock)
164{
Fred Drakea44d3532000-06-30 15:01:00 +0000165 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossumb738d261999-04-08 13:57:06 +0000166
167 if (!SetEvent(aLock))
Fred Drakea44d3532000-06-30 15:01:00 +0000168 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", PyThread_get_thread_ident(), aLock, GetLastError()));
Guido van Rossumb738d261999-04-08 13:57:06 +0000169}
170
171