@@ -10,31 +10,55 @@ import unsafe::{SharedMutableState,
10
10
shared_mutable_state, clone_shared_mutable_state,
11
11
get_shared_mutable_state, get_shared_immutable_state} ;
12
12
import sync;
13
- import sync:: { mutex, rwlock} ;
13
+ import sync:: { mutex, mutex_with_condvars , rwlock, rwlock_with_condvars } ;
14
14
15
15
export arc, clone, get;
16
- export condvar, mutex_arc, rw_arc, rw_write_mode, rw_read_mode;
16
+ export condvar, mutex_arc, mutex_arc_with_condvars;
17
+ export rw_arc, rw_arc_with_condvars, rw_write_mode, rw_read_mode;
17
18
18
19
/// As sync::condvar, a mechanism for unlock-and-descheduling and signalling.
19
20
struct condvar { is_mutex : bool ; failed: & mut bool; cond: & sync:: condvar; }
20
21
21
22
impl & condvar {
22
23
/// Atomically exit the associated ARC and block until a signal is sent.
23
- fn wait ( ) {
24
+ #[ inline( always) ]
25
+ fn wait ( ) { self . wait_on ( 0 ) }
26
+ /**
27
+ * Atomically exit the associated ARC and block on a specified condvar
28
+ * until a signal is sent on that same condvar (as sync::cond.wait_on).
29
+ *
30
+ * wait() is equivalent to wait_on(0).
31
+ */
32
+ #[ inline( always) ]
33
+ fn wait_on ( condvar_id : uint ) {
24
34
assert !* self . failed ;
25
- self . cond . wait ( ) ;
35
+ self . cond . wait_on ( condvar_id ) ;
26
36
// This is why we need to wrap sync::condvar.
27
37
check_poison ( self . is_mutex , * self . failed ) ;
28
38
}
29
39
/// Wake up a blocked task. Returns false if there was no blocked task.
30
- fn signal ( ) -> bool {
40
+ #[ inline( always) ]
41
+ fn signal ( ) -> bool { self . signal_on ( 0 ) }
42
+ /**
43
+ * Wake up a blocked task on a specified condvar (as
44
+ * sync::cond.signal_on). Returns false if there was no blocked task.
45
+ */
46
+ #[ inline( always) ]
47
+ fn signal_on ( condvar_id : uint ) -> bool {
31
48
assert !* self . failed ;
32
- self . cond . signal ( )
49
+ self . cond . signal_on ( condvar_id )
33
50
}
34
51
/// Wake up all blocked tasks. Returns the number of tasks woken.
35
- fn broadcast ( ) -> uint {
52
+ #[ inline( always) ]
53
+ fn broadcast ( ) -> uint { self . broadcast_on ( 0 ) }
54
+ /**
55
+ * Wake up all blocked tasks on a specified condvar (as
56
+ * sync::cond.broadcast_on). Returns Returns the number of tasks woken.
57
+ */
58
+ #[ inline( always) ]
59
+ fn broadcast_on ( condvar_id : uint ) -> uint {
36
60
assert !* self . failed ;
37
- self . cond . broadcast ( )
61
+ self . cond . broadcast_on ( condvar_id )
38
62
}
39
63
}
40
64
@@ -79,9 +103,17 @@ struct mutex_arc<T: send> { x: SharedMutableState<mutex_arc_inner<T>>; }
79
103
80
104
/// Create a mutex-protected ARC with the supplied data.
81
105
fn mutex_arc < T : send > ( +user_data : T ) -> mutex_arc < T > {
82
- let data = mutex_arc_inner {
83
- lock : mutex ( ) , failed : false , data : user_data
84
- } ;
106
+ mutex_arc_with_condvars ( user_data, 1 )
107
+ }
108
+ /**
109
+ * Create a mutex-protected ARC with the supplied data and a specified number
110
+ * of condvars (as sync::mutex_with_condvars).
111
+ */
112
+ fn mutex_arc_with_condvars < T : send > ( +user_data : T ,
113
+ num_condvars : uint ) -> mutex_arc < T > {
114
+ let data =
115
+ mutex_arc_inner { lock : mutex_with_condvars ( num_condvars) ,
116
+ failed : false , data : user_data } ;
85
117
mutex_arc { x : unsafe { shared_mutable_state ( data) } }
86
118
}
87
119
@@ -187,9 +219,17 @@ struct rw_arc<T: const send> {
187
219
188
220
/// Create a reader/writer ARC with the supplied data.
189
221
fn rw_arc<T: const send>(+user_data: T) -> rw_arc<T> {
190
- let data = rw_arc_inner {
191
- lock: rwlock(), failed: false, data: user_data
192
- };
222
+ rw_arc_with_condvars(user_data, 1)
223
+ }
224
+ /**
225
+ * Create a reader/writer ARC with the supplied data and a specified number
226
+ * of condvars (as sync::rwlock_with_condvars).
227
+ */
228
+ fn rw_arc_with_condvars<T: const send>(+user_data: T,
229
+ num_condvars: uint) -> rw_arc<T> {
230
+ let data =
231
+ rw_arc_inner { lock: rwlock_with_condvars(num_condvars),
232
+ failed: false, data: user_data };
193
233
rw_arc { x: unsafe { shared_mutable_state(data) }, cant_nest: () }
194
234
}
195
235
0 commit comments