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
)
40 thread_id
= _beginthread(func
,
42 OS2_STACKSIZE(_pythread_stacksize
),
45 if (thread_id
== -1) {
46 dprintf(("_beginthread failed. return %ld\n", errno
));
53 PyThread_get_thread_ident(void)
55 #if !defined(PYCC_GCC)
61 PyThread_init_thread();
66 DosGetInfoBlocks(&tib
, &pib
);
67 return tib
->tib_ptib2
->tib2_ultid
;
72 do_PyThread_exit_thread(int no_cleanup
)
74 dprintf(("%ld: PyThread_exit_thread called\n",
75 PyThread_get_thread_ident()));
85 PyThread_exit_thread(void)
87 do_PyThread_exit_thread(0);
91 PyThread__exit_thread(void)
93 do_PyThread_exit_thread(1);
98 do_PyThread_exit_prog(int status
, int no_cleanup
)
100 dprintf(("PyThread_exit_prog(%d) called\n", status
));
109 PyThread_exit_prog(int status
)
111 do_PyThread_exit_prog(status
, 0);
115 PyThread__exit_prog(int status
)
117 do_PyThread_exit_prog(status
, 1);
119 #endif /* NO_EXIT_PROG */
122 * Lock support. This is implemented with an event semaphore and critical
123 * sections to make it behave more like a posix mutex than its OS/2
127 typedef struct os2_lock_t
{
133 PyThread_allocate_lock(void)
135 #if defined(PYCC_GCC)
136 _fmutex
*sem
= malloc(sizeof(_fmutex
));
138 PyThread_init_thread();
139 dprintf(("%ld: PyThread_allocate_lock() -> %lx\n",
140 PyThread_get_thread_ident(),
142 if (_fmutex_create(sem
, 0)) {
146 return (PyThread_type_lock
)sem
;
149 type_os2_lock lock
= (type_os2_lock
)malloc(sizeof(struct os2_lock_t
));
151 dprintf(("PyThread_allocate_lock called\n"));
153 PyThread_init_thread();
157 DosCreateEventSem(NULL
, &lock
->changed
, 0, 0);
159 dprintf(("%ld: PyThread_allocate_lock() -> %p\n",
160 PyThread_get_thread_ident(),
163 return (PyThread_type_lock
)lock
;
168 PyThread_free_lock(PyThread_type_lock aLock
)
170 #if !defined(PYCC_GCC)
171 type_os2_lock lock
= (type_os2_lock
)aLock
;
174 dprintf(("%ld: PyThread_free_lock(%p) called\n",
175 PyThread_get_thread_ident(),aLock
));
177 #if defined(PYCC_GCC)
179 _fmutex_close((_fmutex
*)aLock
);
180 free((_fmutex
*)aLock
);
183 DosCloseEventSem(lock
->changed
);
189 * Return 1 on success if the lock was acquired
191 * and 0 if the lock was not acquired.
194 PyThread_acquire_lock(PyThread_type_lock aLock
, int waitflag
)
196 #if !defined(PYCC_GCC)
201 type_os2_lock lock
= (type_os2_lock
)aLock
;
204 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n",
205 PyThread_get_thread_ident(),
209 #if defined(PYCC_GCC)
210 /* always successful if the lock doesn't exist */
212 _fmutex_request((_fmutex
*)aLock
, waitflag
? 0 : _FMR_NOWAIT
))
216 /* if the lock is currently set, we have to wait for
217 * the state to change
222 DosWaitEventSem(lock
->changed
, SEM_INDEFINITE_WAIT
);
225 /* enter a critical section and try to get the semaphore. If
226 * it is still locked, we will try again.
228 if (DosEnterCritSec())
233 DosResetEventSem(lock
->changed
, &count
);
245 PyThread_release_lock(PyThread_type_lock aLock
)
247 #if !defined(PYCC_GCC)
248 type_os2_lock lock
= (type_os2_lock
)aLock
;
251 dprintf(("%ld: PyThread_release_lock(%p) called\n",
252 PyThread_get_thread_ident(),
255 #if defined(PYCC_GCC)
257 _fmutex_release((_fmutex
*)aLock
);
260 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
261 PyThread_get_thread_ident(),
267 if (DosEnterCritSec()) {
268 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
269 PyThread_get_thread_ident(),
276 DosPostEventSem(lock
->changed
);
282 /* minimum/maximum thread stack sizes supported */
283 #define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */
284 #define THREAD_MAX_STACKSIZE 0x2000000 /* 32MB */
286 /* set the thread stack size.
287 * Return 0 if size is valid, -1 otherwise.
290 _pythread_os2_set_stacksize(size_t size
)
294 _pythread_stacksize
= 0;
299 if (size
>= THREAD_MIN_STACKSIZE
&& size
< THREAD_MAX_STACKSIZE
) {
300 _pythread_stacksize
= size
;
307 #define THREAD_SET_STACKSIZE(x) _pythread_os2_set_stacksize(x)