annotate Python/thread_beos.h @ 9603:c4e2da477949 legacy-trunk

Adding thread support for BeOS by Chris Herborth.
author Guido van Rossum <guido@python.org>
date Mon, 07 Dec 1998 21:56:59 +0000
parents
children 877d23bd5bdc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /***********************************************************
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 The Netherlands.
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 documentation for any purpose and without fee is hereby granted,
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 provided that the above copyright notice appear in all copies and that
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 both that copyright notice and this permission notice appear in
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 distribution of the software without specific, written prior
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 permission.
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 While CWI is the initial source for this software, a modified version
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 is made available by the Corporation for National Research Initiatives
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 BeOS thread support by Chris Herborth (chrish@qnx.com)
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 ******************************************************************/
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 #include <kernel/OS.h>
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 #include <support/SupportDefs.h>
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 #include <errno.h>
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 /* ----------------------------------------------------------------------
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 * Fast locking mechanism described by Benoit Schillings (benoit@be.com)
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 * in the Be Developer's Newsletter, Issue #26 (http://www.be.com/).
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 typedef struct benaphore {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 sem_id _sem;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 int32 _atom;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 } benaphore_t;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 static status_t benaphore_create( const char *name, benaphore_t *ben );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 static status_t benaphore_destroy( benaphore_t *ben );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 static status_t benaphore_lock( benaphore_t *ben );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 static status_t benaphore_timedlock( benaphore_t *ben, bigtime_t micros );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 static status_t benaphore_unlock( benaphore_t *ben );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 static status_t benaphore_create( const char *name, benaphore_t *ben )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 if( ben != NULL ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 ben->_atom = 0;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 ben->_sem = create_sem( 0, name );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 if( ben->_sem < B_NO_ERROR ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 return B_BAD_SEM_ID;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 } else {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 return EFAULT;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 return EOK;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 static status_t benaphore_destroy( benaphore_t *ben )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 if( ben->_sem >= B_NO_ERROR ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 status_t retval = benaphore_timedlock( ben, 0 );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 if( retval == EOK || retval == EWOULDBLOCK ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 status_t del_retval = delete_sem( ben->_sem );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 return del_retval;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 return B_BAD_SEM_ID;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 static status_t benaphore_lock( benaphore_t *ben )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 int32 prev = atomic_add( &(ben->_atom), 1 );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 if( prev > 0 ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 return acquire_sem( ben->_sem );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 return EOK;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 static status_t benaphore_timedlock( benaphore_t *ben, bigtime_t micros )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 int32 prev = atomic_add( &(ben->_atom), 1 );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 if( prev > 0 ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 status_t retval = acquire_sem_etc( ben->_sem, 1, B_TIMEOUT, micros );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 switch( retval ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 case B_WOULD_BLOCK: /* Fall through... */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 case B_TIMED_OUT:
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 return EWOULDBLOCK;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 break;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 case B_OK:
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 return EOK;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 break;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 default:
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 return retval;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 break;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 return EOK;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 static status_t benaphore_unlock( benaphore_t *ben )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 int32 prev = atomic_add( &(ben->_atom), -1 );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 if( prev > 1 ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 return release_sem( ben->_sem );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 return EOK;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 /* ----------------------------------------------------------------------
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 * Initialization.
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 static void _init_thread( void )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 /* Do nothing. */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 return;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 /* ----------------------------------------------------------------------
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 * Thread support.
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 *
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 * Only ANSI C, renamed functions here; you can't use K&R on BeOS,
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 * and there's no legacy thread module to support.
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 static int32 thread_count = 0;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 int PyThread_start_new_thread( void (*func)(void *), void *arg )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 status_t success = 0;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 thread_id tid;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 char name[B_OS_NAME_LENGTH];
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 int32 this_thread;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 dprintf(("start_new_thread called\n"));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 /* We are so very thread-safe... */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 this_thread = atomic_add( &thread_count, 1 );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 sprintf( name, "python thread (%d)", this_thread );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 tid = spawn_thread( (thread_func)func, name,
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 B_NORMAL_PRIORITY, arg );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 if( tid > B_NO_ERROR ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 success = resume_thread( tid );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 return ( success == B_NO_ERROR ? 1 : 0 );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 long PyThread_get_thread_ident( void )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 /* Presumed to return the current thread's ID... */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 thread_id tid;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 tid = find_thread( NULL );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 return ( tid != B_NAME_NOT_FOUND ? tid : -1 );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 static void do_exit_thread( int no_cleanup )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 int32 threads;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 dprintf(("exit_thread called\n"));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 /* Thread-safe way to read a variable without a mutex: */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 threads = atomic_add( &thread_count, 0 );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 if( threads == 0 ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 /* No threads around, so exit main(). */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 if( no_cleanup ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 _exit(0);
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 } else {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 exit(0);
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 } else {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 /* Oh, we're a thread, let's try to exit gracefully... */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 exit_thread( B_NO_ERROR );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 void PyThread_exit_thread( void )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 do_exit_thread(0);
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 void PyThread__exit_thread( void )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 do_exit_thread(1);
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 #ifndef NO_EXIT_PROG
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 static void do_exit_prog( int status, int no_cleanup )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 dprintf(("exit_prog(%d) called\n", status));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 /* No need to do anything, the threads get torn down if main() exits. */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 if (no_cleanup) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 _exit(status);
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 } else {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 exit(status);
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 void PyThread_exit_prog( int status )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 do_exit_prog(status, 0);
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 void PyThread__exit_prog( int status )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 do_exit_prog(status, 1);
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 #endif /* NO_EXIT_PROG */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 /* ----------------------------------------------------------------------
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 * Lock support.
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 static int32 lock_count = 0;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
240
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 type_lock PyThread_allocate_lock( void )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 benaphore_t *lock;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 status_t retval;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 char name[B_OS_NAME_LENGTH];
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 int32 this_lock;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 dprintf(("allocate_lock called\n"));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 lock = (benaphore_t *)malloc( sizeof( benaphore_t ) );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 if( lock == NULL ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 /* TODO: that's bad, raise MemoryError */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 return (type_lock)NULL;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 this_lock = atomic_add( &lock_count, 1 );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 sprintf( name, "python lock (%d)", this_lock );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 retval = benaphore_create( name, lock );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 if( retval != EOK ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 /* TODO: that's bad, raise an exception */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 return (type_lock)NULL;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 dprintf(("allocate_lock() -> %lx\n", (long)lock));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 return (type_lock) lock;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
268
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 void PyThread_free_lock( type_lock lock )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 status_t retval;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 dprintf(("free_lock(%lx) called\n", (long)lock));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 retval = benaphore_destroy( (benaphore_t *)lock );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 if( retval != EOK ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 /* TODO: that's bad, raise an exception */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 return;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 int PyThread_acquire_lock( type_lock lock, int waitflag )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 int success;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 status_t retval;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 if( waitflag ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 retval = benaphore_lock( (benaphore_t *)lock );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 } else {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 retval = benaphore_timedlock( (benaphore_t *)lock, 0 );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
294
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 if( retval == EOK ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 success = 1;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 } else {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 success = 0;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
299
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 /* TODO: that's bad, raise an exception */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
302
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 return success;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 void PyThread_release_lock( type_lock lock )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 status_t retval;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
310
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 dprintf(("release_lock(%lx) called\n", (long)lock));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 retval = benaphore_unlock( (benaphore_t *)lock );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 if( retval != EOK ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 /* TODO: that's bad, raise an exception */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 return;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
319
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 /* ----------------------------------------------------------------------
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 * Semaphore support.
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 *
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 * Guido says not to implement this because it's not used anywhere;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 * I'll do it anyway, you never know when it might be handy, and it's
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 * easy...
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 type_sema PyThread_allocate_sema( int value )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 sem_id sema;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
330
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 dprintf(("allocate_sema called\n"));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 sema = create_sem( value, "python semaphore" );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 if( sema < B_NO_ERROR ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 /* TODO: that's bad, raise an exception */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 return 0;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
338
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 dprintf(("allocate_sema() -> %lx\n", (long) sema));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 return (type_sema) sema;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
342
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 void PyThread_free_sema( type_sema sema )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 status_t retval;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 dprintf(("free_sema(%lx) called\n", (long) sema));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
348
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 retval = delete_sem( (sem_id)sema );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 if( retval != B_NO_ERROR ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 /* TODO: that's bad, raise an exception */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 return;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
355
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 int PyThread_down_sema( type_sema sema, int waitflag )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 status_t retval;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
359
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 dprintf(("down_sema(%lx, %d) called\n", (long) sema, waitflag));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 if( waitflag ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 retval = acquire_sem( (sem_id)sema );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 } else {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 retval = acquire_sem_etc( (sem_id)sema, 1, B_TIMEOUT, 0 );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 if( retval != B_NO_ERROR ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 /* TODO: that's bad, raise an exception */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 return 0;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 dprintf(("down_sema(%lx) return\n", (long) sema));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 return -1;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
376
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 void PyThread_up_sema( type_sema sema )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 status_t retval;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 dprintf(("up_sema(%lx)\n", (long) sema));
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
382
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 retval = release_sem( (sem_id)sema );
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 if( retval != B_NO_ERROR ) {
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 /* TODO: that's bad, raise an exception */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 return;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 }