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 Dag.Gruneau@elsa.preseco.comm.se */
38 long PyThread_get_thread_ident(void);
41 * Change all headers to pure ANSI as no one will use K&R style on an
46 * Initialization of the C package, should not be needed.
48 static void PyThread__init_thread(void)
55 int PyThread_start_new_thread(void (*func
)(void *), void *arg
)
60 dprintf(("%ld: PyThread_start_new_thread called\n", PyThread_get_thread_ident()));
62 PyThread_init_thread();
64 rv
= _beginthread(func
, 0, arg
); /* use default stack size */
68 dprintf(("%ld: PyThread_start_new_thread succeeded: %ld\n", PyThread_get_thread_ident(), aThreadId
));
75 * Return the thread Id instead of an handle. The Id is said to uniquely identify the
76 * thread in the system
78 long PyThread_get_thread_ident(void)
81 PyThread_init_thread();
83 return GetCurrentThreadId();
86 static void do_PyThread_exit_thread(int no_cleanup
)
88 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
97 void PyThread_exit_thread(void)
99 do_PyThread_exit_thread(0);
102 void PyThread__exit_thread(void)
104 do_PyThread_exit_thread(1);
108 static void do_PyThread_exit_prog(int status
, int no_cleanup
)
110 dprintf(("PyThread_exit_prog(%d) called\n", status
));
118 void PyThread_exit_prog(int status
)
120 do_PyThread_exit_prog(status
, 0);
123 void PyThread__exit_prog
_P1(int status
)
125 do_PyThread_exit_prog(status
, 1);
127 #endif /* NO_EXIT_PROG */
130 * Lock support. It has too be implemented as semaphores.
131 * I [Dag] tried to implement it with mutex but I could find a way to
132 * tell whether a thread already own the lock or not.
134 PyThread_type_lock
PyThread_allocate_lock(void)
138 dprintf(("PyThread_allocate_lock called\n"));
140 PyThread_init_thread();
142 aLock
= CreateSemaphore(NULL
, /* Security attributes */
143 1, /* Initial value */
144 1, /* Maximum value */
146 /* Name of semaphore */
148 dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", PyThread_get_thread_ident(), (long)aLock
));
150 return (PyThread_type_lock
) aLock
;
153 void PyThread_free_lock(PyThread_type_lock aLock
)
155 dprintf(("%ld: PyThread_free_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock
));
157 CloseHandle((HANDLE
) aLock
);
161 * Return 1 on success if the lock was acquired
163 * and 0 if the lock was not acquired. This means a 0 is returned
164 * if the lock has already been acquired by this thread!
166 int PyThread_acquire_lock(PyThread_type_lock aLock
, int waitflag
)
171 dprintf(("%ld: PyThread_acquire_lock(%lx, %d) called\n", PyThread_get_thread_ident(),(long)aLock
, waitflag
));
173 waitResult
= WaitForSingleObject((HANDLE
) aLock
, (waitflag
== 1 ? INFINITE
: 0));
175 if (waitResult
!= WAIT_OBJECT_0
) {
176 success
= 0; /* We failed */
179 dprintf(("%ld: PyThread_acquire_lock(%lx, %d) -> %d\n", PyThread_get_thread_ident(),(long)aLock
, waitflag
, success
));
184 void PyThread_release_lock(PyThread_type_lock aLock
)
186 dprintf(("%ld: PyThread_release_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock
));
188 if (!ReleaseSemaphore(
189 (HANDLE
) aLock
, /* Handle of semaphore */
190 1, /* increment count by one */
191 NULL
)) /* not interested in previous count */
193 dprintf(("%ld: Could not PyThread_release_lock(%lx) error: %l\n", PyThread_get_thread_ident(), (long)aLock
, GetLastError()));
200 PyThread_type_sema
PyThread_allocate_sema(int value
)
204 dprintf(("%ld: PyThread_allocate_sema called\n", PyThread_get_thread_ident()));
206 PyThread_init_thread();
208 aSemaphore
= CreateSemaphore( NULL
, /* Security attributes */
209 value
, /* Initial value */
210 INT_MAX
, /* Maximum value */
211 NULL
); /* Name of semaphore */
213 dprintf(("%ld: PyThread_allocate_sema() -> %lx\n", PyThread_get_thread_ident(), (long)aSemaphore
));
215 return (PyThread_type_sema
) aSemaphore
;
218 void PyThread_free_sema(PyThread_type_sema aSemaphore
)
220 dprintf(("%ld: PyThread_free_sema(%lx) called\n", PyThread_get_thread_ident(), (long)aSemaphore
));
222 CloseHandle((HANDLE
) aSemaphore
);
226 XXX must do something about waitflag
228 int PyThread_down_sema(PyThread_type_sema aSemaphore
, int waitflag
)
232 dprintf(("%ld: PyThread_down_sema(%lx) called\n", PyThread_get_thread_ident(), (long)aSemaphore
));
234 waitResult
= WaitForSingleObject( (HANDLE
) aSemaphore
, INFINITE
);
236 dprintf(("%ld: PyThread_down_sema(%lx) return: %l\n", PyThread_get_thread_ident(),(long) aSemaphore
, waitResult
));
240 void PyThread_up_sema(PyThread_type_sema aSemaphore
)
243 (HANDLE
) aSemaphore
, /* Handle of semaphore */
244 1, /* increment count by one */
245 NULL
); /* not interested in previous count */
247 dprintf(("%ld: PyThread_up_sema(%lx)\n", PyThread_get_thread_ident(), (long)aSemaphore
));