Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
| 16 | |
| 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
| 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* This code implemented by Mark Hammond (MHammond@skippinet.com.au) */ |
| 33 | |
| 34 | #include <windows.h> |
| 35 | #include <limits.h> |
| 36 | #include <pydebug.h> |
| 37 | |
| 38 | long PyThread_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 PyThread__init_thread(void) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Thread support. |
| 54 | */ |
| 55 | int PyThread_start_new_thread(void (*func)(void *), void *arg) |
| 56 | { |
| 57 | long rv; |
| 58 | int success = 0; |
| 59 | |
| 60 | dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident())); |
| 61 | if (!initialized) |
| 62 | PyThread_init_thread(); |
| 63 | |
| 64 | rv = _beginthread(func, 0, arg); /* use default stack size */ |
| 65 | |
| 66 | if (rv != -1) { |
| 67 | success = 1; |
| 68 | dprintf(("%ld: PyThread_start_new_thread succeeded:\n", PyThread_get_thread_ident())); |
| 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 PyThread_get_thread_ident(void) |
| 79 | { |
| 80 | if (!initialized) |
| 81 | PyThread_init_thread(); |
| 82 | |
| 83 | return GetCurrentThreadId(); |
| 84 | } |
| 85 | |
| 86 | static void do_PyThread_exit_thread(int no_cleanup) |
| 87 | { |
| 88 | dprintf(("%ld: do_PyThread_exit_thread called\n", PyThread_get_thread_ident())); |
| 89 | if (!initialized) |
| 90 | if (no_cleanup) |
| 91 | exit(0); /* XXX - was _exit()!! */ |
| 92 | else |
| 93 | exit(0); |
| 94 | _endthread(); |
| 95 | } |
| 96 | |
| 97 | void PyThread_exit_thread(void) |
| 98 | { |
| 99 | do_PyThread_exit_thread(0); |
| 100 | } |
| 101 | |
| 102 | void PyThread__exit_thread(void) |
| 103 | { |
| 104 | do_PyThread_exit_thread(1); |
| 105 | } |
| 106 | |
| 107 | #ifndef NO_EXIT_PROG |
| 108 | static void do_PyThread_exit_prog(int status, int no_cleanup) |
| 109 | { |
| 110 | dprintf(("PyThread_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 PyThread_exit_prog(int status) |
| 119 | { |
| 120 | do_PyThread_exit_prog(status, 0); |
| 121 | } |
| 122 | |
| 123 | void PyThread__exit_prog _P1(int status) |
| 124 | { |
| 125 | do_PyThread_exit_prog(status, 1); |
| 126 | } |
| 127 | #endif /* NO_EXIT_PROG */ |
| 128 | |
| 129 | /* |
| 130 | * Lock support. It has to be implemented using Mutexes, as |
| 131 | * CE doesnt support semaphores. Therefore we use some hacks to |
| 132 | * simulate the non reentrant requirements of Python locks |
| 133 | */ |
| 134 | PyThread_type_lock PyThread_allocate_lock(void) |
| 135 | { |
| 136 | HANDLE aLock; |
| 137 | |
| 138 | dprintf(("PyThread_allocate_lock called\n")); |
| 139 | if (!initialized) |
| 140 | PyThread_init_thread(); |
| 141 | |
| 142 | aLock = CreateEvent(NULL, /* Security attributes */ |
| 143 | 0, /* Manual-Reset */ |
| 144 | 1, /* Is initially signalled */ |
| 145 | NULL); /* Name of event */ |
| 146 | |
| 147 | dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", PyThread_get_thread_ident(), (long)aLock)); |
| 148 | |
| 149 | return (PyThread_type_lock) aLock; |
| 150 | } |
| 151 | |
| 152 | void PyThread_free_lock(PyThread_type_lock aLock) |
| 153 | { |
| 154 | dprintf(("%ld: PyThread_free_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock)); |
| 155 | |
| 156 | CloseHandle(aLock); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Return 1 on success if the lock was acquired |
| 161 | * |
| 162 | * and 0 if the lock was not acquired. This means a 0 is returned |
| 163 | * if the lock has already been acquired by this thread! |
| 164 | */ |
| 165 | int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) |
| 166 | { |
| 167 | int success = 1; |
| 168 | DWORD waitResult; |
| 169 | |
| 170 | dprintf(("%ld: PyThread_acquire_lock(%lx, %d) called\n", PyThread_get_thread_ident(),(long)aLock, waitflag)); |
| 171 | |
| 172 | #ifndef DEBUG |
| 173 | waitResult = WaitForSingleObject(aLock, (waitflag == 1 ? INFINITE : 0)); |
| 174 | #else |
| 175 | /* To aid in debugging, we regularly wake up. This allows us to |
| 176 | break into the debugger */ |
| 177 | while (TRUE) { |
| 178 | waitResult = WaitForSingleObject(aLock, waitflag ? 3000 : 0); |
| 179 | if (waitflag==0 || (waitflag==1 && waitResult == WAIT_OBJECT_0)) |
| 180 | break; |
| 181 | } |
| 182 | #endif |
| 183 | |
| 184 | if (waitResult != WAIT_OBJECT_0) { |
| 185 | success = 0; /* We failed */ |
| 186 | } |
| 187 | |
| 188 | dprintf(("%ld: PyThread_acquire_lock(%lx, %d) -> %d\n", PyThread_get_thread_ident(),(long)aLock, waitflag, success)); |
| 189 | |
| 190 | return success; |
| 191 | } |
| 192 | |
| 193 | void PyThread_release_lock(PyThread_type_lock aLock) |
| 194 | { |
| 195 | dprintf(("%ld: PyThread_release_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock)); |
| 196 | |
| 197 | if (!SetEvent(aLock)) |
| 198 | dprintf(("%ld: Could not PyThread_release_lock(%lx) error: %l\n", PyThread_get_thread_ident(), (long)aLock, GetLastError())); |
| 199 | } |
| 200 | |
| 201 | |