annotate Python/thread_pthread.h @ 5493:ef93d51dab36 legacy-trunk

Support for various versions of the pthread draft.
author Guido van Rossum <guido@python.org>
date Tue, 13 May 1997 17:51:13 +0000
parents 2c81d7d68ed3
children f5decc5d7073
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /***********************************************************
2062
d0d0a69b5ca3 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1651
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
d0d0a69b5ca3 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1651
diff changeset
3 The Netherlands.
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
8 documentation for any purpose and without fee is hereby granted,
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
10 both that copyright notice and this permission notice appear in
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
15 permission.
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4371
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 ******************************************************************/
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
3663
07164d30429d Got rid of florida hack and made it work with Solaris 2.5 pthreads.
Guido van Rossum <guido@python.org>
parents: 2097
diff changeset
32 /* Posix threads interface */
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
3663
07164d30429d Got rid of florida hack and made it work with Solaris 2.5 pthreads.
Guido van Rossum <guido@python.org>
parents: 2097
diff changeset
34 #include <stdlib.h>
07164d30429d Got rid of florida hack and made it work with Solaris 2.5 pthreads.
Guido van Rossum <guido@python.org>
parents: 2097
diff changeset
35 #include <pthread.h>
07164d30429d Got rid of florida hack and made it work with Solaris 2.5 pthreads.
Guido van Rossum <guido@python.org>
parents: 2097
diff changeset
36
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
37
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
38 /* try to determine what version of the Pthread Standard is installed.
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
39 * this is important, since all sorts of parameter types changed from
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
40 * draft to draft and there are several (incompatible) drafts in
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
41 * common use. these macros are a start, at least.
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
42 * 12 May 1997 -- david arnold <davida@pobox.com>
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
43 */
3965
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
44
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
45 #if defined(__ultrix) && defined(__mips) && defined(_DECTHREADS_)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
46 /* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
47 # define PY_PTHREAD_D4
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
48
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
49 #elif defined(__osf__) && defined (__alpha)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
50 /* _DECTHREADS_ is defined in cma.h which is included by pthread.h */
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
51 # if !defined(_PTHREAD_ENV_ALPHA) || defined(_PTHREAD_USE_D4) || defined(PTHREAD_USE_D4)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
52 # define PY_PTHREAD_D4
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
53 # else
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
54 # define PY_PTHREAD_STD
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
55 # endif
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
56
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
57 #elif defined(_AIX)
3965
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
58 /* SCHED_BG_NP is defined if using AIX DCE pthreads
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
59 * but it is unsupported by AIX 4 pthreads. Default
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
60 * attributes for AIX 4 pthreads equal to NULL. For
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
61 * AIX DCE pthreads they should be left unchanged.
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
62 */
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
63 # if !defined(SCHED_BG_NP)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
64 # define PY_PTHREAD_STD
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
65 # else
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
66 # define PY_PTHREAD_D7
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
67 # endif
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
68
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
69 #elif defined(__unix) && defined(__sparc)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
70 # define PY_PTHREAD_STD
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
71
3965
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
72 #endif
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
73
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
74
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
75 /* set default attribute object for different versions */
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
76
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
77 #if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D7)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
78 # define pthread_attr_default pthread_attr_default
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
79 # define pthread_mutexattr_default pthread_mutexattr_default
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
80 # define pthread_condattr_default pthread_condattr_default
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
81 #elif defined(PY_PTHREAD_STD)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
82 # define pthread_attr_default ((pthread_attr_t *)NULL)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
83 # define pthread_mutexattr_default ((pthread_mutexattr_t *)NULL)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
84 # define pthread_condattr_default ((pthread_condattr_t *)NULL)
3965
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
85 #endif
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
87
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
88 /* A pthread mutex isn't sufficient to model the Python lock type
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
89 * because, according to Draft 5 of the docs (P1003.4a/D5), both of the
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
90 * following are undefined:
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
91 * -> a thread tries to lock a mutex it already has locked
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
92 * -> a thread tries to unlock a mutex locked by a different thread
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
93 * pthread mutexes are designed for serializing threads over short pieces
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
94 * of code anyway, so wouldn't be an appropriate implementation of
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 * Python's locks regardless.
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
96 *
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
97 * The pthread_lock struct implements a Python lock as a "locked?" bit
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
98 * and a <condition, mutex> pair. In general, if the bit can be acquired
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
99 * instantly, it is, else the pair is used to block the thread until the
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
100 * bit is cleared. 9 May 1994 tim@ksr.com
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 */
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
102
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 typedef struct {
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
104 char locked; /* 0=unlocked, 1=locked */
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
105 /* a <cond, mutex> pair to handle an acquire of a locked lock */
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
106 pthread_cond_t lock_released;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
107 pthread_mutex_t mut;
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 } pthread_lock;
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
110 #define CHECK_STATUS(name) if (status < 0) { perror(name); error=1; }
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
111
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 /*
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 * Initialization.
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 */
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 static void _init_thread _P0()
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 {
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 /*
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 * Thread support.
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 */
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
122
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
123
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 {
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 pthread_t th;
1651
7e5f97bab1e6 ceval.c: dict of local mapping is now a tuple
Guido van Rossum <guido@python.org>
parents: 1629
diff changeset
127 int success;
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 dprintf(("start_new_thread called\n"));
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 if (!initialized)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 init_thread();
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
131
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
132 success = pthread_create(&th,
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
133 #if defined(PY_PTHREAD_D4)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
134 pthread_attr_default,
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
135 (pthread_startroutine_t)func,
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
136 (pthread_addr_t)arg
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
137 #elif defined(PY_PTHREAD_D7)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
138 pthread_attr_default,
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
139 func,
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
140 arg
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
141 #elif defined(PY_PTHREAD_STD)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
142 (pthread_attr_t*)NULL,
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
143 (void* (*)_P((void *)))func,
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
144 (void *)arg
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
145 #endif
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
146 );
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
147
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
148 if (success >= 0) {
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
149 #if defined(PY_THREAD_D4) || defined(PY_PTHREAD_D7)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
150 pthread_detach(&th);
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
151 #elif defined(PY_PTHREAD_STD)
5395
2c81d7d68ed3 Add detach call so threads are GC'ed.
Guido van Rossum <guido@python.org>
parents: 4994
diff changeset
152 pthread_detach(th);
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
153 #endif
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
154 }
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 return success < 0 ? 0 : 1;
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
1651
7e5f97bab1e6 ceval.c: dict of local mapping is now a tuple
Guido van Rossum <guido@python.org>
parents: 1629
diff changeset
158 long get_thread_ident _P0()
7e5f97bab1e6 ceval.c: dict of local mapping is now a tuple
Guido van Rossum <guido@python.org>
parents: 1629
diff changeset
159 {
2097
e4588404fdff Alpha OSF/1 fix
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
160 pthread_t threadid;
1651
7e5f97bab1e6 ceval.c: dict of local mapping is now a tuple
Guido van Rossum <guido@python.org>
parents: 1629
diff changeset
161 if (!initialized)
7e5f97bab1e6 ceval.c: dict of local mapping is now a tuple
Guido van Rossum <guido@python.org>
parents: 1629
diff changeset
162 init_thread();
2097
e4588404fdff Alpha OSF/1 fix
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
163 /* Jump through some hoops for Alpha OSF/1 */
e4588404fdff Alpha OSF/1 fix
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
164 threadid = pthread_self();
e4588404fdff Alpha OSF/1 fix
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
165 return (long) *(long *) &threadid;
1651
7e5f97bab1e6 ceval.c: dict of local mapping is now a tuple
Guido van Rossum <guido@python.org>
parents: 1629
diff changeset
166 }
7e5f97bab1e6 ceval.c: dict of local mapping is now a tuple
Guido van Rossum <guido@python.org>
parents: 1629
diff changeset
167
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 static void do_exit_thread _P1(no_cleanup, int no_cleanup)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 {
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 dprintf(("exit_thread called\n"));
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 if (!initialized)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 if (no_cleanup)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 _exit(0);
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 else
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 exit(0);
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 void exit_thread _P0()
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 {
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 do_exit_thread(0);
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 void _exit_thread _P0()
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 {
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 do_exit_thread(1);
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 #ifndef NO_EXIT_PROG
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 static void do_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 {
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 dprintf(("exit_prog(%d) called\n", status));
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 if (!initialized)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 if (no_cleanup)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 _exit(status);
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 else
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 exit(status);
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 void exit_prog _P1(status, int status)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 {
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 do_exit_prog(status, 0);
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 void _exit_prog _P1(status, int status)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 {
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 do_exit_prog(status, 1);
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 #endif /* NO_EXIT_PROG */
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 /*
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 * Lock support.
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 */
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 type_lock allocate_lock _P0()
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 {
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 pthread_lock *lock;
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
216 int status, error = 0;
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 dprintf(("allocate_lock called\n"));
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 if (!initialized)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 init_thread();
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
223 if (lock) {
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
224 lock->locked = 0;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
225
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
226 status = pthread_mutex_init(&lock->mut,
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
227 pthread_mutexattr_default);
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
228 CHECK_STATUS("pthread_mutex_init");
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
229
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
230 status = pthread_cond_init(&lock->lock_released,
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
231 pthread_condattr_default);
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
232 CHECK_STATUS("pthread_cond_init");
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
233
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
234 if (error) {
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 free((void *)lock);
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 lock = 0;
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 dprintf(("allocate_lock() -> %lx\n", (long)lock));
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 return (type_lock) lock;
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 void free_lock _P1(lock, type_lock lock)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 {
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
246 pthread_lock *thelock = (pthread_lock *)lock;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
247 int status, error = 0;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
248
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 dprintf(("free_lock(%lx) called\n", (long)lock));
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
250
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
251 status = pthread_mutex_destroy( &thelock->mut );
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
252 CHECK_STATUS("pthread_mutex_destroy");
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
253
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
254 status = pthread_cond_destroy( &thelock->lock_released );
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
255 CHECK_STATUS("pthread_cond_destroy");
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
256
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
257 free((void *)thelock);
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
259
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 {
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 int success;
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
263 pthread_lock *thelock = (pthread_lock *)lock;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
264 int status, error = 0;
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
267
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
268 status = pthread_mutex_lock( &thelock->mut );
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
269 CHECK_STATUS("pthread_mutex_lock[1]");
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
270 success = thelock->locked == 0;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
271 if (success) thelock->locked = 1;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
272 status = pthread_mutex_unlock( &thelock->mut );
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
273 CHECK_STATUS("pthread_mutex_unlock[1]");
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
274
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
275 if ( !success && waitflag ) {
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
276 /* continue trying until we get the lock */
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
277
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
278 /* mut must be locked by me -- part of the condition
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
279 * protocol */
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
280 status = pthread_mutex_lock( &thelock->mut );
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
281 CHECK_STATUS("pthread_mutex_lock[2]");
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
282 while ( thelock->locked ) {
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
283 status = pthread_cond_wait(&thelock->lock_released,
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
284 &thelock->mut);
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
285 CHECK_STATUS("pthread_cond_wait");
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 }
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
287 thelock->locked = 1;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
288 status = pthread_mutex_unlock( &thelock->mut );
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
289 CHECK_STATUS("pthread_mutex_unlock[2]");
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
290 success = 1;
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 }
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
292 if (error) success = 0;
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 return success;
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 void release_lock _P1(lock, type_lock lock)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 {
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
299 pthread_lock *thelock = (pthread_lock *)lock;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
300 int status, error = 0;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
301
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 dprintf(("release_lock(%lx) called\n", (long)lock));
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
303
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
304 status = pthread_mutex_lock( &thelock->mut );
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
305 CHECK_STATUS("pthread_mutex_lock[3]");
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
307 thelock->locked = 0;
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
309 status = pthread_mutex_unlock( &thelock->mut );
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
310 CHECK_STATUS("pthread_mutex_unlock[3]");
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
311
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
312 /* wake up someone (anyone, if any) waiting on the lock */
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
313 status = pthread_cond_signal( &thelock->lock_released );
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
314 CHECK_STATUS("pthread_cond_signal");
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 /*
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 * Semaphore support.
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 */
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
320
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
321 struct semaphore {
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
322 pthread_mutex_t mutex;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
323 pthread_cond_t cond;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
324 int value;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
325 };
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
326
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 type_sema allocate_sema _P1(value, int value)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 {
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
329 struct semaphore *sema;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
330 int status, error = 0;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
331
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 dprintf(("allocate_sema called\n"));
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 if (!initialized)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 init_thread();
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
335
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
336 sema = (struct semaphore *) malloc(sizeof(struct semaphore));
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
337 if (sema != NULL) {
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
338 sema->value = value;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
339 status = pthread_mutex_init(&sema->mutex,
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
340 pthread_mutexattr_default);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
341 CHECK_STATUS("pthread_mutex_init");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
342 status = pthread_cond_init(&sema->cond,
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
343 pthread_condattr_default);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
344 CHECK_STATUS("pthread_cond_init");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
345 if (error) {
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
346 free((void *) sema);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
347 sema = NULL;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
348 }
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
349 }
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 dprintf(("allocate_sema() -> %lx\n", (long) sema));
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 return (type_sema) sema;
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
353
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 void free_sema _P1(sema, type_sema sema)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 {
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
356 int status, error = 0;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
357 struct semaphore *thesema = (struct semaphore *) sema;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
358
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 dprintf(("free_sema(%lx) called\n", (long) sema));
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
360 status = pthread_cond_destroy(&thesema->cond);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
361 CHECK_STATUS("pthread_cond_destroy");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
362 status = pthread_mutex_destroy(&thesema->mutex);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
363 CHECK_STATUS("pthread_mutex_destroy");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
364 free((void *) thesema);
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
366
4371
c277ee8c669c Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents: 3965
diff changeset
367 int down_sema _P2(sema, type_sema sema, waitflag, int waitflag)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 {
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
369 int status, error = 0, success;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
370 struct semaphore *thesema = (struct semaphore *) sema;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
371
4371
c277ee8c669c Sjoerd's thread changes (including down_sema typo fix).
Guido van Rossum <guido@python.org>
parents: 3965
diff changeset
372 dprintf(("down_sema(%lx, %d) called\n", (long) sema, waitflag));
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
373 status = pthread_mutex_lock(&thesema->mutex);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
374 CHECK_STATUS("pthread_mutex_lock");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
375 if (waitflag) {
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
376 while (!error && thesema->value <= 0) {
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
377 status = pthread_cond_wait(&thesema->cond,
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
378 &thesema->mutex);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
379 CHECK_STATUS("pthread_cond_wait");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
380 }
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
381 }
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
382 if (error)
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
383 success = 0;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
384 else if (thesema->value > 0) {
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
385 thesema->value--;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
386 success = 1;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
387 }
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
388 else
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
389 success = 0;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
390 status = pthread_mutex_unlock(&thesema->mutex);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
391 CHECK_STATUS("pthread_mutex_unlock");
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 dprintf(("down_sema(%lx) return\n", (long) sema));
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
393 return success;
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
395
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 void up_sema _P1(sema, type_sema sema)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 {
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
398 int status, error = 0;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
399 struct semaphore *thesema = (struct semaphore *) sema;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
400
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 dprintf(("up_sema(%lx)\n", (long) sema));
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
402 status = pthread_mutex_lock(&thesema->mutex);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
403 CHECK_STATUS("pthread_mutex_lock");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
404 thesema->value++;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
405 status = pthread_cond_signal(&thesema->cond);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
406 CHECK_STATUS("pthread_cond_signal");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
407 status = pthread_mutex_unlock(&thesema->mutex);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
408 CHECK_STATUS("pthread_mutex_unlock");
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 }