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 #if !defined(THREAD_STACK_SIZE)
18 #define THREAD_STACK_SIZE 0x10000
22 * Initialization of the C package, should not be needed.
25 PyThread__init_thread(void)
33 PyThread_start_new_thread(void (*func
)(void *), void *arg
)
38 aThread
= _beginthread(func
, NULL
, THREAD_STACK_SIZE
, arg
);
42 fprintf(stderr
, "aThread failed == %d", aThread
);
43 dprintf(("_beginthread failed. return %ld\n", errno
));
50 PyThread_get_thread_ident(void)
52 #if !defined(PYCC_GCC)
58 PyThread_init_thread();
63 DosGetInfoBlocks(&tib
, &pib
);
64 return tib
->tib_ptib2
->tib2_ultid
;
69 do_PyThread_exit_thread(int no_cleanup
)
71 dprintf(("%ld: PyThread_exit_thread called\n",
72 PyThread_get_thread_ident()));
82 PyThread_exit_thread(void)
84 do_PyThread_exit_thread(0);
88 PyThread__exit_thread(void)
90 do_PyThread_exit_thread(1);
95 do_PyThread_exit_prog(int status
, int no_cleanup
)
97 dprintf(("PyThread_exit_prog(%d) called\n", status
));
106 PyThread_exit_prog(int status
)
108 do_PyThread_exit_prog(status
, 0);
112 PyThread__exit_prog(int status
)
114 do_PyThread_exit_prog(status
, 1);
116 #endif /* NO_EXIT_PROG */
119 * Lock support. This is implemented with an event semaphore and critical
120 * sections to make it behave more like a posix mutex than its OS/2
124 typedef struct os2_lock_t
{
130 PyThread_allocate_lock(void)
132 #if defined(PYCC_GCC)
133 _fmutex
*sem
= malloc(sizeof(_fmutex
));
135 PyThread_init_thread();
136 dprintf(("%ld: PyThread_allocate_lock() -> %lx\n",
137 PyThread_get_thread_ident(),
139 if (_fmutex_create(sem
, 0)) {
143 return (PyThread_type_lock
)sem
;
146 type_os2_lock lock
= (type_os2_lock
)malloc(sizeof(struct os2_lock_t
));
148 dprintf(("PyThread_allocate_lock called\n"));
150 PyThread_init_thread();
154 DosCreateEventSem(NULL
, &lock
->changed
, 0, 0);
156 dprintf(("%ld: PyThread_allocate_lock() -> %p\n",
157 PyThread_get_thread_ident(),
160 return (PyThread_type_lock
)lock
;
165 PyThread_free_lock(PyThread_type_lock aLock
)
167 #if !defined(PYCC_GCC)
168 type_os2_lock lock
= (type_os2_lock
)aLock
;
171 dprintf(("%ld: PyThread_free_lock(%p) called\n",
172 PyThread_get_thread_ident(),aLock
));
174 #if defined(PYCC_GCC)
176 _fmutex_close((_fmutex
*)aLock
);
177 free((_fmutex
*)aLock
);
180 DosCloseEventSem(lock
->changed
);
186 * Return 1 on success if the lock was acquired
188 * and 0 if the lock was not acquired.
191 PyThread_acquire_lock(PyThread_type_lock aLock
, int waitflag
)
193 #if !defined(PYCC_GCC)
198 type_os2_lock lock
= (type_os2_lock
)aLock
;
201 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n",
202 PyThread_get_thread_ident(),
206 #if defined(PYCC_GCC)
207 /* always successful if the lock doesn't exist */
209 _fmutex_request((_fmutex
*)aLock
, waitflag
? 0 : _FMR_NOWAIT
))
213 /* if the lock is currently set, we have to wait for
214 * the state to change
219 DosWaitEventSem(lock
->changed
, SEM_INDEFINITE_WAIT
);
222 /* enter a critical section and try to get the semaphore. If
223 * it is still locked, we will try again.
225 if (DosEnterCritSec())
230 DosResetEventSem(lock
->changed
, &count
);
241 void PyThread_release_lock(PyThread_type_lock aLock
)
243 #if !defined(PYCC_GCC)
244 type_os2_lock lock
= (type_os2_lock
)aLock
;
247 dprintf(("%ld: PyThread_release_lock(%p) called\n",
248 PyThread_get_thread_ident(),
251 #if defined(PYCC_GCC)
253 _fmutex_release((_fmutex
*)aLock
);
256 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
257 PyThread_get_thread_ident(),
263 if (DosEnterCritSec()) {
264 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
265 PyThread_get_thread_ident(),
272 DosPostEventSem(lock
->changed
);