blob: f9eda928dbaf5e0ffc4d62ac9f1f2bd92f653351 [file] [log] [blame]
Guido van Rossum62332931995-04-10 11:36:14 +00001/***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
4
5 All Rights Reserved
6
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
14
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23 ******************************************************************/
24
25/* This code implemented by cvale@netcom.com */
26
27#define INCL_DOSPROCESS
28#define INCL_DOSSEMAPHORES
29#include "os2.h"
30#include "limits.h"
31
32#include "process.h"
33
34long get_thread_ident(void);
35
36
37/*
38 * Initialization of the C package, should not be needed.
39 */
40static void _init_thread(void)
41{
42}
43
44/*
45 * Thread support.
46 */
47int start_new_thread(void (*func)(void *), void *arg)
48{
49 int aThread;
50 int success = 1;
51
52 aThread = _beginthread(func,4096,arg);
53
54 if( aThread == -1 ) {
55 success = 0;
56 fprintf(stderr,"aThread failed == %d",aThread);
57 dprintf(("_beginthread failed. return %ld\n", errno));
58 }
59
60 return success;
61}
62
63long get_thread_ident(void)
64{
65 PPIB pib;
66 PTIB tib;
67
68 if (!initialized)
69 init_thread();
70
71 DosGetInfoBlocks(&tib,&pib);
72 return tib->tib_ptib2->tib2_ultid;
73}
74
75static void do_exit_thread(int no_cleanup)
76{
77 dprintf(("%ld: exit_thread called\n", get_thread_ident()));
78 if (!initialized)
79 if (no_cleanup)
80 _exit(0);
81 else
82 exit(0);
83 _endthread();
84}
85
86void exit_thread(void)
87{
88 do_exit_thread(0);
89}
90
91void _exit_thread(void)
92{
93 do_exit_thread(1);
94}
95
96#ifndef NO_EXIT_PROG
97static void do_exit_prog(int status, int no_cleanup)
98{
99 dprintf(("exit_prog(%d) called\n", status));
100 if (!initialized)
101 if (no_cleanup)
102 _exit(status);
103 else
104 exit(status);
105}
106
107void exit_prog(int status)
108{
109 do_exit_prog(status, 0);
110}
111
112void _exit_prog _P1(int status)
113{
114 do_exit_prog(status, 1);
115}
116#endif /* NO_EXIT_PROG */
117
118/*
119 * Lock support. It has too be implemented as semaphores.
120 * I [Dag] tried to implement it with mutex but I could find a way to
121 * tell whether a thread already own the lock or not.
122 */
123type_lock allocate_lock(void)
124{
125 HMTX aLock;
126 APIRET rc;
127
128 dprintf(("allocate_lock called\n"));
129 if (!initialized)
130 init_thread();
131
132 DosCreateMutexSem(NULL, /* Sem name */
133 &aLock, /* the semaphone */
134 0, /* shared ? */
135 0); /* initial state */
136
137 dprintf(("%ld: allocate_lock() -> %lx\n", get_thread_ident(), (long)aLock));
138
139 return (type_lock) aLock;
140}
141
142void free_lock(type_lock aLock)
143{
144 dprintf(("%ld: free_lock(%lx) called\n", get_thread_ident(),(long)aLock));
145
146 DosCloseMutexSem((HMTX)aLock);
147}
148
149/*
150 * Return 1 on success if the lock was acquired
151 *
152 * and 0 if the lock was not acquired. This means a 0 is returned
153 * if the lock has already been acquired by this thread!
154 */
155int acquire_lock(type_lock aLock, int waitflag)
156{
157 int success = 1;
158 ULONG rc, count;
159 PID pid = 0;
160 TID tid = 0;
161
162 dprintf(("%ld: acquire_lock(%lx, %d) called\n", get_thread_ident(),
163 (long)aLock, waitflag));
164
165 DosQueryMutexSem((HMTX)aLock,&pid,&tid,&count);
166 if( tid == get_thread_ident() ) { /* if we own this lock */
167 success = 0;
168 } else {
169 rc = DosRequestMutexSem((HMTX) aLock,
170 (waitflag == 1 ? SEM_INDEFINITE_WAIT : 0));
171
172 if( rc != 0) {
173 success = 0; /* We failed */
174 }
175 }
176
177 dprintf(("%ld: acquire_lock(%lx, %d) -> %d\n",
178 get_thread_ident(),(long)aLock, waitflag, success));
179
180 return success;
181}
182
183void release_lock(type_lock aLock)
184{
185 dprintf(("%ld: release_lock(%lx) called\n", get_thread_ident(),(long)aLock));
186
187 if ( DosReleaseMutexSem( (HMTX) aLock ) != 0 ) {
188 dprintf(("%ld: Could not release_lock(%lx) error: %l\n",
189 get_thread_ident(), (long)aLock, GetLastError()));
190 }
191}
192
193/*
194 * Semaphore support.
195 */
196type_sema allocate_sema(int value)
197{
198 return (type_sema) 0;
199}
200
201void free_sema(type_sema aSemaphore)
202{
203
204}
205
206void down_sema(type_sema aSemaphore)
207{
208
209}
210
211void up_sema(type_sema aSemaphore)
212{
213 dprintf(("%ld: up_sema(%lx)\n", get_thread_ident(), (long)aSemaphore));
214}