blob: 982fb6f5bf3cc429c1fe372bd1edf15729775bb3 [file] [log] [blame]
Guido van Rossum62332931995-04-10 11:36:14 +00001/***********************************************************
Guido van Rossumd266eb41996-10-25 14:44:06 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
16
17While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum62332931995-04-10 11:36:14 +000029
Guido van Rossumd266eb41996-10-25 14:44:06 +000030******************************************************************/
Guido van Rossum62332931995-04-10 11:36:14 +000031
32/* This code implemented by cvale@netcom.com */
33
34#define INCL_DOSPROCESS
35#define INCL_DOSSEMAPHORES
36#include "os2.h"
37#include "limits.h"
38
39#include "process.h"
40
Guido van Rossum65d5b571998-12-21 19:32:43 +000041long PyThread_get_thread_ident(void);
Guido van Rossum62332931995-04-10 11:36:14 +000042
43
44/*
45 * Initialization of the C package, should not be needed.
46 */
Guido van Rossum65d5b571998-12-21 19:32:43 +000047static void PyThread__init_thread(void)
Guido van Rossum62332931995-04-10 11:36:14 +000048{
49}
50
51/*
52 * Thread support.
53 */
Guido van Rossum65d5b571998-12-21 19:32:43 +000054int PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum62332931995-04-10 11:36:14 +000055{
56 int aThread;
57 int success = 1;
58
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000059 aThread = _beginthread(func,NULL,65536,arg);
Guido van Rossum62332931995-04-10 11:36:14 +000060
61 if( aThread == -1 ) {
62 success = 0;
63 fprintf(stderr,"aThread failed == %d",aThread);
64 dprintf(("_beginthread failed. return %ld\n", errno));
65 }
66
67 return success;
68}
69
Guido van Rossum65d5b571998-12-21 19:32:43 +000070long PyThread_get_thread_ident(void)
Guido van Rossum62332931995-04-10 11:36:14 +000071{
72 PPIB pib;
73 PTIB tib;
74
75 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +000076 PyThread_init_thread();
Guido van Rossum62332931995-04-10 11:36:14 +000077
78 DosGetInfoBlocks(&tib,&pib);
79 return tib->tib_ptib2->tib2_ultid;
80}
81
Guido van Rossum65d5b571998-12-21 19:32:43 +000082static void do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum62332931995-04-10 11:36:14 +000083{
Guido van Rossum65d5b571998-12-21 19:32:43 +000084 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
Guido van Rossum62332931995-04-10 11:36:14 +000085 if (!initialized)
86 if (no_cleanup)
87 _exit(0);
88 else
89 exit(0);
90 _endthread();
91}
92
Guido van Rossum65d5b571998-12-21 19:32:43 +000093void PyThread_exit_thread(void)
Guido van Rossum62332931995-04-10 11:36:14 +000094{
Guido van Rossum65d5b571998-12-21 19:32:43 +000095 do_PyThread_exit_thread(0);
Guido van Rossum62332931995-04-10 11:36:14 +000096}
97
Guido van Rossum65d5b571998-12-21 19:32:43 +000098void PyThread__exit_thread(void)
Guido van Rossum62332931995-04-10 11:36:14 +000099{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000100 do_PyThread_exit_thread(1);
Guido van Rossum62332931995-04-10 11:36:14 +0000101}
102
103#ifndef NO_EXIT_PROG
Guido van Rossum65d5b571998-12-21 19:32:43 +0000104static void do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum62332931995-04-10 11:36:14 +0000105{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000106 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum62332931995-04-10 11:36:14 +0000107 if (!initialized)
108 if (no_cleanup)
109 _exit(status);
110 else
111 exit(status);
112}
113
Guido van Rossum65d5b571998-12-21 19:32:43 +0000114void PyThread_exit_prog(int status)
Guido van Rossum62332931995-04-10 11:36:14 +0000115{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000116 do_PyThread_exit_prog(status, 0);
Guido van Rossum62332931995-04-10 11:36:14 +0000117}
118
Guido van Rossum65d5b571998-12-21 19:32:43 +0000119void PyThread__exit_prog _P1(int status)
Guido van Rossum62332931995-04-10 11:36:14 +0000120{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000121 do_PyThread_exit_prog(status, 1);
Guido van Rossum62332931995-04-10 11:36:14 +0000122}
123#endif /* NO_EXIT_PROG */
124
125/*
126 * Lock support. It has too be implemented as semaphores.
127 * I [Dag] tried to implement it with mutex but I could find a way to
128 * tell whether a thread already own the lock or not.
129 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000130PyThread_type_lock PyThread_allocate_lock(void)
Guido van Rossum62332931995-04-10 11:36:14 +0000131{
132 HMTX aLock;
133 APIRET rc;
134
Guido van Rossum65d5b571998-12-21 19:32:43 +0000135 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum62332931995-04-10 11:36:14 +0000136 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000137 PyThread_init_thread();
Guido van Rossum62332931995-04-10 11:36:14 +0000138
139 DosCreateMutexSem(NULL, /* Sem name */
140 &aLock, /* the semaphone */
141 0, /* shared ? */
142 0); /* initial state */
143
Guido van Rossum65d5b571998-12-21 19:32:43 +0000144 dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", PyThread_get_thread_ident(), (long)aLock));
Guido van Rossum62332931995-04-10 11:36:14 +0000145
Guido van Rossum65d5b571998-12-21 19:32:43 +0000146 return (PyThread_type_lock) aLock;
Guido van Rossum62332931995-04-10 11:36:14 +0000147}
148
Guido van Rossum65d5b571998-12-21 19:32:43 +0000149void PyThread_free_lock(PyThread_type_lock aLock)
Guido van Rossum62332931995-04-10 11:36:14 +0000150{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000151 dprintf(("%ld: PyThread_free_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
Guido van Rossum62332931995-04-10 11:36:14 +0000152
153 DosCloseMutexSem((HMTX)aLock);
154}
155
156/*
157 * Return 1 on success if the lock was acquired
158 *
159 * and 0 if the lock was not acquired. This means a 0 is returned
160 * if the lock has already been acquired by this thread!
161 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000162int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
Guido van Rossum62332931995-04-10 11:36:14 +0000163{
164 int success = 1;
165 ULONG rc, count;
166 PID pid = 0;
167 TID tid = 0;
168
Guido van Rossum65d5b571998-12-21 19:32:43 +0000169 dprintf(("%ld: PyThread_acquire_lock(%lx, %d) called\n", PyThread_get_thread_ident(),
Guido van Rossum62332931995-04-10 11:36:14 +0000170 (long)aLock, waitflag));
171
172 DosQueryMutexSem((HMTX)aLock,&pid,&tid,&count);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000173 if( tid == PyThread_get_thread_ident() ) { /* if we own this lock */
Guido van Rossum62332931995-04-10 11:36:14 +0000174 success = 0;
175 } else {
176 rc = DosRequestMutexSem((HMTX) aLock,
177 (waitflag == 1 ? SEM_INDEFINITE_WAIT : 0));
178
179 if( rc != 0) {
180 success = 0; /* We failed */
181 }
182 }
183
Guido van Rossum65d5b571998-12-21 19:32:43 +0000184 dprintf(("%ld: PyThread_acquire_lock(%lx, %d) -> %d\n",
185 PyThread_get_thread_ident(),(long)aLock, waitflag, success));
Guido van Rossum62332931995-04-10 11:36:14 +0000186
187 return success;
188}
189
Guido van Rossum65d5b571998-12-21 19:32:43 +0000190void PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossum62332931995-04-10 11:36:14 +0000191{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000192 dprintf(("%ld: PyThread_release_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
Guido van Rossum62332931995-04-10 11:36:14 +0000193
194 if ( DosReleaseMutexSem( (HMTX) aLock ) != 0 ) {
Guido van Rossum65d5b571998-12-21 19:32:43 +0000195 dprintf(("%ld: Could not PyThread_release_lock(%lx) error: %l\n",
196 PyThread_get_thread_ident(), (long)aLock, GetLastError()));
Guido van Rossum62332931995-04-10 11:36:14 +0000197 }
198}
199
200/*
201 * Semaphore support.
202 */
Guido van Rossum65d5b571998-12-21 19:32:43 +0000203PyThread_type_sema PyThread_allocate_sema(int value)
Guido van Rossum62332931995-04-10 11:36:14 +0000204{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000205 return (PyThread_type_sema) 0;
Guido van Rossum62332931995-04-10 11:36:14 +0000206}
207
Guido van Rossum65d5b571998-12-21 19:32:43 +0000208void PyThread_free_sema(PyThread_type_sema aSemaphore)
Guido van Rossum62332931995-04-10 11:36:14 +0000209{
210
211}
212
Guido van Rossum65d5b571998-12-21 19:32:43 +0000213int PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag)
Guido van Rossum62332931995-04-10 11:36:14 +0000214{
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000215 return -1;
Guido van Rossum62332931995-04-10 11:36:14 +0000216}
217
Guido van Rossum65d5b571998-12-21 19:32:43 +0000218void PyThread_up_sema(PyThread_type_sema aSemaphore)
Guido van Rossum62332931995-04-10 11:36:14 +0000219{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000220 dprintf(("%ld: PyThread_up_sema(%lx)\n", PyThread_get_thread_ident(), (long)aSemaphore));
Guido van Rossum62332931995-04-10 11:36:14 +0000221}