blob: d95d7655c3fc2a033ad546ebe8ffa652ea4b8b0a [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
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007Copyright (c) 2000, BeOpen.com.
8Copyright (c) 1995-2000, Corporation for National Research Initiatives.
9Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
10All rights reserved.
Guido van Rossumd266eb41996-10-25 14:44:06 +000011
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000012See the file "Misc/COPYRIGHT" for information on usage and
13redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum62332931995-04-10 11:36:14 +000014
Guido van Rossumd266eb41996-10-25 14:44:06 +000015******************************************************************/
Guido van Rossum62332931995-04-10 11:36:14 +000016
17/* This code implemented by cvale@netcom.com */
18
19#define INCL_DOSPROCESS
20#define INCL_DOSSEMAPHORES
21#include "os2.h"
22#include "limits.h"
23
24#include "process.h"
25
Guido van Rossum65d5b571998-12-21 19:32:43 +000026long PyThread_get_thread_ident(void);
Guido van Rossum62332931995-04-10 11:36:14 +000027
28
29/*
30 * Initialization of the C package, should not be needed.
31 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000032static void
33PyThread__init_thread(void)
Guido van Rossum62332931995-04-10 11:36:14 +000034{
35}
36
37/*
38 * Thread support.
39 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000040int
41PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum62332931995-04-10 11:36:14 +000042{
43 int aThread;
44 int success = 1;
45
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000046 aThread = _beginthread(func,NULL,65536,arg);
Guido van Rossum62332931995-04-10 11:36:14 +000047
48 if( aThread == -1 ) {
49 success = 0;
50 fprintf(stderr,"aThread failed == %d",aThread);
51 dprintf(("_beginthread failed. return %ld\n", errno));
52 }
53
54 return success;
55}
56
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000057long
58PyThread_get_thread_ident(void)
Guido van Rossum62332931995-04-10 11:36:14 +000059{
60 PPIB pib;
61 PTIB tib;
62
63 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +000064 PyThread_init_thread();
Guido van Rossum62332931995-04-10 11:36:14 +000065
66 DosGetInfoBlocks(&tib,&pib);
67 return tib->tib_ptib2->tib2_ultid;
68}
69
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000070static void
71do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum62332931995-04-10 11:36:14 +000072{
Guido van Rossum65d5b571998-12-21 19:32:43 +000073 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
Guido van Rossum62332931995-04-10 11:36:14 +000074 if (!initialized)
75 if (no_cleanup)
76 _exit(0);
77 else
78 exit(0);
79 _endthread();
80}
81
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000082void
83PyThread_exit_thread(void)
Guido van Rossum62332931995-04-10 11:36:14 +000084{
Guido van Rossum65d5b571998-12-21 19:32:43 +000085 do_PyThread_exit_thread(0);
Guido van Rossum62332931995-04-10 11:36:14 +000086}
87
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000088void
89PyThread__exit_thread(void)
Guido van Rossum62332931995-04-10 11:36:14 +000090{
Guido van Rossum65d5b571998-12-21 19:32:43 +000091 do_PyThread_exit_thread(1);
Guido van Rossum62332931995-04-10 11:36:14 +000092}
93
94#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000095static void
96do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum62332931995-04-10 11:36:14 +000097{
Guido van Rossum65d5b571998-12-21 19:32:43 +000098 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum62332931995-04-10 11:36:14 +000099 if (!initialized)
100 if (no_cleanup)
101 _exit(status);
102 else
103 exit(status);
104}
105
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000106void
107PyThread_exit_prog(int status)
Guido van Rossum62332931995-04-10 11:36:14 +0000108{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000109 do_PyThread_exit_prog(status, 0);
Guido van Rossum62332931995-04-10 11:36:14 +0000110}
111
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000112void
113PyThread__exit_prog(int status)
Guido van Rossum62332931995-04-10 11:36:14 +0000114{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000115 do_PyThread_exit_prog(status, 1);
Guido van Rossum62332931995-04-10 11:36:14 +0000116}
117#endif /* NO_EXIT_PROG */
118
119/*
120 * Lock support. It has too be implemented as semaphores.
121 * I [Dag] tried to implement it with mutex but I could find a way to
122 * tell whether a thread already own the lock or not.
123 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000124PyThread_type_lock
125PyThread_allocate_lock(void)
Guido van Rossum62332931995-04-10 11:36:14 +0000126{
127 HMTX aLock;
128 APIRET rc;
129
Guido van Rossum65d5b571998-12-21 19:32:43 +0000130 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum62332931995-04-10 11:36:14 +0000131 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000132 PyThread_init_thread();
Guido van Rossum62332931995-04-10 11:36:14 +0000133
134 DosCreateMutexSem(NULL, /* Sem name */
Thomas Wouters7e474022000-07-16 12:04:32 +0000135 &aLock, /* the semaphore */
Guido van Rossum62332931995-04-10 11:36:14 +0000136 0, /* shared ? */
137 0); /* initial state */
138
Fred Drakea44d3532000-06-30 15:01:00 +0000139 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
Guido van Rossum62332931995-04-10 11:36:14 +0000140
Guido van Rossum65d5b571998-12-21 19:32:43 +0000141 return (PyThread_type_lock) aLock;
Guido van Rossum62332931995-04-10 11:36:14 +0000142}
143
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000144void
145PyThread_free_lock(PyThread_type_lock aLock)
Guido van Rossum62332931995-04-10 11:36:14 +0000146{
Fred Drakea44d3532000-06-30 15:01:00 +0000147 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossum62332931995-04-10 11:36:14 +0000148
149 DosCloseMutexSem((HMTX)aLock);
150}
151
152/*
153 * Return 1 on success if the lock was acquired
154 *
155 * and 0 if the lock was not acquired. This means a 0 is returned
156 * if the lock has already been acquired by this thread!
157 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000158int
159PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
Guido van Rossum62332931995-04-10 11:36:14 +0000160{
161 int success = 1;
162 ULONG rc, count;
163 PID pid = 0;
164 TID tid = 0;
165
Fred Drakea44d3532000-06-30 15:01:00 +0000166 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),
167 aLock, waitflag));
Guido van Rossum62332931995-04-10 11:36:14 +0000168
169 DosQueryMutexSem((HMTX)aLock,&pid,&tid,&count);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000170 if( tid == PyThread_get_thread_ident() ) { /* if we own this lock */
Guido van Rossum62332931995-04-10 11:36:14 +0000171 success = 0;
172 } else {
173 rc = DosRequestMutexSem((HMTX) aLock,
174 (waitflag == 1 ? SEM_INDEFINITE_WAIT : 0));
175
176 if( rc != 0) {
177 success = 0; /* We failed */
178 }
179 }
180
Fred Drakea44d3532000-06-30 15:01:00 +0000181 dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n",
182 PyThread_get_thread_ident(),aLock, waitflag, success));
Guido van Rossum62332931995-04-10 11:36:14 +0000183
184 return success;
185}
186
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000187void
188PyThread_release_lock(PyThread_type_lock aLock)
Guido van Rossum62332931995-04-10 11:36:14 +0000189{
Fred Drakea44d3532000-06-30 15:01:00 +0000190 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
Guido van Rossum62332931995-04-10 11:36:14 +0000191
192 if ( DosReleaseMutexSem( (HMTX) aLock ) != 0 ) {
Fred Drakea44d3532000-06-30 15:01:00 +0000193 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n",
194 PyThread_get_thread_ident(), aLock, GetLastError()));
Guido van Rossum62332931995-04-10 11:36:14 +0000195 }
196}
197
198/*
199 * Semaphore support.
200 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000201PyThread_type_sema
202PyThread_allocate_sema(int value)
Guido van Rossum62332931995-04-10 11:36:14 +0000203{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000204 return (PyThread_type_sema) 0;
Guido van Rossum62332931995-04-10 11:36:14 +0000205}
206
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000207void
208PyThread_free_sema(PyThread_type_sema aSemaphore)
Guido van Rossum62332931995-04-10 11:36:14 +0000209{
210
211}
212
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000213int
214PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag)
Guido van Rossum62332931995-04-10 11:36:14 +0000215{
Guido van Rossumcf1474b1996-10-08 14:17:53 +0000216 return -1;
Guido van Rossum62332931995-04-10 11:36:14 +0000217}
218
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219void
220PyThread_up_sema(PyThread_type_sema aSemaphore)
Guido van Rossum62332931995-04-10 11:36:14 +0000221{
Fred Drakea44d3532000-06-30 15:01:00 +0000222 dprintf(("%ld: PyThread_up_sema(%p)\n", PyThread_get_thread_ident(), aSemaphore));
Guido van Rossum62332931995-04-10 11:36:14 +0000223}