Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 1 | |
| 2 | /* This code implemented by Dag.Gruneau@elsa.preseco.comm.se */ |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 3 | /* Fast NonRecursiveMutex support by Yakov Markovitch, markovitch@iso.ru */ |
Tim Peters | e64ef93 | 2002-02-28 21:34:34 +0000 | [diff] [blame] | 4 | /* Eliminated some memory leaks, gsw@agere.com */ |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 6 | #include <windows.h> |
| 7 | #include <limits.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 9 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 10 | #endif |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 12 | typedef struct NRMUTEX { |
| 13 | LONG owned ; |
| 14 | DWORD thread_id ; |
| 15 | HANDLE hevent ; |
| 16 | } NRMUTEX, *PNRMUTEX ; |
| 17 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 18 | |
| 19 | BOOL |
| 20 | InitializeNonRecursiveMutex(PNRMUTEX mutex) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 21 | { |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 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 */ |
| 26 | } |
| 27 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 28 | VOID |
| 29 | DeleteNonRecursiveMutex(PNRMUTEX mutex) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 30 | { |
| 31 | /* No in-use check */ |
| 32 | CloseHandle(mutex->hevent) ; |
| 33 | mutex->hevent = NULL ; /* Just in case */ |
| 34 | } |
| 35 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 36 | DWORD |
| 37 | EnterNonRecursiveMutex(PNRMUTEX mutex, BOOL wait) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 38 | { |
| 39 | /* Assume that the thread waits successfully */ |
| 40 | DWORD ret ; |
| 41 | |
| 42 | /* InterlockedIncrement(&mutex->owned) == 0 means that no thread currently owns the mutex */ |
| 43 | if (!wait) |
| 44 | { |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 45 | if (InterlockedCompareExchange(&mutex->owned, 0, -1) != -1) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 46 | return WAIT_TIMEOUT ; |
| 47 | ret = WAIT_OBJECT_0 ; |
| 48 | } |
| 49 | else |
| 50 | ret = InterlockedIncrement(&mutex->owned) ? |
| 51 | /* Some thread owns the mutex, let's wait... */ |
| 52 | WaitForSingleObject(mutex->hevent, INFINITE) : WAIT_OBJECT_0 ; |
| 53 | |
| 54 | mutex->thread_id = GetCurrentThreadId() ; /* We own it */ |
| 55 | return ret ; |
| 56 | } |
| 57 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 58 | BOOL |
| 59 | LeaveNonRecursiveMutex(PNRMUTEX mutex) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 60 | { |
| 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 */ |
| 66 | } |
| 67 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 68 | PNRMUTEX |
| 69 | AllocNonRecursiveMutex(void) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 70 | { |
| 71 | PNRMUTEX mutex = (PNRMUTEX)malloc(sizeof(NRMUTEX)) ; |
| 72 | if (mutex && !InitializeNonRecursiveMutex(mutex)) |
| 73 | { |
| 74 | free(mutex) ; |
| 75 | mutex = NULL ; |
| 76 | } |
| 77 | return mutex ; |
| 78 | } |
| 79 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 80 | void |
| 81 | FreeNonRecursiveMutex(PNRMUTEX mutex) |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 82 | { |
| 83 | if (mutex) |
| 84 | { |
| 85 | DeleteNonRecursiveMutex(mutex) ; |
| 86 | free(mutex) ; |
| 87 | } |
| 88 | } |
| 89 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 90 | long PyThread_get_thread_ident(void); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 91 | |
| 92 | /* |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 93 | * Initialization of the C package, should not be needed. |
| 94 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 95 | static void |
| 96 | PyThread__init_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 97 | { |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Thread support. |
| 102 | */ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 103 | |
| 104 | typedef struct { |
| 105 | void (*func)(void*); |
Tim Peters | 2e7e7df | 2003-07-04 04:40:45 +0000 | [diff] [blame] | 106 | void *arg; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 107 | long id; |
| 108 | HANDLE done; |
| 109 | } callobj; |
| 110 | |
| 111 | static int |
| 112 | bootstrap(void *call) |
| 113 | { |
| 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; |
| 118 | |
| 119 | obj->id = PyThread_get_thread_ident(); |
| 120 | ReleaseSemaphore(obj->done, 1, NULL); |
| 121 | func(arg); |
| 122 | return 0; |
| 123 | } |
| 124 | |
Tim Peters | 2e7e7df | 2003-07-04 04:40:45 +0000 | [diff] [blame] | 125 | long |
| 126 | PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 127 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 128 | Py_uintptr_t rv; |
Tim Peters | 2e7e7df | 2003-07-04 04:40:45 +0000 | [diff] [blame] | 129 | callobj obj; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 130 | |
Tim Peters | 2e7e7df | 2003-07-04 04:40:45 +0000 | [diff] [blame] | 131 | dprintf(("%ld: PyThread_start_new_thread called\n", |
| 132 | PyThread_get_thread_ident())); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 133 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 134 | PyThread_init_thread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 135 | |
Tim Peters | 2e7e7df | 2003-07-04 04:40:45 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 142 | |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 143 | rv = _beginthread(bootstrap, |
| 144 | Py_SAFE_DOWNCAST(_pythread_stacksize, |
| 145 | Py_ssize_t, int), |
| 146 | &obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 147 | if (rv == (Py_uintptr_t)-1) { |
Tim Peters | 2e7e7df | 2003-07-04 04:40:45 +0000 | [diff] [blame] | 148 | /* I've seen errno == EAGAIN here, which means "there are |
| 149 | * too many threads". |
| 150 | */ |
| 151 | dprintf(("%ld: PyThread_start_new_thread failed: %p errno %d\n", |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 152 | PyThread_get_thread_ident(), (void*)rv, errno)); |
Tim Peters | 2e7e7df | 2003-07-04 04:40:45 +0000 | [diff] [blame] | 153 | obj.id = -1; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 154 | } |
Tim Peters | 2e7e7df | 2003-07-04 04:40:45 +0000 | [diff] [blame] | 155 | else { |
| 156 | dprintf(("%ld: PyThread_start_new_thread succeeded: %p\n", |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 157 | PyThread_get_thread_ident(), (void*)rv)); |
Tim Peters | 2e7e7df | 2003-07-04 04:40:45 +0000 | [diff] [blame] | 158 | /* wait for thread to initialize, so we can get its id */ |
| 159 | WaitForSingleObject(obj.done, INFINITE); |
| 160 | assert(obj.id != -1); |
| 161 | } |
| 162 | CloseHandle((HANDLE)obj.done); |
| 163 | return obj.id; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Return the thread Id instead of an handle. The Id is said to uniquely identify the |
| 168 | * thread in the system |
| 169 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 170 | long |
| 171 | PyThread_get_thread_ident(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 172 | { |
| 173 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 174 | PyThread_init_thread(); |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 175 | |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 176 | return GetCurrentThreadId(); |
| 177 | } |
| 178 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 179 | static void |
| 180 | do_PyThread_exit_thread(int no_cleanup) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 181 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 182 | dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident())); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 183 | if (!initialized) |
| 184 | if (no_cleanup) |
| 185 | _exit(0); |
| 186 | else |
| 187 | exit(0); |
Guido van Rossum | 49b1226 | 1997-08-14 20:12:58 +0000 | [diff] [blame] | 188 | _endthread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 191 | void |
| 192 | PyThread_exit_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 193 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 194 | do_PyThread_exit_thread(0); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 197 | void |
| 198 | PyThread__exit_thread(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 199 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 200 | do_PyThread_exit_thread(1); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | #ifndef NO_EXIT_PROG |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 204 | static void |
| 205 | do_PyThread_exit_prog(int status, int no_cleanup) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 206 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 207 | dprintf(("PyThread_exit_prog(%d) called\n", status)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 208 | if (!initialized) |
| 209 | if (no_cleanup) |
| 210 | _exit(status); |
| 211 | else |
| 212 | exit(status); |
| 213 | } |
| 214 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 215 | void |
| 216 | PyThread_exit_prog(int status) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 217 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 218 | do_PyThread_exit_prog(status, 0); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 221 | void |
| 222 | PyThread__exit_prog(int status) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 223 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 224 | do_PyThread_exit_prog(status, 1); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 225 | } |
| 226 | #endif /* NO_EXIT_PROG */ |
| 227 | |
| 228 | /* |
| 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. |
| 232 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 233 | PyThread_type_lock |
| 234 | PyThread_allocate_lock(void) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 235 | { |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 236 | PNRMUTEX aLock; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 238 | dprintf(("PyThread_allocate_lock called\n")); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 239 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 240 | PyThread_init_thread(); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 242 | aLock = AllocNonRecursiveMutex() ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 243 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 244 | dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 246 | return (PyThread_type_lock) aLock; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 249 | void |
| 250 | PyThread_free_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 251 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 252 | dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 253 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 254 | FreeNonRecursiveMutex(aLock) ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Return 1 on success if the lock was acquired |
| 259 | * |
| 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! |
| 262 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 263 | int |
| 264 | PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 265 | { |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 266 | int success ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 267 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 268 | dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 269 | |
Georg Brandl | af410b5 | 2005-07-08 22:26:13 +0000 | [diff] [blame] | 270 | success = aLock && EnterNonRecursiveMutex((PNRMUTEX) aLock, (waitflag ? INFINITE : 0)) == WAIT_OBJECT_0 ; |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 271 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 272 | dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 273 | |
| 274 | return success; |
| 275 | } |
| 276 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 277 | void |
| 278 | PyThread_release_lock(PyThread_type_lock aLock) |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 279 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 280 | dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 281 | |
Guido van Rossum | 706262b | 2000-05-04 18:47:15 +0000 | [diff] [blame] | 282 | if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock))) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 283 | dprintf(("%ld: Could not PyThread_release_lock(%p) error: %ld\n", PyThread_get_thread_ident(), aLock, GetLastError())); |
Guido van Rossum | c3f82b6 | 1995-01-17 16:29:31 +0000 | [diff] [blame] | 284 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 285 | |
| 286 | /* minimum/maximum thread stack sizes supported */ |
| 287 | #define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */ |
| 288 | #define THREAD_MAX_STACKSIZE 0x10000000 /* 256MB */ |
| 289 | |
| 290 | /* set the thread stack size. |
| 291 | * Return 0 if size is valid, -1 otherwise. |
| 292 | */ |
| 293 | static int |
| 294 | _pythread_nt_set_stacksize(size_t size) |
| 295 | { |
| 296 | /* set to default */ |
| 297 | if (size == 0) { |
| 298 | _pythread_stacksize = 0; |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /* valid range? */ |
| 303 | if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) { |
| 304 | _pythread_stacksize = size; |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | #define THREAD_SET_STACKSIZE(x) _pythread_nt_set_stacksize(x) |