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 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 10 | long PyThread_get_thread_ident(void); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 11 | |
| 12 | |
| 13 | /* |
| 14 | * Initialization of the C package, should not be needed. |
| 15 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 16 | static void |
| 17 | PyThread__init_thread(void) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 18 | { |
| 19 | } |
| 20 | |
| 21 | /* |
| 22 | * Thread support. |
| 23 | */ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 24 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 25 | PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 26 | { |
| 27 | int aThread; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 28 | int success = 0; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 30 | aThread = _beginthread(func,NULL,65536,arg); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 31 | |
| 32 | if( aThread == -1 ) { |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 33 | success = -1; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 34 | fprintf(stderr,"aThread failed == %d",aThread); |
| 35 | dprintf(("_beginthread failed. return %ld\n", errno)); |
| 36 | } |
| 37 | |
| 38 | return success; |
| 39 | } |
| 40 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 41 | long |
| 42 | PyThread_get_thread_ident(void) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 43 | { |
| 44 | PPIB pib; |
| 45 | PTIB tib; |
| 46 | |
| 47 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 48 | PyThread_init_thread(); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 49 | |
| 50 | DosGetInfoBlocks(&tib,&pib); |
| 51 | return tib->tib_ptib2->tib2_ultid; |
| 52 | } |
| 53 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 54 | static void |
| 55 | do_PyThread_exit_thread(int no_cleanup) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 56 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 57 | dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident())); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 58 | if (!initialized) |
| 59 | if (no_cleanup) |
| 60 | _exit(0); |
| 61 | else |
| 62 | exit(0); |
| 63 | _endthread(); |
| 64 | } |
| 65 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 66 | void |
| 67 | PyThread_exit_thread(void) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 68 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 69 | do_PyThread_exit_thread(0); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 72 | void |
| 73 | PyThread__exit_thread(void) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 74 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 75 | do_PyThread_exit_thread(1); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | #ifndef NO_EXIT_PROG |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 79 | static void |
| 80 | do_PyThread_exit_prog(int status, int no_cleanup) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 81 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 82 | dprintf(("PyThread_exit_prog(%d) called\n", status)); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 83 | if (!initialized) |
| 84 | if (no_cleanup) |
| 85 | _exit(status); |
| 86 | else |
| 87 | exit(status); |
| 88 | } |
| 89 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 90 | void |
| 91 | PyThread_exit_prog(int status) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 92 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 93 | do_PyThread_exit_prog(status, 0); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 96 | void |
| 97 | PyThread__exit_prog(int status) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 98 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 99 | do_PyThread_exit_prog(status, 1); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 100 | } |
| 101 | #endif /* NO_EXIT_PROG */ |
| 102 | |
| 103 | /* |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 104 | * Lock support. This is implemented with an event semaphore and critical |
| 105 | * sections to make it behave more like a posix mutex than its OS/2 |
| 106 | # counterparts. |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 107 | */ |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 108 | |
| 109 | typedef struct os2_lock_t { |
| 110 | int is_set; |
| 111 | HEV changed; |
| 112 | } *type_os2_lock; |
| 113 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 114 | PyThread_type_lock |
| 115 | PyThread_allocate_lock(void) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 116 | { |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 117 | APIRET rc; |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 118 | 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] | 119 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 120 | dprintf(("PyThread_allocate_lock called\n")); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 121 | if (!initialized) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 122 | PyThread_init_thread(); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 123 | |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 124 | lock->is_set = 0; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 125 | |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 126 | DosCreateEventSem(NULL, &lock->changed, 0, 0); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 127 | |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 128 | dprintf(("%ld: PyThread_allocate_lock() -> %p\n", |
| 129 | PyThread_get_thread_ident(), |
| 130 | lock->changed)); |
| 131 | |
| 132 | return (PyThread_type_lock) lock; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 135 | void |
| 136 | PyThread_free_lock(PyThread_type_lock aLock) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 137 | { |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 138 | type_os2_lock lock = (type_os2_lock)aLock; |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 139 | dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 140 | |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 141 | DosCloseEventSem(lock->changed); |
| 142 | free(aLock); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Return 1 on success if the lock was acquired |
| 147 | * |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 148 | * and 0 if the lock was not acquired. |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 149 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 150 | int |
| 151 | PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 152 | { |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 153 | int done = 0; |
| 154 | ULONG count; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 155 | PID pid = 0; |
| 156 | TID tid = 0; |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 157 | type_os2_lock lock = (type_os2_lock)aLock; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 158 | |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 159 | dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(), |
| 160 | aLock, waitflag)); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 161 | |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 162 | while (!done) { |
| 163 | /* if the lock is currently set, we have to wait for the state to change */ |
| 164 | if (lock->is_set) { |
| 165 | if (!waitflag) |
| 166 | return 0; |
| 167 | DosWaitEventSem(lock->changed, SEM_INDEFINITE_WAIT); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 168 | } |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * enter a critical section and try to get the semaphore. If |
| 172 | * it is still locked, we will try again. |
| 173 | */ |
| 174 | if (DosEnterCritSec()) |
| 175 | return 0; |
| 176 | |
| 177 | if (!lock->is_set) { |
| 178 | lock->is_set = 1; |
| 179 | DosResetEventSem(lock->changed, &count); |
| 180 | done = 1; |
| 181 | } |
| 182 | |
| 183 | DosExitCritSec(); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 186 | return 1; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 189 | void PyThread_release_lock(PyThread_type_lock aLock) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 190 | { |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 191 | type_os2_lock lock = (type_os2_lock)aLock; |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 192 | dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 193 | |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 194 | if (!lock->is_set) { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 195 | dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", |
| 196 | PyThread_get_thread_ident(), aLock, GetLastError())); |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 197 | return; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 198 | } |
Tim Peters | a6ca4f4 | 2001-10-31 03:50:45 +0000 | [diff] [blame] | 199 | |
| 200 | |
| 201 | if (DosEnterCritSec()) { |
| 202 | dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", |
| 203 | PyThread_get_thread_ident(), aLock, GetLastError())); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | lock->is_set = 0; |
| 208 | DosPostEventSem(lock->changed); |
| 209 | |
| 210 | DosExitCritSec(); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Semaphore support. |
| 215 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 216 | PyThread_type_sema |
| 217 | PyThread_allocate_sema(int value) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 218 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 219 | return (PyThread_type_sema) 0; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 222 | void |
| 223 | PyThread_free_sema(PyThread_type_sema aSemaphore) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 224 | { |
| 225 | |
| 226 | } |
| 227 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 228 | int |
| 229 | PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 230 | { |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 231 | return -1; |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 234 | void |
| 235 | PyThread_up_sema(PyThread_type_sema aSemaphore) |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 236 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 237 | dprintf(("%ld: PyThread_up_sema(%p)\n", PyThread_get_thread_ident(), aSemaphore)); |
Guido van Rossum | 6233293 | 1995-04-10 11:36:14 +0000 | [diff] [blame] | 238 | } |