Bump version number to 2.4.2 to pick up the latest minor bug fixes.
[python/dscho.git] / Python / thread_os2.h
blob69bcd2ce504ff539a230b199dad09f11453ceb9e
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
18 * Initialization of the C package, should not be needed.
20 static void
21 PyThread__init_thread(void)
26 * Thread support.
28 long
29 PyThread_start_new_thread(void (*func)(void *), void *arg)
31 int aThread;
32 int success = 0;
34 aThread = _beginthread(func,NULL,65536,arg);
36 if( aThread == -1 ) {
37 success = -1;
38 fprintf(stderr,"aThread failed == %d",aThread);
39 dprintf(("_beginthread failed. return %ld\n", errno));
42 return success;
45 long
46 PyThread_get_thread_ident(void)
48 #if !defined(PYCC_GCC)
49 PPIB pib;
50 PTIB tib;
51 #endif
53 if (!initialized)
54 PyThread_init_thread();
56 #if defined(PYCC_GCC)
57 return _gettid();
58 #else
59 DosGetInfoBlocks(&tib,&pib);
60 return tib->tib_ptib2->tib2_ultid;
61 #endif
64 static void
65 do_PyThread_exit_thread(int no_cleanup)
67 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
68 if (!initialized)
69 if (no_cleanup)
70 _exit(0);
71 else
72 exit(0);
73 _endthread();
76 void
77 PyThread_exit_thread(void)
79 do_PyThread_exit_thread(0);
82 void
83 PyThread__exit_thread(void)
85 do_PyThread_exit_thread(1);
88 #ifndef NO_EXIT_PROG
89 static void
90 do_PyThread_exit_prog(int status, int no_cleanup)
92 dprintf(("PyThread_exit_prog(%d) called\n", status));
93 if (!initialized)
94 if (no_cleanup)
95 _exit(status);
96 else
97 exit(status);
100 void
101 PyThread_exit_prog(int status)
103 do_PyThread_exit_prog(status, 0);
106 void
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
116 * counterparts.
119 typedef struct os2_lock_t {
120 int is_set;
121 HEV changed;
122 } *type_os2_lock;
124 PyThread_type_lock
125 PyThread_allocate_lock(void)
127 #if defined(PYCC_GCC)
128 _fmutex *sem = malloc(sizeof(_fmutex));
129 if (!initialized)
130 PyThread_init_thread();
131 dprintf(("%ld: PyThread_allocate_lock() -> %lx\n",
132 PyThread_get_thread_ident(),
133 (long)sem));
134 if (_fmutex_create(sem, 0)) {
135 free(sem);
136 sem = NULL;
138 return (PyThread_type_lock) sem;
139 #else
140 APIRET rc;
141 type_os2_lock lock = (type_os2_lock)malloc(sizeof(struct os2_lock_t));
143 dprintf(("PyThread_allocate_lock called\n"));
144 if (!initialized)
145 PyThread_init_thread();
147 lock->is_set = 0;
149 DosCreateEventSem(NULL, &lock->changed, 0, 0);
151 dprintf(("%ld: PyThread_allocate_lock() -> %p\n",
152 PyThread_get_thread_ident(),
153 lock->changed));
155 return (PyThread_type_lock) lock;
156 #endif
159 void
160 PyThread_free_lock(PyThread_type_lock aLock)
162 #if !defined(PYCC_GCC)
163 type_os2_lock lock = (type_os2_lock)aLock;
164 #endif
166 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
168 #if defined(PYCC_GCC)
169 if (aLock) {
170 _fmutex_close((_fmutex *)aLock);
171 free((_fmutex *)aLock);
173 #else
174 DosCloseEventSem(lock->changed);
175 free(aLock);
176 #endif
180 * Return 1 on success if the lock was acquired
182 * and 0 if the lock was not acquired.
184 int
185 PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
187 #if !defined(PYCC_GCC)
188 int done = 0;
189 ULONG count;
190 PID pid = 0;
191 TID tid = 0;
192 type_os2_lock lock = (type_os2_lock)aLock;
193 #endif
195 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),
196 aLock, waitflag));
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))
201 return 0;
202 #else
203 while (!done) {
204 /* if the lock is currently set, we have to wait for the state to change */
205 if (lock->is_set) {
206 if (!waitflag)
207 return 0;
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())
216 return 0;
218 if (!lock->is_set) {
219 lock->is_set = 1;
220 DosResetEventSem(lock->changed, &count);
221 done = 1;
224 DosExitCritSec();
226 #endif
228 return 1;
231 void PyThread_release_lock(PyThread_type_lock aLock)
233 #if defined(PYCC_GCC)
234 type_os2_lock lock = (type_os2_lock)aLock;
235 #endif
237 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
239 #if defined(PYCC_GCC)
240 if (aLock)
241 _fmutex_release((_fmutex *)aLock);
242 #else
243 if (!lock->is_set) {
244 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
245 PyThread_get_thread_ident(), aLock, GetLastError()));
246 return;
250 if (DosEnterCritSec()) {
251 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
252 PyThread_get_thread_ident(), aLock, GetLastError()));
253 return;
256 lock->is_set = 0;
257 DosPostEventSem(lock->changed);
259 DosExitCritSec();
260 #endif