Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 1 | /* This code implemented by cvale@netcom.com */ |
| 2 | |
| 3 | #define INCL_DOSPROCESS |
| 4 | #define INCL_DOSSEMAPHORES |
| 5 | #include "os2.h" |
| 6 | #include "limits.h" |
| 7 | |
| 8 | #include "process.h" |
| 9 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 10 | #if defined(PYCC_GCC) |
| 11 | #include <sys/builtin.h> |
| 12 | #include <sys/fmutex.h> |
| 13 | #else |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 14 | long PyThread_get_thread_ident(void); |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 15 | #endif |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 16 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 17 | /* default thread stack size of 64kB */ |
Andrew MacIntyre | 2bea474 | 2005-01-17 12:16:36 +0000 | [diff] [blame] | 18 | #if !defined(THREAD_STACK_SIZE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 19 | #define THREAD_STACK_SIZE 0x10000 |
Andrew MacIntyre | 2bea474 | 2005-01-17 12:16:36 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | #define OS2_STACKSIZE(x) (x ? x : THREAD_STACK_SIZE) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 24 | /* |
| 25 | * Initialization of the C package, should not be needed. |
| 26 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 27 | static void |
| 28 | PyThread__init_thread(void) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 29 | { |
| 30 | } |
| 31 | |
| 32 | /* |
| 33 | * Thread support. |
| 34 | */ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 35 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 36 | PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 37 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | int thread_id; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 39 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 40 | thread_id = _beginthread(func, |
| 41 | NULL, |
| 42 | OS2_STACKSIZE(_pythread_stacksize), |
| 43 | arg); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 44 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | if (thread_id == -1) { |
| 46 | dprintf(("_beginthread failed. return %ld\n", errno)); |
| 47 | } |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 48 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | return thread_id; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 52 | long |
| 53 | PyThread_get_thread_ident(void) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 54 | { |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 55 | #if !defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | PPIB pib; |
| 57 | PTIB tib; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 58 | #endif |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 59 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | if (!initialized) |
| 61 | PyThread_init_thread(); |
Andrew MacIntyre | c4c127b | 2002-12-04 12:29:37 +0000 | [diff] [blame] | 62 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 63 | #if defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | return _gettid(); |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 65 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | DosGetInfoBlocks(&tib, &pib); |
| 67 | return tib->tib_ptib2->tib2_ultid; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 68 | #endif |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Amaury Forgeot d'Arc | 72aee3d | 2010-02-24 00:10:48 +0000 | [diff] [blame] | 71 | void |
| 72 | PyThread_exit_thread(void) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 73 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | dprintf(("%ld: PyThread_exit_thread called\n", |
| 75 | PyThread_get_thread_ident())); |
| 76 | if (!initialized) |
| 77 | exit(0); |
| 78 | _endthread(); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 81 | /* |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 82 | * Lock support. This is implemented with an event semaphore and critical |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | * sections to make it behave more like a posix mutex than its OS/2 |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 84 | * counterparts. |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 85 | */ |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 86 | |
| 87 | typedef struct os2_lock_t { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | int is_set; |
| 89 | HEV changed; |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 90 | } *type_os2_lock; |
| 91 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | PyThread_type_lock |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 93 | PyThread_allocate_lock(void) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 94 | { |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 95 | #if defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | _fmutex *sem = malloc(sizeof(_fmutex)); |
| 97 | if (!initialized) |
| 98 | PyThread_init_thread(); |
| 99 | dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", |
| 100 | PyThread_get_thread_ident(), |
| 101 | (long)sem)); |
| 102 | if (_fmutex_create(sem, 0)) { |
| 103 | free(sem); |
| 104 | sem = NULL; |
| 105 | } |
| 106 | return (PyThread_type_lock)sem; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 107 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | APIRET rc; |
| 109 | type_os2_lock lock = (type_os2_lock)malloc(sizeof(struct os2_lock_t)); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | dprintf(("PyThread_allocate_lock called\n")); |
| 112 | if (!initialized) |
| 113 | PyThread_init_thread(); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | lock->is_set = 0; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | DosCreateEventSem(NULL, &lock->changed, 0, 0); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | dprintf(("%ld: PyThread_allocate_lock() -> %p\n", |
| 120 | PyThread_get_thread_ident(), |
| 121 | lock->changed)); |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | return (PyThread_type_lock)lock; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 124 | #endif |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 128 | PyThread_free_lock(PyThread_type_lock aLock) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 129 | { |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 130 | #if !defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | type_os2_lock lock = (type_os2_lock)aLock; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 132 | #endif |
| 133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | dprintf(("%ld: PyThread_free_lock(%p) called\n", |
| 135 | PyThread_get_thread_ident(),aLock)); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 136 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 137 | #if defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | if (aLock) { |
| 139 | _fmutex_close((_fmutex *)aLock); |
| 140 | free((_fmutex *)aLock); |
| 141 | } |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 142 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | DosCloseEventSem(lock->changed); |
| 144 | free(aLock); |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 145 | #endif |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Return 1 on success if the lock was acquired |
| 150 | * |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 151 | * and 0 if the lock was not acquired. |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 152 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 154 | PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 155 | { |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 156 | #if !defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | int done = 0; |
| 158 | ULONG count; |
| 159 | PID pid = 0; |
| 160 | TID tid = 0; |
| 161 | type_os2_lock lock = (type_os2_lock)aLock; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 162 | #endif |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", |
| 165 | PyThread_get_thread_ident(), |
| 166 | aLock, |
| 167 | waitflag)); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 168 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 169 | #if defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | /* always successful if the lock doesn't exist */ |
| 171 | if (aLock && |
| 172 | _fmutex_request((_fmutex *)aLock, waitflag ? 0 : _FMR_NOWAIT)) |
| 173 | return 0; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 174 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | while (!done) { |
| 176 | /* if the lock is currently set, we have to wait for |
| 177 | * the state to change |
| 178 | */ |
| 179 | if (lock->is_set) { |
| 180 | if (!waitflag) |
| 181 | return 0; |
| 182 | DosWaitEventSem(lock->changed, SEM_INDEFINITE_WAIT); |
| 183 | } |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | /* enter a critical section and try to get the semaphore. If |
| 186 | * it is still locked, we will try again. |
| 187 | */ |
| 188 | if (DosEnterCritSec()) |
| 189 | return 0; |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | if (!lock->is_set) { |
| 192 | lock->is_set = 1; |
| 193 | DosResetEventSem(lock->changed, &count); |
| 194 | done = 1; |
| 195 | } |
Andrew MacIntyre | c4c127b | 2002-12-04 12:29:37 +0000 | [diff] [blame] | 196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | DosExitCritSec(); |
| 198 | } |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 199 | #endif |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | return 1; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 204 | void |
| 205 | PyThread_release_lock(PyThread_type_lock aLock) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 206 | { |
Andrew MacIntyre | 6904959 | 2002-12-04 12:27:06 +0000 | [diff] [blame] | 207 | #if !defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | type_os2_lock lock = (type_os2_lock)aLock; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 209 | #endif |
| 210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | dprintf(("%ld: PyThread_release_lock(%p) called\n", |
| 212 | PyThread_get_thread_ident(), |
| 213 | aLock)); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 214 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 215 | #if defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | if (aLock) |
| 217 | _fmutex_release((_fmutex *)aLock); |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 218 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | if (!lock->is_set) { |
| 220 | dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", |
| 221 | PyThread_get_thread_ident(), |
| 222 | aLock, |
| 223 | GetLastError())); |
| 224 | return; |
| 225 | } |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | if (DosEnterCritSec()) { |
| 228 | dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", |
| 229 | PyThread_get_thread_ident(), |
| 230 | aLock, |
| 231 | GetLastError())); |
| 232 | return; |
| 233 | } |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | lock->is_set = 0; |
| 236 | DosPostEventSem(lock->changed); |
Andrew MacIntyre | c4c127b | 2002-12-04 12:29:37 +0000 | [diff] [blame] | 237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | DosExitCritSec(); |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 239 | #endif |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 240 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 241 | |
| 242 | /* minimum/maximum thread stack sizes supported */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | #define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */ |
| 244 | #define THREAD_MAX_STACKSIZE 0x2000000 /* 32MB */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 245 | |
| 246 | /* set the thread stack size. |
| 247 | * Return 0 if size is valid, -1 otherwise. |
| 248 | */ |
| 249 | static int |
| 250 | _pythread_os2_set_stacksize(size_t size) |
| 251 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | /* set to default */ |
| 253 | if (size == 0) { |
| 254 | _pythread_stacksize = 0; |
| 255 | return 0; |
| 256 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | /* valid range? */ |
| 259 | if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) { |
| 260 | _pythread_stacksize = size; |
| 261 | return 0; |
| 262 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | #define THREAD_SET_STACKSIZE(x) _pythread_os2_set_stacksize(x) |