Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 1 | |
| 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 | |
| 8 | long 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 | */ |
| 18 | static void PyThread__init_thread(void) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | /* |
| 23 | * Thread support. |
| 24 | */ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 25 | long PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 26 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 27 | long rv; |
| 28 | int success = -1; |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 29 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 30 | dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident())); |
| 31 | if (!initialized) |
| 32 | PyThread_init_thread(); |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 33 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 34 | rv = _beginthread(func, 0, arg); /* use default stack size */ |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 35 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 36 | if (rv != -1) { |
| 37 | success = 0; |
| 38 | dprintf(("%ld: PyThread_start_new_thread succeeded:\n", PyThread_get_thread_ident())); |
| 39 | } |
| 40 | |
| 41 | return success; |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /* |
Serhiy Storchaka | 9a118f1 | 2016-04-17 09:37:36 +0300 | [diff] [blame] | 45 | * Return the thread Id instead of a handle. The Id is said to uniquely identify the |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 46 | * thread in the system |
| 47 | */ |
| 48 | long PyThread_get_thread_ident(void) |
| 49 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 50 | if (!initialized) |
| 51 | PyThread_init_thread(); |
| 52 | |
| 53 | return GetCurrentThreadId(); |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 56 | void PyThread_exit_thread(void) |
| 57 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 58 | dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident())); |
| 59 | if (!initialized) |
| 60 | exit(0); |
| 61 | _endthread(); |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 64 | /* |
| 65 | * Lock support. It has to be implemented using Mutexes, as |
| 66 | * CE doesnt support semaphores. Therefore we use some hacks to |
| 67 | * simulate the non reentrant requirements of Python locks |
| 68 | */ |
| 69 | PyThread_type_lock PyThread_allocate_lock(void) |
| 70 | { |
| 71 | HANDLE aLock; |
| 72 | |
| 73 | dprintf(("PyThread_allocate_lock called\n")); |
| 74 | if (!initialized) |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 75 | PyThread_init_thread(); |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 76 | |
| 77 | aLock = CreateEvent(NULL, /* Security attributes */ |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 78 | 0, /* Manual-Reset */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 79 | 1, /* Is initially signalled */ |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 80 | NULL); /* Name of event */ |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 81 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 82 | dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock)); |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 83 | |
| 84 | return (PyThread_type_lock) aLock; |
| 85 | } |
| 86 | |
| 87 | void PyThread_free_lock(PyThread_type_lock aLock) |
| 88 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 89 | dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 90 | |
| 91 | CloseHandle(aLock); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Return 1 on success if the lock was acquired |
| 96 | * |
| 97 | * and 0 if the lock was not acquired. This means a 0 is returned |
| 98 | * if the lock has already been acquired by this thread! |
| 99 | */ |
| 100 | int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) |
| 101 | { |
| 102 | int success = 1; |
| 103 | DWORD waitResult; |
| 104 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 105 | dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag)); |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 106 | |
| 107 | #ifndef DEBUG |
Georg Brandl | 9a3240e | 2005-07-09 15:26:33 +0000 | [diff] [blame] | 108 | waitResult = WaitForSingleObject(aLock, (waitflag ? INFINITE : 0)); |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 109 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 110 | /* To aid in debugging, we regularly wake up. This allows us to |
| 111 | break into the debugger */ |
| 112 | while (TRUE) { |
| 113 | waitResult = WaitForSingleObject(aLock, waitflag ? 3000 : 0); |
| 114 | if (waitflag==0 || (waitflag && waitResult == WAIT_OBJECT_0)) |
| 115 | break; |
| 116 | } |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 117 | #endif |
| 118 | |
| 119 | if (waitResult != WAIT_OBJECT_0) { |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 120 | success = 0; /* We failed */ |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 123 | dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success)); |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 124 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 125 | return success; |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void PyThread_release_lock(PyThread_type_lock aLock) |
| 129 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 130 | dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 131 | |
| 132 | if (!SetEvent(aLock)) |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 133 | dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", PyThread_get_thread_ident(), aLock, GetLastError())); |
Guido van Rossum | b738d26 | 1999-04-08 13:57:06 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |