1 /* This code implemented by cvale@netcom.com */
3 #define INCL_DOSPROCESS
4 #define INCL_DOSSEMAPHORES
11 #include <sys/builtin.h>
12 #include <sys/fmutex.h>
14 long PyThread_get_thread_ident(void);
17 /* default thread stack size of 64kB */
18 #if !defined(THREAD_STACK_SIZE)
19 #define THREAD_STACK_SIZE 0x10000
22 #define OS2_STACKSIZE(x) (x ? x : THREAD_STACK_SIZE)
25 * Initialization of the C package, should not be needed.
28 PyThread__init_thread(void)
36 PyThread_start_new_thread(void (*func
)(void *), void *arg
)
41 aThread
= _beginthread(func
,
43 OS2_STACKSIZE(_pythread_stacksize
),
48 fprintf(stderr
, "aThread failed == %d", aThread
);
49 dprintf(("_beginthread failed. return %ld\n", errno
));
56 PyThread_get_thread_ident(void)
58 #if !defined(PYCC_GCC)
64 PyThread_init_thread();
69 DosGetInfoBlocks(&tib
, &pib
);
70 return tib
->tib_ptib2
->tib2_ultid
;
75 do_PyThread_exit_thread(int no_cleanup
)
77 dprintf(("%ld: PyThread_exit_thread called\n",
78 PyThread_get_thread_ident()));
88 PyThread_exit_thread(void)
90 do_PyThread_exit_thread(0);
94 PyThread__exit_thread(void)
96 do_PyThread_exit_thread(1);
101 do_PyThread_exit_prog(int status
, int no_cleanup
)
103 dprintf(("PyThread_exit_prog(%d) called\n", status
));
112 PyThread_exit_prog(int status
)
114 do_PyThread_exit_prog(status
, 0);
118 PyThread__exit_prog(int status
)
120 do_PyThread_exit_prog(status
, 1);
122 #endif /* NO_EXIT_PROG */
125 * Lock support. This is implemented with an event semaphore and critical
126 * sections to make it behave more like a posix mutex than its OS/2
130 typedef struct os2_lock_t
{
136 PyThread_allocate_lock(void)
138 #if defined(PYCC_GCC)
139 _fmutex
*sem
= malloc(sizeof(_fmutex
));
141 PyThread_init_thread();
142 dprintf(("%ld: PyThread_allocate_lock() -> %lx\n",
143 PyThread_get_thread_ident(),
145 if (_fmutex_create(sem
, 0)) {
149 return (PyThread_type_lock
)sem
;
152 type_os2_lock lock
= (type_os2_lock
)malloc(sizeof(struct os2_lock_t
));
154 dprintf(("PyThread_allocate_lock called\n"));
156 PyThread_init_thread();
160 DosCreateEventSem(NULL
, &lock
->changed
, 0, 0);
162 dprintf(("%ld: PyThread_allocate_lock() -> %p\n",
163 PyThread_get_thread_ident(),
166 return (PyThread_type_lock
)lock
;
171 PyThread_free_lock(PyThread_type_lock aLock
)
173 #if !defined(PYCC_GCC)
174 type_os2_lock lock
= (type_os2_lock
)aLock
;
177 dprintf(("%ld: PyThread_free_lock(%p) called\n",
178 PyThread_get_thread_ident(),aLock
));
180 #if defined(PYCC_GCC)
182 _fmutex_close((_fmutex
*)aLock
);
183 free((_fmutex
*)aLock
);
186 DosCloseEventSem(lock
->changed
);
192 * Return 1 on success if the lock was acquired
194 * and 0 if the lock was not acquired.
197 PyThread_acquire_lock(PyThread_type_lock aLock
, int waitflag
)
199 #if !defined(PYCC_GCC)
204 type_os2_lock lock
= (type_os2_lock
)aLock
;
207 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n",
208 PyThread_get_thread_ident(),
212 #if defined(PYCC_GCC)
213 /* always successful if the lock doesn't exist */
215 _fmutex_request((_fmutex
*)aLock
, waitflag
? 0 : _FMR_NOWAIT
))
219 /* if the lock is currently set, we have to wait for
220 * the state to change
225 DosWaitEventSem(lock
->changed
, SEM_INDEFINITE_WAIT
);
228 /* enter a critical section and try to get the semaphore. If
229 * it is still locked, we will try again.
231 if (DosEnterCritSec())
236 DosResetEventSem(lock
->changed
, &count
);
248 PyThread_release_lock(PyThread_type_lock aLock
)
250 #if !defined(PYCC_GCC)
251 type_os2_lock lock
= (type_os2_lock
)aLock
;
254 dprintf(("%ld: PyThread_release_lock(%p) called\n",
255 PyThread_get_thread_ident(),
258 #if defined(PYCC_GCC)
260 _fmutex_release((_fmutex
*)aLock
);
263 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
264 PyThread_get_thread_ident(),
270 if (DosEnterCritSec()) {
271 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
272 PyThread_get_thread_ident(),
279 DosPostEventSem(lock
->changed
);
285 /* minimum/maximum thread stack sizes supported */
286 #define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */
287 #define THREAD_MAX_STACKSIZE 0x2000000 /* 32MB */
289 /* set the thread stack size.
290 * Return 0 if size is valid, -1 otherwise.
293 _pythread_os2_set_stacksize(size_t size
)
297 _pythread_stacksize
= 0;
302 if (size
>= THREAD_MIN_STACKSIZE
&& size
< THREAD_MAX_STACKSIZE
) {
303 _pythread_stacksize
= size
;
310 #define THREAD_SET_STACKSIZE(x) _pythread_os2_set_stacksize(x)