@@ -100,30 +100,34 @@ fn new_sem_and_signal(count: int, num_condvars: uint)
100
100
#[ doc( hidden) ]
101
101
pub impl < Q : Owned > Sem < Q > {
102
102
fn acquire ( & self ) {
103
- let mut waiter_nobe = None ;
104
- do ( * * self ) . with |state| {
105
- state. count -= 1 ;
106
- if state. count < 0 {
107
- // Create waiter nobe.
108
- let ( WaitEnd , SignalEnd ) = comm:: oneshot ( ) ;
109
- // Tell outer scope we need to block.
110
- waiter_nobe = Some ( WaitEnd ) ;
111
- // Enqueue ourself.
112
- state. waiters . tail . send ( SignalEnd ) ;
103
+ unsafe {
104
+ let mut waiter_nobe = None ;
105
+ do ( * * self ) . with |state| {
106
+ state. count -= 1 ;
107
+ if state. count < 0 {
108
+ // Create waiter nobe.
109
+ let ( WaitEnd , SignalEnd ) = comm:: oneshot ( ) ;
110
+ // Tell outer scope we need to block.
111
+ waiter_nobe = Some ( WaitEnd ) ;
112
+ // Enqueue ourself.
113
+ state. waiters . tail . send ( SignalEnd ) ;
114
+ }
115
+ }
116
+ // Uncomment if you wish to test for sem races. Not valgrind-friendly.
117
+ /* for 1000.times { task::yield(); } */
118
+ // Need to wait outside the exclusive.
119
+ if waiter_nobe. is_some ( ) {
120
+ let _ = comm:: recv_one ( waiter_nobe. unwrap ( ) ) ;
113
121
}
114
- }
115
- // Uncomment if you wish to test for sem races. Not valgrind-friendly.
116
- /* for 1000.times { task::yield(); } */
117
- // Need to wait outside the exclusive.
118
- if waiter_nobe. is_some ( ) {
119
- let _ = comm:: recv_one ( waiter_nobe. unwrap ( ) ) ;
120
122
}
121
123
}
122
124
fn release ( & self ) {
123
- do ( * * self ) . with |state| {
124
- state. count += 1 ;
125
- if state. count <= 0 {
126
- signal_waitqueue ( & state. waiters ) ;
125
+ unsafe {
126
+ do ( * * self ) . with |state| {
127
+ state. count += 1 ;
128
+ if state. count <= 0 {
129
+ signal_waitqueue ( & state. waiters ) ;
130
+ }
127
131
}
128
132
}
129
133
}
@@ -283,17 +287,19 @@ pub impl<'self> Condvar<'self> {
283
287
284
288
/// As signal, but with a specified condvar_id. See wait_on.
285
289
fn signal_on ( & self , condvar_id : uint ) -> bool {
286
- let mut out_of_bounds = None ;
287
- let mut result = false ;
288
- do ( * * self . sem ) . with |state| {
289
- if condvar_id < state. blocked . len ( ) {
290
- result = signal_waitqueue ( & state. blocked [ condvar_id] ) ;
291
- } else {
292
- out_of_bounds = Some ( state. blocked . len ( ) ) ;
290
+ unsafe {
291
+ let mut out_of_bounds = None ;
292
+ let mut result = false ;
293
+ do ( * * self . sem ) . with |state| {
294
+ if condvar_id < state. blocked . len ( ) {
295
+ result = signal_waitqueue ( & state. blocked [ condvar_id] ) ;
296
+ } else {
297
+ out_of_bounds = Some ( state. blocked . len ( ) ) ;
298
+ }
299
+ }
300
+ do check_cvar_bounds( out_of_bounds, condvar_id, "cond.signal_on()" ) {
301
+ result
293
302
}
294
- }
295
- do check_cvar_bounds( out_of_bounds, condvar_id, "cond.signal_on()" ) {
296
- result
297
303
}
298
304
}
299
305
@@ -304,20 +310,22 @@ pub impl<'self> Condvar<'self> {
304
310
fn broadcast_on ( & self , condvar_id : uint ) -> uint {
305
311
let mut out_of_bounds = None ;
306
312
let mut queue = None ;
307
- do ( * * self . sem ) . with |state| {
308
- if condvar_id < state. blocked . len ( ) {
309
- // To avoid :broadcast_heavy, we make a new waitqueue,
310
- // swap it out with the old one, and broadcast on the
311
- // old one outside of the little-lock.
312
- queue = Some ( util:: replace ( & mut state. blocked [ condvar_id] ,
313
- new_waitqueue ( ) ) ) ;
314
- } else {
315
- out_of_bounds = Some ( state. blocked . len ( ) ) ;
313
+ unsafe {
314
+ do ( * * self . sem ) . with |state| {
315
+ if condvar_id < state. blocked . len ( ) {
316
+ // To avoid :broadcast_heavy, we make a new waitqueue,
317
+ // swap it out with the old one, and broadcast on the
318
+ // old one outside of the little-lock.
319
+ queue = Some ( util:: replace ( & mut state. blocked [ condvar_id] ,
320
+ new_waitqueue ( ) ) ) ;
321
+ } else {
322
+ out_of_bounds = Some ( state. blocked . len ( ) ) ;
323
+ }
324
+ }
325
+ do check_cvar_bounds( out_of_bounds, condvar_id, "cond.signal_on()" ) {
326
+ let queue = queue. swap_unwrap ( ) ;
327
+ broadcast_waitqueue ( & queue)
316
328
}
317
- }
318
- do check_cvar_bounds( out_of_bounds, condvar_id, "cond.signal_on()" ) {
319
- let queue = queue. swap_unwrap ( ) ;
320
- broadcast_waitqueue ( & queue)
321
329
}
322
330
}
323
331
}
0 commit comments