1 /* This code implemented by cvale@netcom.com */
3 #define INCL_DOSPROCESS
4 #define INCL_DOSSEMAPHORES
10 long PyThread_get_thread_ident(void);
14 * Initialization of the C package, should not be needed.
17 PyThread__init_thread(void)
25 PyThread_start_new_thread(void (*func
)(void *), void *arg
)
30 aThread
= _beginthread(func
,NULL
,65536,arg
);
34 fprintf(stderr
,"aThread failed == %d",aThread
);
35 dprintf(("_beginthread failed. return %ld\n", errno
));
42 PyThread_get_thread_ident(void)
48 PyThread_init_thread();
50 DosGetInfoBlocks(&tib
,&pib
);
51 return tib
->tib_ptib2
->tib2_ultid
;
55 do_PyThread_exit_thread(int no_cleanup
)
57 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
67 PyThread_exit_thread(void)
69 do_PyThread_exit_thread(0);
73 PyThread__exit_thread(void)
75 do_PyThread_exit_thread(1);
80 do_PyThread_exit_prog(int status
, int no_cleanup
)
82 dprintf(("PyThread_exit_prog(%d) called\n", status
));
91 PyThread_exit_prog(int status
)
93 do_PyThread_exit_prog(status
, 0);
97 PyThread__exit_prog(int status
)
99 do_PyThread_exit_prog(status
, 1);
101 #endif /* NO_EXIT_PROG */
104 * Lock support. This is implemented with an event semaphore and critical
105 * sections to make it behave more like a posix mutex than its OS/2
109 typedef struct os2_lock_t
{
115 PyThread_allocate_lock(void)
118 type_os2_lock lock
= (type_os2_lock
)malloc(sizeof(struct os2_lock_t
));
120 dprintf(("PyThread_allocate_lock called\n"));
122 PyThread_init_thread();
126 DosCreateEventSem(NULL
, &lock
->changed
, 0, 0);
128 dprintf(("%ld: PyThread_allocate_lock() -> %p\n",
129 PyThread_get_thread_ident(),
132 return (PyThread_type_lock
) lock
;
136 PyThread_free_lock(PyThread_type_lock aLock
)
138 type_os2_lock lock
= (type_os2_lock
)aLock
;
139 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock
));
141 DosCloseEventSem(lock
->changed
);
146 * Return 1 on success if the lock was acquired
148 * and 0 if the lock was not acquired.
151 PyThread_acquire_lock(PyThread_type_lock aLock
, int waitflag
)
157 type_os2_lock lock
= (type_os2_lock
)aLock
;
159 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),
163 /* if the lock is currently set, we have to wait for the state to change */
167 DosWaitEventSem(lock
->changed
, SEM_INDEFINITE_WAIT
);
171 * enter a critical section and try to get the semaphore. If
172 * it is still locked, we will try again.
174 if (DosEnterCritSec())
179 DosResetEventSem(lock
->changed
, &count
);
189 void PyThread_release_lock(PyThread_type_lock aLock
)
191 type_os2_lock lock
= (type_os2_lock
)aLock
;
192 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock
));
195 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
196 PyThread_get_thread_ident(), aLock
, GetLastError()));
201 if (DosEnterCritSec()) {
202 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
203 PyThread_get_thread_ident(), aLock
, GetLastError()));
208 DosPostEventSem(lock
->changed
);
217 PyThread_allocate_sema(int value
)
219 return (PyThread_type_sema
) 0;
223 PyThread_free_sema(PyThread_type_sema aSemaphore
)
229 PyThread_down_sema(PyThread_type_sema aSemaphore
, int waitflag
)
235 PyThread_up_sema(PyThread_type_sema aSemaphore
)
237 dprintf(("%ld: PyThread_up_sema(%p)\n", PyThread_get_thread_ident(), aSemaphore
));