This commit was manufactured by cvs2svn to create tag
[python/dscho.git] / Python / thread_os2.h
blobcfd0834249bd86954778b5ead72fcf5153cdd498
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 long PyThread_get_thread_ident(void);
14 * Initialization of the C package, should not be needed.
16 static void
17 PyThread__init_thread(void)
22 * Thread support.
24 long
25 PyThread_start_new_thread(void (*func)(void *), void *arg)
27 int aThread;
28 int success = 0;
30 aThread = _beginthread(func,NULL,65536,arg);
32 if( aThread == -1 ) {
33 success = -1;
34 fprintf(stderr,"aThread failed == %d",aThread);
35 dprintf(("_beginthread failed. return %ld\n", errno));
38 return success;
41 long
42 PyThread_get_thread_ident(void)
44 PPIB pib;
45 PTIB tib;
47 if (!initialized)
48 PyThread_init_thread();
50 DosGetInfoBlocks(&tib,&pib);
51 return tib->tib_ptib2->tib2_ultid;
54 static void
55 do_PyThread_exit_thread(int no_cleanup)
57 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
58 if (!initialized)
59 if (no_cleanup)
60 _exit(0);
61 else
62 exit(0);
63 _endthread();
66 void
67 PyThread_exit_thread(void)
69 do_PyThread_exit_thread(0);
72 void
73 PyThread__exit_thread(void)
75 do_PyThread_exit_thread(1);
78 #ifndef NO_EXIT_PROG
79 static void
80 do_PyThread_exit_prog(int status, int no_cleanup)
82 dprintf(("PyThread_exit_prog(%d) called\n", status));
83 if (!initialized)
84 if (no_cleanup)
85 _exit(status);
86 else
87 exit(status);
90 void
91 PyThread_exit_prog(int status)
93 do_PyThread_exit_prog(status, 0);
96 void
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
106 # counterparts.
109 typedef struct os2_lock_t {
110 int is_set;
111 HEV changed;
112 } *type_os2_lock;
114 PyThread_type_lock
115 PyThread_allocate_lock(void)
117 APIRET rc;
118 type_os2_lock lock = (type_os2_lock)malloc(sizeof(struct os2_lock_t));
120 dprintf(("PyThread_allocate_lock called\n"));
121 if (!initialized)
122 PyThread_init_thread();
124 lock->is_set = 0;
126 DosCreateEventSem(NULL, &lock->changed, 0, 0);
128 dprintf(("%ld: PyThread_allocate_lock() -> %p\n",
129 PyThread_get_thread_ident(),
130 lock->changed));
132 return (PyThread_type_lock) lock;
135 void
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);
142 free(aLock);
146 * Return 1 on success if the lock was acquired
148 * and 0 if the lock was not acquired.
150 int
151 PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
153 int done = 0;
154 ULONG count;
155 PID pid = 0;
156 TID tid = 0;
157 type_os2_lock lock = (type_os2_lock)aLock;
159 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),
160 aLock, waitflag));
162 while (!done) {
163 /* if the lock is currently set, we have to wait for the state to change */
164 if (lock->is_set) {
165 if (!waitflag)
166 return 0;
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())
175 return 0;
177 if (!lock->is_set) {
178 lock->is_set = 1;
179 DosResetEventSem(lock->changed, &count);
180 done = 1;
183 DosExitCritSec();
186 return 1;
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));
194 if (!lock->is_set) {
195 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
196 PyThread_get_thread_ident(), aLock, GetLastError()));
197 return;
201 if (DosEnterCritSec()) {
202 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
203 PyThread_get_thread_ident(), aLock, GetLastError()));
204 return;
207 lock->is_set = 0;
208 DosPostEventSem(lock->changed);
210 DosExitCritSec();
214 * Semaphore support.
216 PyThread_type_sema
217 PyThread_allocate_sema(int value)
219 return (PyThread_type_sema) 0;
222 void
223 PyThread_free_sema(PyThread_type_sema aSemaphore)
228 int
229 PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag)
231 return -1;
234 void
235 PyThread_up_sema(PyThread_type_sema aSemaphore)
237 dprintf(("%ld: PyThread_up_sema(%p)\n", PyThread_get_thread_ident(), aSemaphore));