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",
68 PyThread_get_thread_ident()));
78 PyThread_exit_thread(void)
80 do_PyThread_exit_thread(0);
84 PyThread__exit_thread(void)
86 do_PyThread_exit_thread(1);
91 do_PyThread_exit_prog(int status
, int no_cleanup
)
93 dprintf(("PyThread_exit_prog(%d) called\n", status
));
102 PyThread_exit_prog(int status
)
104 do_PyThread_exit_prog(status
, 0);
108 PyThread__exit_prog(int status
)
110 do_PyThread_exit_prog(status
, 1);
112 #endif /* NO_EXIT_PROG */
115 * Lock support. This is implemented with an event semaphore and critical
116 * sections to make it behave more like a posix mutex than its OS/2
120 typedef struct os2_lock_t
{
126 PyThread_allocate_lock(void)
128 #if defined(PYCC_GCC)
129 _fmutex
*sem
= malloc(sizeof(_fmutex
));
131 PyThread_init_thread();
132 dprintf(("%ld: PyThread_allocate_lock() -> %lx\n",
133 PyThread_get_thread_ident(),
135 if (_fmutex_create(sem
, 0)) {
139 return (PyThread_type_lock
)sem
;
142 type_os2_lock lock
= (type_os2_lock
)malloc(sizeof(struct os2_lock_t
));
144 dprintf(("PyThread_allocate_lock called\n"));
146 PyThread_init_thread();
150 DosCreateEventSem(NULL
, &lock
->changed
, 0, 0);
152 dprintf(("%ld: PyThread_allocate_lock() -> %p\n",
153 PyThread_get_thread_ident(),
156 return (PyThread_type_lock
)lock
;
161 PyThread_free_lock(PyThread_type_lock aLock
)
163 #if !defined(PYCC_GCC)
164 type_os2_lock lock
= (type_os2_lock
)aLock
;
167 dprintf(("%ld: PyThread_free_lock(%p) called\n",
168 PyThread_get_thread_ident(),aLock
));
170 #if defined(PYCC_GCC)
172 _fmutex_close((_fmutex
*)aLock
);
173 free((_fmutex
*)aLock
);
176 DosCloseEventSem(lock
->changed
);
182 * Return 1 on success if the lock was acquired
184 * and 0 if the lock was not acquired.
187 PyThread_acquire_lock(PyThread_type_lock aLock
, int waitflag
)
189 #if !defined(PYCC_GCC)
194 type_os2_lock lock
= (type_os2_lock
)aLock
;
197 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n",
198 PyThread_get_thread_ident(),
202 #if defined(PYCC_GCC)
203 /* always successful if the lock doesn't exist */
205 _fmutex_request((_fmutex
*)aLock
, waitflag
? 0 : _FMR_NOWAIT
))
209 /* if the lock is currently set, we have to wait for
210 * the state to change
215 DosWaitEventSem(lock
->changed
, SEM_INDEFINITE_WAIT
);
218 /* enter a critical section and try to get the semaphore. If
219 * it is still locked, we will try again.
221 if (DosEnterCritSec())
226 DosResetEventSem(lock
->changed
, &count
);
237 void PyThread_release_lock(PyThread_type_lock aLock
)
239 #if !defined(PYCC_GCC)
240 type_os2_lock lock
= (type_os2_lock
)aLock
;
243 dprintf(("%ld: PyThread_release_lock(%p) called\n",
244 PyThread_get_thread_ident(),
247 #if defined(PYCC_GCC)
249 _fmutex_release((_fmutex
*)aLock
);
252 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
253 PyThread_get_thread_ident(),
259 if (DosEnterCritSec()) {
260 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
261 PyThread_get_thread_ident(),
268 DosPostEventSem(lock
->changed
);