annotate Python/thread_pthread.h @ 20307:aa681345bfab legacy-trunk

Implement isinstance(x, (A, B, ...)). Note that we only allow tuples, not other sequences (then we'd have to except strings, and we'd still be susceptible to recursive attacks).
author Guido van Rossum <guido@python.org>
date Sun, 07 Oct 2001 20:54:12 +0000
parents 8ef456949b76
children c9a212329da6
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
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
2 /* Posix threads interface */
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
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
4 #include <stdlib.h>
9290
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
5 #include <string.h>
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
6 #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
7
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
8
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
9 /* 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
10 * 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
11 * 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
12 * 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
13 * 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
14 */
3965
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
15
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
16 #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
17 /* _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
18 # define PY_PTHREAD_D4
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
19
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
20 #elif defined(__osf__) && defined (__alpha)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
21 /* _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
22 # 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
23 # define PY_PTHREAD_D4
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
24 # else
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
25 # define PY_PTHREAD_STD
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
26 # endif
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
27
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
28 #elif defined(_AIX)
3965
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
29 /* 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
30 * but it is unsupported by AIX 4 pthreads. Default
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
31 * attributes for AIX 4 pthreads equal to NULL. For
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
32 * AIX DCE pthreads they should be left unchanged.
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
33 */
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
34 # if !defined(SCHED_BG_NP)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
35 # define PY_PTHREAD_STD
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
36 # else
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
37 # define PY_PTHREAD_D7
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
38 # endif
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
39
5599
b5e51ae9f832 DG/UX thread patches (Ross Andrus)
Guido van Rossum <guido@python.org>
parents: 5529
diff changeset
40 #elif defined(__DGUX)
b5e51ae9f832 DG/UX thread patches (Ross Andrus)
Guido van Rossum <guido@python.org>
parents: 5529
diff changeset
41 # define PY_PTHREAD_D6
5700
60fca4783793 Add default case (standard conformance) to avoid piling up
Guido van Rossum <guido@python.org>
parents: 5599
diff changeset
42
8343
1b4a37f37345 Improved version of patch for HPUX from David Arnold.
Guido van Rossum <guido@python.org>
parents: 8250
diff changeset
43 #elif defined(__hpux) && defined(_DECTHREADS_)
8250
50d35bdf2042 Support HPUX 10.20 DCE threads.
Guido van Rossum <guido@python.org>
parents: 7982
diff changeset
44 # define PY_PTHREAD_D4
50d35bdf2042 Support HPUX 10.20 DCE threads.
Guido van Rossum <guido@python.org>
parents: 7982
diff changeset
45
5700
60fca4783793 Add default case (standard conformance) to avoid piling up
Guido van Rossum <guido@python.org>
parents: 5599
diff changeset
46 #else /* Default case */
60fca4783793 Add default case (standard conformance) to avoid piling up
Guido van Rossum <guido@python.org>
parents: 5599
diff changeset
47 # define PY_PTHREAD_STD
60fca4783793 Add default case (standard conformance) to avoid piling up
Guido van Rossum <guido@python.org>
parents: 5599
diff changeset
48
3965
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
49 #endif
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
50
19549
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
51 #ifdef USE_GUSI
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
52 /* The Macintosh GUSI I/O library sets the stackspace to
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
53 ** 20KB, much too low. We up it to 64K.
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
54 */
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
55 #define THREAD_STACK_SIZE 0x10000
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
56 #endif
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
57
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
58
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
59 /* 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
60
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
61 #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
62 # 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
63 # 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
64 # define pthread_condattr_default pthread_condattr_default
5599
b5e51ae9f832 DG/UX thread patches (Ross Andrus)
Guido van Rossum <guido@python.org>
parents: 5529
diff changeset
65 #elif defined(PY_PTHREAD_STD) || defined(PY_PTHREAD_D6)
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
66 # 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
67 # 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
68 # define pthread_condattr_default ((pthread_condattr_t *)NULL)
3965
9c05ff6fb0a4 Changes necessary for AIX.
Guido van Rossum <guido@python.org>
parents: 3663
diff changeset
69 #endif
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
71
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
72 /* 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
73 * 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
74 * following are undefined:
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
75 * -> 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
76 * -> 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
77 * 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
78 * 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
79 * 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
80 *
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
81 * 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
82 * 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
83 * 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
84 * 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
85 */
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
86
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 typedef struct {
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
88 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
89 /* 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
90 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
91 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
92 } pthread_lock;
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
9290
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
94 #define CHECK_STATUS(name) if (status != 0) { perror(name); error = 1; }
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
95
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 /*
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 * Initialization.
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 */
9290
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
99
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
100 #ifdef _HAVE_BSDI
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
101 static
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
102 void _noop(void)
9290
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
103 {
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
104 }
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
105
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
106 static void
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
107 PyThread__init_thread(void)
9290
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
108 {
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
109 /* DO AN INIT BY STARTING THE THREAD */
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
110 static int dummy = 0;
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
111 pthread_t thread1;
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
112 pthread_create(&thread1, NULL, (void *) _noop, &dummy);
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
113 pthread_join(thread1, NULL);
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
114 }
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
115
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
116 #else /* !_HAVE_BSDI */
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
117
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
118 static void
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
119 PyThread__init_thread(void)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 {
9079
4d2cb212c8d1 Apparently on AIX when using gcc you need to call pthread_init()
Guido van Rossum <guido@python.org>
parents: 9070
diff changeset
121 #if defined(_AIX) && defined(__GNUC__)
4d2cb212c8d1 Apparently on AIX when using gcc you need to call pthread_init()
Guido van Rossum <guido@python.org>
parents: 9070
diff changeset
122 pthread_init();
4d2cb212c8d1 Apparently on AIX when using gcc you need to call pthread_init()
Guido van Rossum <guido@python.org>
parents: 9070
diff changeset
123 #endif
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
9290
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
126 #endif /* !_HAVE_BSDI */
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
127
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 /*
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 * Thread support.
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 */
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
131
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
132
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
133 int
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
134 PyThread_start_new_thread(void (*func)(void *), void *arg)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 {
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 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
137 int success;
19826
8ef456949b76 Improve threading on Solaris, according to SF patch #460269, submitted
Guido van Rossum <guido@python.org>
parents: 19549
diff changeset
138 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
19549
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
139 pthread_attr_t attrs;
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
140 #endif
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
141 dprintf(("PyThread_start_new_thread called\n"));
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 if (!initialized)
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
143 PyThread_init_thread();
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
144
19826
8ef456949b76 Improve threading on Solaris, according to SF patch #460269, submitted
Guido van Rossum <guido@python.org>
parents: 19549
diff changeset
145 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
19549
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
146 pthread_attr_init(&attrs);
19826
8ef456949b76 Improve threading on Solaris, according to SF patch #460269, submitted
Guido van Rossum <guido@python.org>
parents: 19549
diff changeset
147 #endif
8ef456949b76 Improve threading on Solaris, according to SF patch #460269, submitted
Guido van Rossum <guido@python.org>
parents: 19549
diff changeset
148 #ifdef THREAD_STACK_SIZE
19549
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
149 pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
150 #endif
19826
8ef456949b76 Improve threading on Solaris, according to SF patch #460269, submitted
Guido van Rossum <guido@python.org>
parents: 19549
diff changeset
151 #ifdef PTHREAD_SYSTEM_SCHED_SUPPORTED
8ef456949b76 Improve threading on Solaris, according to SF patch #460269, submitted
Guido van Rossum <guido@python.org>
parents: 19549
diff changeset
152 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
8ef456949b76 Improve threading on Solaris, according to SF patch #460269, submitted
Guido van Rossum <guido@python.org>
parents: 19549
diff changeset
153 #endif
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
154 success = pthread_create(&th,
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
155 #if defined(PY_PTHREAD_D4)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
156 pthread_attr_default,
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
157 (pthread_startroutine_t)func,
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
158 (pthread_addr_t)arg
5599
b5e51ae9f832 DG/UX thread patches (Ross Andrus)
Guido van Rossum <guido@python.org>
parents: 5529
diff changeset
159 #elif defined(PY_PTHREAD_D6)
b5e51ae9f832 DG/UX thread patches (Ross Andrus)
Guido van Rossum <guido@python.org>
parents: 5529
diff changeset
160 pthread_attr_default,
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
161 (void* (*)(void *))func,
5599
b5e51ae9f832 DG/UX thread patches (Ross Andrus)
Guido van Rossum <guido@python.org>
parents: 5529
diff changeset
162 arg
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
163 #elif defined(PY_PTHREAD_D7)
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
164 pthread_attr_default,
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
165 func,
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
166 arg
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
167 #elif defined(PY_PTHREAD_STD)
19826
8ef456949b76 Improve threading on Solaris, according to SF patch #460269, submitted
Guido van Rossum <guido@python.org>
parents: 19549
diff changeset
168 #if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
19549
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
169 &attrs,
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
170 #else
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
171 (pthread_attr_t*)NULL,
19549
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
172 #endif
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
173 (void* (*)(void *))func,
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
174 (void *)arg
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
175 #endif
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
176 );
19549
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
177 #ifdef THREAD_STACK_SIZE
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
178 pthread_attr_destroy(&attrs);
11a758c56027 GUSI on the Mac creates threads with a default stack size of 20KB, which is
Jack Jansen <jack.jansen@cwi.nl>
parents: 14375
diff changeset
179 #endif
10400
94ad90fd2712 Rob Riggs wrote:
Guido van Rossum <guido@python.org>
parents: 9679
diff changeset
180 if (success == 0) {
9070
c2cbe9acfc8e Correct typo in #ifdef: PY_THREAD_D4, should be PY_PTHREAD_D4.
Guido van Rossum <guido@python.org>
parents: 9055
diff changeset
181 #if defined(PY_PTHREAD_D4) || defined(PY_PTHREAD_D6) || defined(PY_PTHREAD_D7)
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
182 pthread_detach(&th);
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
183 #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
184 pthread_detach(th);
5493
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
185 #endif
ef93d51dab36 Support for various versions of the pthread draft.
Guido van Rossum <guido@python.org>
parents: 5395
diff changeset
186 }
10400
94ad90fd2712 Rob Riggs wrote:
Guido van Rossum <guido@python.org>
parents: 9679
diff changeset
187 return success != 0 ? 0 : 1;
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
14144
31cda87e9ff4 This patch partly (some stuff went in already) ports Python to Monterey.
Trent Mick <trentm@activestate.com>
parents: 13658
diff changeset
190 /* XXX This implementation is considered (to quote Tim Peters) "inherently
31cda87e9ff4 This patch partly (some stuff went in already) ports Python to Monterey.
Trent Mick <trentm@activestate.com>
parents: 13658
diff changeset
191 hosed" because:
31cda87e9ff4 This patch partly (some stuff went in already) ports Python to Monterey.
Trent Mick <trentm@activestate.com>
parents: 13658
diff changeset
192 - It does not guanrantee the promise that a non-zero integer is returned.
31cda87e9ff4 This patch partly (some stuff went in already) ports Python to Monterey.
Trent Mick <trentm@activestate.com>
parents: 13658
diff changeset
193 - The cast to long is inherently unsafe.
31cda87e9ff4 This patch partly (some stuff went in already) ports Python to Monterey.
Trent Mick <trentm@activestate.com>
parents: 13658
diff changeset
194 - It is not clear that the 'volatile' (for AIX?) and ugly casting in the
31cda87e9ff4 This patch partly (some stuff went in already) ports Python to Monterey.
Trent Mick <trentm@activestate.com>
parents: 13658
diff changeset
195 latter return statement (for Alpha OSF/1) are any longer necessary.
31cda87e9ff4 This patch partly (some stuff went in already) ports Python to Monterey.
Trent Mick <trentm@activestate.com>
parents: 13658
diff changeset
196 */
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
197 long
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
198 PyThread_get_thread_ident(void)
1651
7e5f97bab1e6 ceval.c: dict of local mapping is now a tuple
Guido van Rossum <guido@python.org>
parents: 1629
diff changeset
199 {
9055
5bc64efc65e0 Add a 'volatile' to the declaration of threadid in get_thread_ident().
Guido van Rossum <guido@python.org>
parents: 8343
diff changeset
200 volatile 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
201 if (!initialized)
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
202 PyThread_init_thread();
2097
e4588404fdff Alpha OSF/1 fix
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
203 /* Jump through some hoops for Alpha OSF/1 */
e4588404fdff Alpha OSF/1 fix
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
204 threadid = pthread_self();
14144
31cda87e9ff4 This patch partly (some stuff went in already) ports Python to Monterey.
Trent Mick <trentm@activestate.com>
parents: 13658
diff changeset
205 #if SIZEOF_PTHREAD_T <= SIZEOF_LONG
31cda87e9ff4 This patch partly (some stuff went in already) ports Python to Monterey.
Trent Mick <trentm@activestate.com>
parents: 13658
diff changeset
206 return (long) threadid;
31cda87e9ff4 This patch partly (some stuff went in already) ports Python to Monterey.
Trent Mick <trentm@activestate.com>
parents: 13658
diff changeset
207 #else
2097
e4588404fdff Alpha OSF/1 fix
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
208 return (long) *(long *) &threadid;
14144
31cda87e9ff4 This patch partly (some stuff went in already) ports Python to Monterey.
Trent Mick <trentm@activestate.com>
parents: 13658
diff changeset
209 #endif
1651
7e5f97bab1e6 ceval.c: dict of local mapping is now a tuple
Guido van Rossum <guido@python.org>
parents: 1629
diff changeset
210 }
7e5f97bab1e6 ceval.c: dict of local mapping is now a tuple
Guido van Rossum <guido@python.org>
parents: 1629
diff changeset
211
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
212 static void
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
213 do_PyThread_exit_thread(int no_cleanup)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
215 dprintf(("PyThread_exit_thread called\n"));
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 5700
diff changeset
216 if (!initialized) {
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 if (no_cleanup)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 _exit(0);
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 else
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 exit(0);
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 5700
diff changeset
221 }
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
224 void
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
225 PyThread_exit_thread(void)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
227 do_PyThread_exit_thread(0);
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
230 void
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
231 PyThread__exit_thread(void)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
233 do_PyThread_exit_thread(1);
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 #ifndef NO_EXIT_PROG
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
237 static void
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
238 do_PyThread_exit_prog(int status, int no_cleanup)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
240 dprintf(("PyThread_exit_prog(%d) called\n", status));
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 if (!initialized)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 if (no_cleanup)
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 _exit(status);
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 else
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 exit(status);
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
248 void
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
249 PyThread_exit_prog(int status)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
251 do_PyThread_exit_prog(status, 0);
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
254 void
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
255 PyThread__exit_prog(int status)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
257 do_PyThread_exit_prog(status, 1);
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 #endif /* NO_EXIT_PROG */
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
260
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 * Lock support.
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 */
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
264 PyThread_type_lock
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
265 PyThread_allocate_lock(void)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 {
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 pthread_lock *lock;
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
268 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
269
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
270 dprintf(("PyThread_allocate_lock called\n"));
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 if (!initialized)
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
272 PyThread_init_thread();
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 lock = (pthread_lock *) malloc(sizeof(pthread_lock));
9290
2340f615da8f BSDI specific patches, inspired by Nigel Head and otto@mail.olympus.net.
Guido van Rossum <guido@python.org>
parents: 9079
diff changeset
275 memset((void *)lock, '\0', sizeof(pthread_lock));
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
276 if (lock) {
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
277 lock->locked = 0;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
278
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
279 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
280 pthread_mutexattr_default);
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_init");
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
282
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
283 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
284 pthread_condattr_default);
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_init");
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
286
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
287 if (error) {
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 free((void *)lock);
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 lock = 0;
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 10400
diff changeset
293 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
294 return (PyThread_type_lock) lock;
1628
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
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
297 void
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
298 PyThread_free_lock(PyThread_type_lock lock)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 {
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
300 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
301 int status, error = 0;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
302
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 10400
diff changeset
303 dprintf(("PyThread_free_lock(%p) called\n", lock));
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
304
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
305 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
306 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
307
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
308 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
309 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
310
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
311 free((void *)thelock);
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
314 int
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
315 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
1628
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 int success;
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
318 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
319 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
320
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 10400
diff changeset
321 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
322
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
323 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
324 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
325 success = thelock->locked == 0;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
326 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
327 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
328 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
329
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
330 if ( !success && waitflag ) {
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
331 /* 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
332
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
333 /* 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
334 * protocol */
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
335 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
336 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
337 while ( thelock->locked ) {
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
338 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
339 &thelock->mut);
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
340 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
341 }
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
342 thelock->locked = 1;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
343 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
344 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
345 success = 1;
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 }
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
347 if (error) success = 0;
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 10400
diff changeset
348 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 return success;
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
352 void
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
353 PyThread_release_lock(PyThread_type_lock lock)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 {
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
355 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
356 int status, error = 0;
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
357
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 10400
diff changeset
358 dprintf(("PyThread_release_lock(%p) called\n", lock));
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
359
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
360 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
361 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
362
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
363 thelock->locked = 0;
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
364
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
365 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
366 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
367
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
368 /* 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
369 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
370 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
371 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 /*
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 * Semaphore support.
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 */
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
376
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
377 struct semaphore {
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
378 pthread_mutex_t mutex;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
379 pthread_cond_t cond;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
380 int value;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
381 };
1629
4121afd76d73 Tim's changes; removed some remaining non-functional ifdefs
Guido van Rossum <guido@python.org>
parents: 1628
diff changeset
382
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
383 PyThread_type_sema
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
384 PyThread_allocate_sema(int value)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 {
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
386 struct semaphore *sema;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
387 int status, error = 0;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
388
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
389 dprintf(("PyThread_allocate_sema called\n"));
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 if (!initialized)
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
391 PyThread_init_thread();
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
393 sema = (struct semaphore *) malloc(sizeof(struct semaphore));
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
394 if (sema != NULL) {
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
395 sema->value = value;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
396 status = pthread_mutex_init(&sema->mutex,
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
397 pthread_mutexattr_default);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
398 CHECK_STATUS("pthread_mutex_init");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
399 status = pthread_cond_init(&sema->cond,
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
400 pthread_condattr_default);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
401 CHECK_STATUS("pthread_cond_init");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
402 if (error) {
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
403 free((void *) sema);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
404 sema = NULL;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
405 }
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
406 }
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 10400
diff changeset
407 dprintf(("PyThread_allocate_sema() -> %p\n", sema));
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9290
diff changeset
408 return (PyThread_type_sema) sema;
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
410
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
411 void
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
412 PyThread_free_sema(PyThread_type_sema sema)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 {
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
414 int status, error = 0;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
415 struct semaphore *thesema = (struct semaphore *) sema;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
416
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 10400
diff changeset
417 dprintf(("PyThread_free_sema(%p) called\n", sema));
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
418 status = pthread_cond_destroy(&thesema->cond);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
419 CHECK_STATUS("pthread_cond_destroy");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
420 status = pthread_mutex_destroy(&thesema->mutex);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
421 CHECK_STATUS("pthread_mutex_destroy");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
422 free((void *) thesema);
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
425 int
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
426 PyThread_down_sema(PyThread_type_sema sema, int waitflag)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 {
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
428 int status, error = 0, success;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
429 struct semaphore *thesema = (struct semaphore *) sema;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
430
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 10400
diff changeset
431 dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag));
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
432 status = pthread_mutex_lock(&thesema->mutex);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
433 CHECK_STATUS("pthread_mutex_lock");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
434 if (waitflag) {
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
435 while (!error && thesema->value <= 0) {
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
436 status = pthread_cond_wait(&thesema->cond,
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
437 &thesema->mutex);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
438 CHECK_STATUS("pthread_cond_wait");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
439 }
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
440 }
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
441 if (error)
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
442 success = 0;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
443 else if (thesema->value > 0) {
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
444 thesema->value--;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
445 success = 1;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
446 }
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
447 else
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
448 success = 0;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
449 status = pthread_mutex_unlock(&thesema->mutex);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
450 CHECK_STATUS("pthread_mutex_unlock");
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 10400
diff changeset
451 dprintf(("PyThread_down_sema(%p) return\n", sema));
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
452 return success;
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 }
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
454
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
455 void
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
456 PyThread_up_sema(PyThread_type_sema sema)
1628
a9dd2e1ea5c9 Split thread.c into a number of system-specific files.
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 {
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
458 int status, error = 0;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
459 struct semaphore *thesema = (struct semaphore *) sema;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
460
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 10400
diff changeset
461 dprintf(("PyThread_up_sema(%p)\n", sema));
4994
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
462 status = pthread_mutex_lock(&thesema->mutex);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
463 CHECK_STATUS("pthread_mutex_lock");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
464 thesema->value++;
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
465 status = pthread_cond_signal(&thesema->cond);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
466 CHECK_STATUS("pthread_cond_signal");
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
467 status = pthread_mutex_unlock(&thesema->mutex);
9bc652e9f2b4 Working semaphore implementation by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
468 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
469 }