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);
18 * Initialization of the C package, should not be needed.
21 PyThread__init_thread(void)
29 PyThread_start_new_thread(void (*func
)(void *), void *arg
)
34 aThread
= _beginthread(func
,NULL
,65536,arg
);
38 fprintf(stderr
,"aThread failed == %d",aThread
);
39 dprintf(("_beginthread failed. return %ld\n", errno
));
46 PyThread_get_thread_ident(void)
48 #if !defined(PYCC_GCC)
54 PyThread_init_thread();
59 DosGetInfoBlocks(&tib
,&pib
);
60 return tib
->tib_ptib2
->tib2_ultid
;
65 do_PyThread_exit_thread(int no_cleanup
)
67 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
77 PyThread_exit_thread(void)
79 do_PyThread_exit_thread(0);
83 PyThread__exit_thread(void)
85 do_PyThread_exit_thread(1);
90 do_PyThread_exit_prog(int status
, int no_cleanup
)
92 dprintf(("PyThread_exit_prog(%d) called\n", status
));
101 PyThread_exit_prog(int status
)
103 do_PyThread_exit_prog(status
, 0);
107 PyThread__exit_prog(int status
)
109 do_PyThread_exit_prog(status
, 1);
111 #endif /* NO_EXIT_PROG */
114 * Lock support. This is implemented with an event semaphore and critical
115 * sections to make it behave more like a posix mutex than its OS/2
119 typedef struct os2_lock_t
{
125 PyThread_allocate_lock(void)
127 #if defined(PYCC_GCC)
128 _fmutex
*sem
= malloc(sizeof(_fmutex
));
130 PyThread_init_thread();
131 dprintf(("%ld: PyThread_allocate_lock() -> %lx\n",
132 PyThread_get_thread_ident(),
134 if (_fmutex_create(sem
, 0)) {
138 return (PyThread_type_lock
) sem
;
141 type_os2_lock lock
= (type_os2_lock
)malloc(sizeof(struct os2_lock_t
));
143 dprintf(("PyThread_allocate_lock called\n"));
145 PyThread_init_thread();
149 DosCreateEventSem(NULL
, &lock
->changed
, 0, 0);
151 dprintf(("%ld: PyThread_allocate_lock() -> %p\n",
152 PyThread_get_thread_ident(),
155 return (PyThread_type_lock
) lock
;
160 PyThread_free_lock(PyThread_type_lock aLock
)
162 #if !defined(PYCC_GCC)
163 type_os2_lock lock
= (type_os2_lock
)aLock
;
166 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock
));
168 #if defined(PYCC_GCC)
170 _fmutex_close((_fmutex
*)aLock
);
171 free((_fmutex
*)aLock
);
174 DosCloseEventSem(lock
->changed
);
180 * Return 1 on success if the lock was acquired
182 * and 0 if the lock was not acquired.
185 PyThread_acquire_lock(PyThread_type_lock aLock
, int waitflag
)
187 #if !defined(PYCC_GCC)
192 type_os2_lock lock
= (type_os2_lock
)aLock
;
195 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),
198 #if defined(PYCC_GCC)
199 /* always successful if the lock doesn't exist */
200 if (aLock
&& _fmutex_request((_fmutex
*)aLock
, waitflag
? 0 : _FMR_NOWAIT
))
204 /* if the lock is currently set, we have to wait for the state to change */
208 DosWaitEventSem(lock
->changed
, SEM_INDEFINITE_WAIT
);
212 * enter a critical section and try to get the semaphore. If
213 * it is still locked, we will try again.
215 if (DosEnterCritSec())
220 DosResetEventSem(lock
->changed
, &count
);
231 void PyThread_release_lock(PyThread_type_lock aLock
)
233 #if defined(PYCC_GCC)
234 type_os2_lock lock
= (type_os2_lock
)aLock
;
237 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock
));
239 #if defined(PYCC_GCC)
241 _fmutex_release((_fmutex
*)aLock
);
244 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
245 PyThread_get_thread_ident(), aLock
, GetLastError()));
250 if (DosEnterCritSec()) {
251 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
252 PyThread_get_thread_ident(), aLock
, GetLastError()));
257 DosPostEventSem(lock
->changed
);