annotate Python/thread_beos.h @ 62743:2145593d108d legacy-trunk v2.7

move sections
author Benjamin Peterson <benjamin@python.org>
date Sat, 03 Jul 2010 13:57:30 +0000
parents fd25030223b0
children b36cb4602e21
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 #include <kernel/OS.h>
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 #include <support/SupportDefs.h>
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 #include <errno.h>
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 /* ----------------------------------------------------------------------
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 * 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
7 * 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
8 */
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 typedef struct benaphore {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
10 sem_id _sem;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
11 int32 _atom;
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 } benaphore_t;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 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
15 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
16 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
17 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
18 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
19
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 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
21 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
22 if( ben != NULL ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
23 ben->_atom = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
24 ben->_sem = create_sem( 0, name );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
25
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
26 if( ben->_sem < B_NO_ERROR ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
27 return B_BAD_SEM_ID;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
28 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
29 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
30 return EFAULT;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
31 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
32
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
33 return EOK;
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 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
37 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
38 if( ben->_sem >= B_NO_ERROR ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
39 status_t retval = benaphore_timedlock( ben, 0 );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
40
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
41 if( retval == EOK || retval == EWOULDBLOCK ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
42 status_t del_retval = delete_sem( ben->_sem );
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
44 return del_retval;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
45 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
46 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
47
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
48 return B_BAD_SEM_ID;
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 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
52 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
53 int32 prev = atomic_add( &(ben->_atom), 1 );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
54
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
55 if( prev > 0 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
56 return acquire_sem( ben->_sem );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
57 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
58
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
59 return EOK;
9603
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
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 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
63 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
64 int32 prev = atomic_add( &(ben->_atom), 1 );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
65
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
66 if( prev > 0 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
67 status_t retval = acquire_sem_etc( ben->_sem, 1, B_TIMEOUT, micros );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
68
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
69 switch( retval ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
70 case B_WOULD_BLOCK: /* Fall through... */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
71 case B_TIMED_OUT:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
72 return EWOULDBLOCK;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
73 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
74 case B_OK:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
75 return EOK;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
76 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
77 default:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
78 return retval;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
79 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
80 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
81 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
82
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
83 return EOK;
9603
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
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 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
87 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
88 int32 prev = atomic_add( &(ben->_atom), -1 );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
89
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
90 if( prev > 1 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
91 return release_sem( ben->_sem );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
92 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
93
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
94 return EOK;
9603
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
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 * Initialization.
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 */
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9603
diff changeset
100 static void PyThread__init_thread( void )
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
102 /* Do nothing. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
103 return;
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 /* ----------------------------------------------------------------------
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 * Thread support.
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 *
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 * 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
110 * 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
111 */
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 static int32 thread_count = 0;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
20468
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
115 long PyThread_start_new_thread( void (*func)(void *), void *arg )
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
117 status_t success = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
118 thread_id tid;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
119 char name[B_OS_NAME_LENGTH];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
120 int32 this_thread;
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
122 dprintf(("PyThread_start_new_thread called\n"));
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
124 /* We are so very thread-safe... */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
125 this_thread = atomic_add( &thread_count, 1 );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
126 PyOS_snprintf(name, sizeof(name),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
127 "python thread (%d)", this_thread );
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
129 tid = spawn_thread( (thread_func)func, name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
130 B_NORMAL_PRIORITY, arg );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
131 if( tid > B_NO_ERROR ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
132 success = resume_thread( tid );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
133 }
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
135 return ( success == B_NO_ERROR ? tid : -1 );
9603
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 long PyThread_get_thread_ident( void )
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
140 /* Presumed to return the current thread's ID... */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
141 thread_id tid;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
142 tid = find_thread( NULL );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
143
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
144 return ( tid != B_NAME_NOT_FOUND ? tid : -1 );
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
59086
df5719c298db #4852: Remove dead code in every thread implementation, unused for many years.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 21766
diff changeset
147 void PyThread_exit_thread( void )
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
149 int32 threads;
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
151 dprintf(("PyThread_exit_thread called\n"));
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
153 /* Thread-safe way to read a variable without a mutex: */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
154 threads = atomic_add( &thread_count, 0 );
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
156 if( threads == 0 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
157 /* No threads around, so exit main(). */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
158 exit(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
159 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
160 /* Oh, we're a thread, let's try to exit gracefully... */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
161 exit_thread( B_NO_ERROR );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
162 }
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 }
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 * Lock support.
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 static int32 lock_count = 0;
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9603
diff changeset
171 PyThread_type_lock PyThread_allocate_lock( void )
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
173 benaphore_t *lock;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
174 status_t retval;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
175 char name[B_OS_NAME_LENGTH];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
176 int32 this_lock;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
177
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
178 dprintf(("PyThread_allocate_lock called\n"));
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
180 lock = (benaphore_t *)malloc( sizeof( benaphore_t ) );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
181 if( lock == NULL ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
182 /* TODO: that's bad, raise MemoryError */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
183 return (PyThread_type_lock)NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
184 }
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
186 this_lock = atomic_add( &lock_count, 1 );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
187 PyOS_snprintf(name, sizeof(name), "python lock (%d)", this_lock);
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
189 retval = benaphore_create( name, lock );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
190 if( retval != EOK ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
191 /* TODO: that's bad, raise an exception */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
192 return (PyThread_type_lock)NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
193 }
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
195 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
196 return (PyThread_type_lock) lock;
9603
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
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9603
diff changeset
199 void PyThread_free_lock( PyThread_type_lock lock )
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
201 status_t retval;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
202
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
203 dprintf(("PyThread_free_lock(%p) called\n", lock));
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
205 retval = benaphore_destroy( (benaphore_t *)lock );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
206 if( retval != EOK ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
207 /* TODO: that's bad, raise an exception */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
208 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
209 }
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 }
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9603
diff changeset
212 int PyThread_acquire_lock( PyThread_type_lock lock, int waitflag )
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
214 int success;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
215 status_t retval;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
216
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
217 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
219 if( waitflag ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
220 retval = benaphore_lock( (benaphore_t *)lock );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
221 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
222 retval = benaphore_timedlock( (benaphore_t *)lock, 0 );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
223 }
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
225 if( retval == EOK ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
226 success = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
227 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
228 success = 0;
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
230 /* TODO: that's bad, raise an exception */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
231 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
232
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
233 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
234 return success;
9603
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
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9603
diff changeset
237 void PyThread_release_lock( PyThread_type_lock lock )
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
239 status_t retval;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
240
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
241 dprintf(("PyThread_release_lock(%p) called\n", lock));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
242
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
243 retval = benaphore_unlock( (benaphore_t *)lock );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
244 if( retval != EOK ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
245 /* TODO: that's bad, raise an exception */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
246 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59086
diff changeset
247 }
9603
c4e2da477949 Adding thread support for BeOS by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 }