Mercurial > cpython-lru_cache-weakref
annotate Python/thread_pth.h @ 13210:ff243b0f16ee legacy-trunk
Trent Mick <trentm@activestate.com>:
The common technique for printing out a pointer has been to cast to a long
and use the "%lx" printf modifier. This is incorrect on Win64 where casting
to a long truncates the pointer. The "%p" formatter should be used instead.
The problem as stated by Tim:
> Unfortunately, the C committee refused to define what %p conversion "looks
> like" -- they explicitly allowed it to be implementation-defined. Older
> versions of Microsoft C even stuck a colon in the middle of the address (in
> the days of segment+offset addressing)!
The result is that the hex value of a pointer will maybe/maybe not have a 0x
prepended to it.
Notes on the patch:
There are two main classes of changes:
- in the various repr() functions that print out pointers
- debugging printf's in the various thread_*.h files (these are why the
patch is large)
Closes SourceForge patch #100505.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Fri, 30 Jun 2000 15:01:00 +0000 |
| parents | b320cf3a98b9 |
| children | 916fb024ce94 |
| rev | line source |
|---|---|
|
12611
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 /*********************************************************** |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 The Netherlands. |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 All Rights Reserved |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 provided that the above copyright notice appear in all copies and that |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 supporting documentation, and that the names of Stichting Mathematisch |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 distribution of the software without specific, written prior |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 permission. |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 ******************************************************************/ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 /* GNU pth threads interface |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 http://www.gnu.org/software/pth |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 2000-05-03 Andy Dustman <andy@dustman.net> |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 Adapted from Posix threads interface |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 12 May 1997 -- david arnold <davida@pobox.com> |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 */ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 #include <stdlib.h> |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 #include <string.h> |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 #include <pth.h> |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 /* A pth mutex isn't sufficient to model the Python lock type |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 * because pth mutexes can be acquired multiple times by the |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 * same thread. |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 * |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 * The pth_lock struct implements a Python lock as a "locked?" bit |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 * and a <condition, mutex> pair. In general, if the bit can be acquired |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 * instantly, it is, else the pair is used to block the thread until the |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 * bit is cleared. |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 */ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 typedef struct { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 char locked; /* 0=unlocked, 1=locked */ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 /* a <cond, mutex> pair to handle an acquire of a locked lock */ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 pth_cond_t lock_released; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 pth_mutex_t mut; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 } pth_lock; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 #define CHECK_STATUS(name) if (status == -1) { printf("%d ", status); perror(name); error = 1; } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 /* |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 * Initialization. |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 */ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 static void PyThread__init_thread _P0() |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 pth_init(); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 /* |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 * Thread support. |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 */ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 int PyThread_start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 pth_t th; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 int success; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 dprintf(("PyThread_start_new_thread called\n")); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 if (!initialized) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 PyThread_init_thread(); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 th = pth_spawn(PTH_ATTR_DEFAULT, |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 (void* (*)_P((void *)))func, |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 (void *)arg |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 ); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 return th == NULL ? 0 : 1; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 long PyThread_get_thread_ident _P0() |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 volatile pth_t threadid; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 if (!initialized) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 PyThread_init_thread(); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 /* Jump through some hoops for Alpha OSF/1 */ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 threadid = pth_self(); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 return (long) *(long *) &threadid; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 static void do_PyThread_exit_thread _P1(no_cleanup, int no_cleanup) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 dprintf(("PyThread_exit_thread called\n")); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 if (!initialized) { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 if (no_cleanup) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 _exit(0); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 else |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 exit(0); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 void PyThread_exit_thread _P0() |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 do_PyThread_exit_thread(0); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 void PyThread__exit_thread _P0() |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 do_PyThread_exit_thread(1); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 #ifndef NO_EXIT_PROG |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 static void do_PyThread_exit_prog _P2(status, int status, no_cleanup, int no_cleanup) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 dprintf(("PyThread_exit_prog(%d) called\n", status)); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 if (!initialized) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 if (no_cleanup) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 _exit(status); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 else |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 exit(status); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 void PyThread_exit_prog _P1(status, int status) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 do_PyThread_exit_prog(status, 0); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 void PyThread__exit_prog _P1(status, int status) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 do_PyThread_exit_prog(status, 1); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 #endif /* NO_EXIT_PROG */ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 /* |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 * Lock support. |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 */ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 PyThread_type_lock PyThread_allocate_lock _P0() |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 pth_lock *lock; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 int status, error = 0; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 dprintf(("PyThread_allocate_lock called\n")); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 if (!initialized) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 PyThread_init_thread(); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 lock = (pth_lock *) malloc(sizeof(pth_lock)); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 memset((void *)lock, '\0', sizeof(pth_lock)); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 if (lock) { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 lock->locked = 0; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 status = pth_mutex_init(&lock->mut); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 CHECK_STATUS("pth_mutex_init"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 status = pth_cond_init(&lock->lock_released); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 CHECK_STATUS("pth_cond_init"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 if (error) { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 free((void *)lock); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 lock = NULL; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 } |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
12611
diff
changeset
|
171 dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
|
12611
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 return (PyThread_type_lock) lock; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 void PyThread_free_lock _P1(lock, PyThread_type_lock lock) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 pth_lock *thelock = (pth_lock *)lock; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 int status, error = 0; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
12611
diff
changeset
|
180 dprintf(("PyThread_free_lock(%p) called\n", lock)); |
|
12611
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 free((void *)thelock); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 int PyThread_acquire_lock _P2(lock, PyThread_type_lock lock, waitflag, int waitflag) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 int success; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 pth_lock *thelock = (pth_lock *)lock; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 int status, error = 0; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
12611
diff
changeset
|
191 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
|
12611
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 status = pth_mutex_acquire(&thelock->mut, !waitflag, NULL); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 CHECK_STATUS("pth_mutex_acquire[1]"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 success = thelock->locked == 0; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 if (success) thelock->locked = 1; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 status = pth_mutex_release( &thelock->mut ); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 CHECK_STATUS("pth_mutex_release[1]"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 if ( !success && waitflag ) { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 /* continue trying until we get the lock */ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 /* mut must be locked by me -- part of the condition |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 * protocol */ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 status = pth_mutex_acquire( &thelock->mut, !waitflag, NULL ); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 CHECK_STATUS("pth_mutex_acquire[2]"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 while ( thelock->locked ) { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 status = pth_cond_await(&thelock->lock_released, |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 &thelock->mut, NULL); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 CHECK_STATUS("pth_cond_await"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 thelock->locked = 1; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 status = pth_mutex_release( &thelock->mut ); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 CHECK_STATUS("pth_mutex_release[2]"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 success = 1; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 if (error) success = 0; |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
12611
diff
changeset
|
218 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
|
12611
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 return success; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 void PyThread_release_lock _P1(lock, PyThread_type_lock lock) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 pth_lock *thelock = (pth_lock *)lock; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 int status, error = 0; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
12611
diff
changeset
|
227 dprintf(("PyThread_release_lock(%p) called\n", lock)); |
|
12611
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 status = pth_mutex_acquire( &thelock->mut, 0, NULL ); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 CHECK_STATUS("pth_mutex_acquire[3]"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 thelock->locked = 0; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 status = pth_mutex_release( &thelock->mut ); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 CHECK_STATUS("pth_mutex_release[3]"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 /* wake up someone (anyone, if any) waiting on the lock */ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 status = pth_cond_notify( &thelock->lock_released, 0 ); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 CHECK_STATUS("pth_cond_notify"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 /* |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 * Semaphore support. |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 */ |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 struct semaphore { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 pth_mutex_t mutex; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 pth_cond_t cond; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 int value; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 }; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 PyThread_type_sema PyThread_allocate_sema _P1(value, int value) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 struct semaphore *sema; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 int status, error = 0; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 dprintf(("PyThread_allocate_sema called\n")); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 if (!initialized) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 PyThread_init_thread(); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 sema = (struct semaphore *) malloc(sizeof(struct semaphore)); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 if (sema != NULL) { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 sema->value = value; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 status = pth_mutex_init(&sema->mutex); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 CHECK_STATUS("pth_mutex_init"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 status = pth_cond_init(&sema->cond); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 CHECK_STATUS("pth_mutex_init"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 if (error) { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 free((void *) sema); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 sema = NULL; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 } |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
12611
diff
changeset
|
273 dprintf(("PyThread_allocate_sema() -> %p\n", sema)); |
|
12611
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 return (PyThread_type_sema) sema; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 void PyThread_free_sema _P1(sema, PyThread_type_sema sema) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 int status, error = 0; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 struct semaphore *thesema = (struct semaphore *) sema; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
12611
diff
changeset
|
282 dprintf(("PyThread_free_sema(%p) called\n", sema)); |
|
12611
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 free((void *) thesema); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 int PyThread_down_sema _P2(sema, PyThread_type_sema sema, waitflag, int waitflag) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 int status, error = 0, success; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 struct semaphore *thesema = (struct semaphore *) sema; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
12611
diff
changeset
|
291 dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag)); |
|
12611
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 status = pth_mutex_acquire(&thesema->mutex, !waitflag, NULL); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 CHECK_STATUS("pth_mutex_acquire"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 if (waitflag) { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 while (!error && thesema->value <= 0) { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 status = pth_cond_await(&thesema->cond, |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 &thesema->mutex, NULL); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 CHECK_STATUS("pth_cond_await"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 if (error) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 success = 0; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 else if (thesema->value > 0) { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 thesema->value--; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 success = 1; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 else |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 success = 0; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 status = pth_mutex_release(&thesema->mutex); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 CHECK_STATUS("pth_mutex_release"); |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
12611
diff
changeset
|
311 dprintf(("PyThread_down_sema(%p) return\n", sema)); |
|
12611
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 return success; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 } |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 void PyThread_up_sema _P1(sema, PyThread_type_sema sema) |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 { |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 int status, error = 0; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 struct semaphore *thesema = (struct semaphore *) sema; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 |
|
13210
ff243b0f16ee
Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents:
12611
diff
changeset
|
320 dprintf(("PyThread_up_sema(%p)\n", sema)); |
|
12611
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 status = pth_mutex_acquire(&thesema->mutex, 0, NULL); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 CHECK_STATUS("pth_mutex_acquire"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 thesema->value++; |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 status = pth_cond_notify(&thesema->cond, 1); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 CHECK_STATUS("pth_cond_notify"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 status = pth_mutex_release(&thesema->mutex); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 CHECK_STATUS("pth_mutex_release"); |
|
b320cf3a98b9
Andy Dustman: add GNU pth user-space thread support.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 } |
