Added duplicate call to fileConfig() to ensure that it cleans up after itself correctly.
[python.git] / Python / thread_os2.h
blob3ed9d08a3147b50205f3edcb5f32d9cc92aa9e34
1 /* This code implemented by cvale@netcom.com */
3 #define INCL_DOSPROCESS
4 #define INCL_DOSSEMAPHORES
5 #include "os2.h"
6 #include "limits.h"
8 #include "process.h"
10 #if defined(PYCC_GCC)
11 #include <sys/builtin.h>
12 #include <sys/fmutex.h>
13 #else
14 long PyThread_get_thread_ident(void);
15 #endif
17 /* default thread stack size of 64kB */
18 #if !defined(THREAD_STACK_SIZE)
19 #define THREAD_STACK_SIZE 0x10000
20 #endif
22 #define OS2_STACKSIZE(x) (x ? x : THREAD_STACK_SIZE)
25 * Initialization of the C package, should not be needed.
27 static void
28 PyThread__init_thread(void)
33 * Thread support.
35 long
36 PyThread_start_new_thread(void (*func)(void *), void *arg)
38 int aThread;
39 int success = 0;
41 aThread = _beginthread(func,
42 NULL,
43 OS2_STACKSIZE(_pythread_stacksize),
44 arg);
46 if (aThread == -1) {
47 success = -1;
48 fprintf(stderr, "aThread failed == %d", aThread);
49 dprintf(("_beginthread failed. return %ld\n", errno));
52 return success;
55 long
56 PyThread_get_thread_ident(void)
58 #if !defined(PYCC_GCC)
59 PPIB pib;
60 PTIB tib;
61 #endif
63 if (!initialized)
64 PyThread_init_thread();
66 #if defined(PYCC_GCC)
67 return _gettid();
68 #else
69 DosGetInfoBlocks(&tib, &pib);
70 return tib->tib_ptib2->tib2_ultid;
71 #endif
74 static void
75 do_PyThread_exit_thread(int no_cleanup)
77 dprintf(("%ld: PyThread_exit_thread called\n",
78 PyThread_get_thread_ident()));
79 if (!initialized)
80 if (no_cleanup)
81 _exit(0);
82 else
83 exit(0);
84 _endthread();
87 void
88 PyThread_exit_thread(void)
90 do_PyThread_exit_thread(0);
93 void
94 PyThread__exit_thread(void)
96 do_PyThread_exit_thread(1);
99 #ifndef NO_EXIT_PROG
100 static void
101 do_PyThread_exit_prog(int status, int no_cleanup)
103 dprintf(("PyThread_exit_prog(%d) called\n", status));
104 if (!initialized)
105 if (no_cleanup)
106 _exit(status);
107 else
108 exit(status);
111 void
112 PyThread_exit_prog(int status)
114 do_PyThread_exit_prog(status, 0);
117 void
118 PyThread__exit_prog(int status)
120 do_PyThread_exit_prog(status, 1);
122 #endif /* NO_EXIT_PROG */
125 * Lock support. This is implemented with an event semaphore and critical
126 * sections to make it behave more like a posix mutex than its OS/2
127 * counterparts.
130 typedef struct os2_lock_t {
131 int is_set;
132 HEV changed;
133 } *type_os2_lock;
135 PyThread_type_lock
136 PyThread_allocate_lock(void)
138 #if defined(PYCC_GCC)
139 _fmutex *sem = malloc(sizeof(_fmutex));
140 if (!initialized)
141 PyThread_init_thread();
142 dprintf(("%ld: PyThread_allocate_lock() -> %lx\n",
143 PyThread_get_thread_ident(),
144 (long)sem));
145 if (_fmutex_create(sem, 0)) {
146 free(sem);
147 sem = NULL;
149 return (PyThread_type_lock)sem;
150 #else
151 APIRET rc;
152 type_os2_lock lock = (type_os2_lock)malloc(sizeof(struct os2_lock_t));
154 dprintf(("PyThread_allocate_lock called\n"));
155 if (!initialized)
156 PyThread_init_thread();
158 lock->is_set = 0;
160 DosCreateEventSem(NULL, &lock->changed, 0, 0);
162 dprintf(("%ld: PyThread_allocate_lock() -> %p\n",
163 PyThread_get_thread_ident(),
164 lock->changed));
166 return (PyThread_type_lock)lock;
167 #endif
170 void
171 PyThread_free_lock(PyThread_type_lock aLock)
173 #if !defined(PYCC_GCC)
174 type_os2_lock lock = (type_os2_lock)aLock;
175 #endif
177 dprintf(("%ld: PyThread_free_lock(%p) called\n",
178 PyThread_get_thread_ident(),aLock));
180 #if defined(PYCC_GCC)
181 if (aLock) {
182 _fmutex_close((_fmutex *)aLock);
183 free((_fmutex *)aLock);
185 #else
186 DosCloseEventSem(lock->changed);
187 free(aLock);
188 #endif
192 * Return 1 on success if the lock was acquired
194 * and 0 if the lock was not acquired.
196 int
197 PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
199 #if !defined(PYCC_GCC)
200 int done = 0;
201 ULONG count;
202 PID pid = 0;
203 TID tid = 0;
204 type_os2_lock lock = (type_os2_lock)aLock;
205 #endif
207 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n",
208 PyThread_get_thread_ident(),
209 aLock,
210 waitflag));
212 #if defined(PYCC_GCC)
213 /* always successful if the lock doesn't exist */
214 if (aLock &&
215 _fmutex_request((_fmutex *)aLock, waitflag ? 0 : _FMR_NOWAIT))
216 return 0;
217 #else
218 while (!done) {
219 /* if the lock is currently set, we have to wait for
220 * the state to change
222 if (lock->is_set) {
223 if (!waitflag)
224 return 0;
225 DosWaitEventSem(lock->changed, SEM_INDEFINITE_WAIT);
228 /* enter a critical section and try to get the semaphore. If
229 * it is still locked, we will try again.
231 if (DosEnterCritSec())
232 return 0;
234 if (!lock->is_set) {
235 lock->is_set = 1;
236 DosResetEventSem(lock->changed, &count);
237 done = 1;
240 DosExitCritSec();
242 #endif
244 return 1;
247 void
248 PyThread_release_lock(PyThread_type_lock aLock)
250 #if !defined(PYCC_GCC)
251 type_os2_lock lock = (type_os2_lock)aLock;
252 #endif
254 dprintf(("%ld: PyThread_release_lock(%p) called\n",
255 PyThread_get_thread_ident(),
256 aLock));
258 #if defined(PYCC_GCC)
259 if (aLock)
260 _fmutex_release((_fmutex *)aLock);
261 #else
262 if (!lock->is_set) {
263 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
264 PyThread_get_thread_ident(),
265 aLock,
266 GetLastError()));
267 return;
270 if (DosEnterCritSec()) {
271 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
272 PyThread_get_thread_ident(),
273 aLock,
274 GetLastError()));
275 return;
278 lock->is_set = 0;
279 DosPostEventSem(lock->changed);
281 DosExitCritSec();
282 #endif
285 /* minimum/maximum thread stack sizes supported */
286 #define THREAD_MIN_STACKSIZE 0x8000 /* 32kB */
287 #define THREAD_MAX_STACKSIZE 0x2000000 /* 32MB */
289 /* set the thread stack size.
290 * Return 0 if size is valid, -1 otherwise.
292 static int
293 _pythread_os2_set_stacksize(size_t size)
295 /* set to default */
296 if (size == 0) {
297 _pythread_stacksize = 0;
298 return 0;
301 /* valid range? */
302 if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
303 _pythread_stacksize = size;
304 return 0;
307 return -1;
310 #define THREAD_SET_STACKSIZE(x) _pythread_os2_set_stacksize(x)