blob: 1b264b5ad5d41f71270e256ba93091b82d93ebec [file] [log] [blame]
Guido van Rossum62332931995-04-10 11:36:14 +00001/* 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 MacIntyred9400542002-02-26 11:41:34 +000010#if defined(PYCC_GCC)
11#include <sys/builtin.h>
12#include <sys/fmutex.h>
13#else
Guido van Rossum65d5b571998-12-21 19:32:43 +000014long PyThread_get_thread_ident(void);
Andrew MacIntyred9400542002-02-26 11:41:34 +000015#endif
Guido van Rossum62332931995-04-10 11:36:14 +000016
Thomas Wouters0e3f5912006-08-11 14:57:12 +000017/* default thread stack size of 64kB */
Andrew MacIntyre2bea4742005-01-17 12:16:36 +000018#if !defined(THREAD_STACK_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019#define THREAD_STACK_SIZE 0x10000
Andrew MacIntyre2bea4742005-01-17 12:16:36 +000020#endif
21
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022#define OS2_STACKSIZE(x) (x ? x : THREAD_STACK_SIZE)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000023
Guido van Rossum62332931995-04-10 11:36:14 +000024/*
25 * Initialization of the C package, should not be needed.
26 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000027static void
28PyThread__init_thread(void)
Guido van Rossum62332931995-04-10 11:36:14 +000029{
30}
31
32/*
33 * Thread support.
34 */
Guido van Rossum3c288632001-10-16 21:13:49 +000035long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000036PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum62332931995-04-10 11:36:14 +000037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 int thread_id;
Guido van Rossum62332931995-04-10 11:36:14 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 thread_id = _beginthread(func,
41 NULL,
42 OS2_STACKSIZE(_pythread_stacksize),
43 arg);
Guido van Rossum62332931995-04-10 11:36:14 +000044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 if (thread_id == -1) {
46 dprintf(("_beginthread failed. return %ld\n", errno));
47 }
Guido van Rossum62332931995-04-10 11:36:14 +000048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 return thread_id;
Guido van Rossum62332931995-04-10 11:36:14 +000050}
51
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000052long
53PyThread_get_thread_ident(void)
Guido van Rossum62332931995-04-10 11:36:14 +000054{
Andrew MacIntyred9400542002-02-26 11:41:34 +000055#if !defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 PPIB pib;
57 PTIB tib;
Andrew MacIntyred9400542002-02-26 11:41:34 +000058#endif
Guido van Rossum62332931995-04-10 11:36:14 +000059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 if (!initialized)
61 PyThread_init_thread();
Andrew MacIntyrec4c127b2002-12-04 12:29:37 +000062
Andrew MacIntyred9400542002-02-26 11:41:34 +000063#if defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 return _gettid();
Andrew MacIntyred9400542002-02-26 11:41:34 +000065#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 DosGetInfoBlocks(&tib, &pib);
67 return tib->tib_ptib2->tib2_ultid;
Andrew MacIntyred9400542002-02-26 11:41:34 +000068#endif
Guido van Rossum62332931995-04-10 11:36:14 +000069}
70
Amaury Forgeot d'Arc72aee3d2010-02-24 00:10:48 +000071void
72PyThread_exit_thread(void)
Guido van Rossum62332931995-04-10 11:36:14 +000073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 dprintf(("%ld: PyThread_exit_thread called\n",
75 PyThread_get_thread_ident()));
76 if (!initialized)
77 exit(0);
78 _endthread();
Guido van Rossum62332931995-04-10 11:36:14 +000079}
80
Guido van Rossum62332931995-04-10 11:36:14 +000081/*
Tim Petersa6ca4f42001-10-31 03:50:45 +000082 * Lock support. This is implemented with an event semaphore and critical
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 * sections to make it behave more like a posix mutex than its OS/2
Andrew MacIntyred9400542002-02-26 11:41:34 +000084 * counterparts.
Guido van Rossum62332931995-04-10 11:36:14 +000085 */
Tim Petersa6ca4f42001-10-31 03:50:45 +000086
87typedef struct os2_lock_t {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 int is_set;
89 HEV changed;
Tim Petersa6ca4f42001-10-31 03:50:45 +000090} *type_os2_lock;
91
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092PyThread_type_lock
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000093PyThread_allocate_lock(void)
Guido van Rossum62332931995-04-10 11:36:14 +000094{
Andrew MacIntyred9400542002-02-26 11:41:34 +000095#if defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 _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 MacIntyred9400542002-02-26 11:41:34 +0000107#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 APIRET rc;
109 type_os2_lock lock = (type_os2_lock)malloc(sizeof(struct os2_lock_t));
Guido van Rossum62332931995-04-10 11:36:14 +0000110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 dprintf(("PyThread_allocate_lock called\n"));
112 if (!initialized)
113 PyThread_init_thread();
Guido van Rossum62332931995-04-10 11:36:14 +0000114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 lock->is_set = 0;
Guido van Rossum62332931995-04-10 11:36:14 +0000116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 DosCreateEventSem(NULL, &lock->changed, 0, 0);
Guido van Rossum62332931995-04-10 11:36:14 +0000118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 dprintf(("%ld: PyThread_allocate_lock() -> %p\n",
120 PyThread_get_thread_ident(),
121 lock->changed));
Tim Petersa6ca4f42001-10-31 03:50:45 +0000122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 return (PyThread_type_lock)lock;
Andrew MacIntyred9400542002-02-26 11:41:34 +0000124#endif
Guido van Rossum62332931995-04-10 11:36:14 +0000125}
126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000128PyThread_free_lock(PyThread_type_lock aLock)
Guido van Rossum62332931995-04-10 11:36:14 +0000129{
Andrew MacIntyred9400542002-02-26 11:41:34 +0000130#if !defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 type_os2_lock lock = (type_os2_lock)aLock;
Andrew MacIntyred9400542002-02-26 11:41:34 +0000132#endif
133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 dprintf(("%ld: PyThread_free_lock(%p) called\n",
135 PyThread_get_thread_ident(),aLock));
Guido van Rossum62332931995-04-10 11:36:14 +0000136
Andrew MacIntyred9400542002-02-26 11:41:34 +0000137#if defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 if (aLock) {
139 _fmutex_close((_fmutex *)aLock);
140 free((_fmutex *)aLock);
141 }
Andrew MacIntyred9400542002-02-26 11:41:34 +0000142#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 DosCloseEventSem(lock->changed);
144 free(aLock);
Andrew MacIntyred9400542002-02-26 11:41:34 +0000145#endif
Guido van Rossum62332931995-04-10 11:36:14 +0000146}
147
148/*
149 * Return 1 on success if the lock was acquired
150 *
Tim Petersa6ca4f42001-10-31 03:50:45 +0000151 * and 0 if the lock was not acquired.
Guido van Rossum62332931995-04-10 11:36:14 +0000152 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000154PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
Guido van Rossum62332931995-04-10 11:36:14 +0000155{
Andrew MacIntyred9400542002-02-26 11:41:34 +0000156#if !defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 int done = 0;
158 ULONG count;
159 PID pid = 0;
160 TID tid = 0;
161 type_os2_lock lock = (type_os2_lock)aLock;
Andrew MacIntyred9400542002-02-26 11:41:34 +0000162#endif
Guido van Rossum62332931995-04-10 11:36:14 +0000163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n",
165 PyThread_get_thread_ident(),
166 aLock,
167 waitflag));
Guido van Rossum62332931995-04-10 11:36:14 +0000168
Andrew MacIntyred9400542002-02-26 11:41:34 +0000169#if defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 /* always successful if the lock doesn't exist */
171 if (aLock &&
172 _fmutex_request((_fmutex *)aLock, waitflag ? 0 : _FMR_NOWAIT))
173 return 0;
Andrew MacIntyred9400542002-02-26 11:41:34 +0000174#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 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 Petersa6ca4f42001-10-31 03:50:45 +0000184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 /* 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 Petersa6ca4f42001-10-31 03:50:45 +0000190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 if (!lock->is_set) {
192 lock->is_set = 1;
193 DosResetEventSem(lock->changed, &count);
194 done = 1;
195 }
Andrew MacIntyrec4c127b2002-12-04 12:29:37 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 DosExitCritSec();
198 }
Andrew MacIntyred9400542002-02-26 11:41:34 +0000199#endif
Guido van Rossum62332931995-04-10 11:36:14 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 return 1;
Guido van Rossum62332931995-04-10 11:36:14 +0000202}
203
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000204void
205PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossum62332931995-04-10 11:36:14 +0000206{
Andrew MacIntyre69049592002-12-04 12:27:06 +0000207#if !defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 type_os2_lock lock = (type_os2_lock)aLock;
Andrew MacIntyred9400542002-02-26 11:41:34 +0000209#endif
210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 dprintf(("%ld: PyThread_release_lock(%p) called\n",
212 PyThread_get_thread_ident(),
213 aLock));
Guido van Rossum62332931995-04-10 11:36:14 +0000214
Andrew MacIntyred9400542002-02-26 11:41:34 +0000215#if defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (aLock)
217 _fmutex_release((_fmutex *)aLock);
Andrew MacIntyred9400542002-02-26 11:41:34 +0000218#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 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 Petersa6ca4f42001-10-31 03:50:45 +0000226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 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 Petersa6ca4f42001-10-31 03:50:45 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 lock->is_set = 0;
236 DosPostEventSem(lock->changed);
Andrew MacIntyrec4c127b2002-12-04 12:29:37 +0000237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 DosExitCritSec();
Andrew MacIntyred9400542002-02-26 11:41:34 +0000239#endif
Guido van Rossum62332931995-04-10 11:36:14 +0000240}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000241
242/* minimum/maximum thread stack sizes supported */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243#define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */
244#define THREAD_MAX_STACKSIZE 0x2000000 /* 32MB */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000245
246/* set the thread stack size.
247 * Return 0 if size is valid, -1 otherwise.
248 */
249static int
250_pythread_os2_set_stacksize(size_t size)
251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 /* set to default */
253 if (size == 0) {
254 _pythread_stacksize = 0;
255 return 0;
256 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 /* valid range? */
259 if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
260 _pythread_stacksize = size;
261 return 0;
262 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000265}
266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267#define THREAD_SET_STACKSIZE(x) _pythread_os2_set_stacksize(x)