1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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 or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* This code implemented by cvale@netcom.com */
34 #define INCL_DOSPROCESS
35 #define INCL_DOSSEMAPHORES
41 long PyThread_get_thread_ident(void);
45 * Initialization of the C package, should not be needed.
47 static void PyThread__init_thread(void)
54 int PyThread_start_new_thread(void (*func
)(void *), void *arg
)
59 aThread
= _beginthread(func
,NULL
,65536,arg
);
63 fprintf(stderr
,"aThread failed == %d",aThread
);
64 dprintf(("_beginthread failed. return %ld\n", errno
));
70 long PyThread_get_thread_ident(void)
76 PyThread_init_thread();
78 DosGetInfoBlocks(&tib
,&pib
);
79 return tib
->tib_ptib2
->tib2_ultid
;
82 static void do_PyThread_exit_thread(int no_cleanup
)
84 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
93 void PyThread_exit_thread(void)
95 do_PyThread_exit_thread(0);
98 void PyThread__exit_thread(void)
100 do_PyThread_exit_thread(1);
104 static void do_PyThread_exit_prog(int status
, int no_cleanup
)
106 dprintf(("PyThread_exit_prog(%d) called\n", status
));
114 void PyThread_exit_prog(int status
)
116 do_PyThread_exit_prog(status
, 0);
119 void PyThread__exit_prog
_P1(int status
)
121 do_PyThread_exit_prog(status
, 1);
123 #endif /* NO_EXIT_PROG */
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.
130 PyThread_type_lock
PyThread_allocate_lock(void)
135 dprintf(("PyThread_allocate_lock called\n"));
137 PyThread_init_thread();
139 DosCreateMutexSem(NULL
, /* Sem name */
140 &aLock
, /* the semaphone */
142 0); /* initial state */
144 dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", PyThread_get_thread_ident(), (long)aLock
));
146 return (PyThread_type_lock
) aLock
;
149 void PyThread_free_lock(PyThread_type_lock aLock
)
151 dprintf(("%ld: PyThread_free_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock
));
153 DosCloseMutexSem((HMTX
)aLock
);
157 * Return 1 on success if the lock was acquired
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!
162 int PyThread_acquire_lock(PyThread_type_lock aLock
, int waitflag
)
169 dprintf(("%ld: PyThread_acquire_lock(%lx, %d) called\n", PyThread_get_thread_ident(),
170 (long)aLock
, waitflag
));
172 DosQueryMutexSem((HMTX
)aLock
,&pid
,&tid
,&count
);
173 if( tid
== PyThread_get_thread_ident() ) { /* if we own this lock */
176 rc
= DosRequestMutexSem((HMTX
) aLock
,
177 (waitflag
== 1 ? SEM_INDEFINITE_WAIT
: 0));
180 success
= 0; /* We failed */
184 dprintf(("%ld: PyThread_acquire_lock(%lx, %d) -> %d\n",
185 PyThread_get_thread_ident(),(long)aLock
, waitflag
, success
));
190 void PyThread_release_lock(PyThread_type_lock aLock
)
192 dprintf(("%ld: PyThread_release_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock
));
194 if ( DosReleaseMutexSem( (HMTX
) aLock
) != 0 ) {
195 dprintf(("%ld: Could not PyThread_release_lock(%lx) error: %l\n",
196 PyThread_get_thread_ident(), (long)aLock
, GetLastError()));
203 PyThread_type_sema
PyThread_allocate_sema(int value
)
205 return (PyThread_type_sema
) 0;
208 void PyThread_free_sema(PyThread_type_sema aSemaphore
)
213 int PyThread_down_sema(PyThread_type_sema aSemaphore
, int waitflag
)
218 void PyThread_up_sema(PyThread_type_sema aSemaphore
)
220 dprintf(("%ld: PyThread_up_sema(%lx)\n", PyThread_get_thread_ident(), (long)aSemaphore
));