|
47 | 47 |
|
48 | 48 | #[allow(non_camel_case_types)]; |
49 | 49 |
|
| 50 | +use int; |
50 | 51 | use libc::c_void; |
51 | 52 | use sync::atomics; |
52 | 53 |
|
@@ -339,15 +340,15 @@ mod imp { |
339 | 340 | /// ``` |
340 | 341 | pub struct Once { |
341 | 342 | priv mutex: Mutex, |
342 | | - priv cnt: AtomicInt, |
343 | | - priv lock_cnt: AtomicInt, |
| 343 | + priv cnt: atomics::AtomicInt, |
| 344 | + priv lock_cnt: atomics::AtomicInt, |
344 | 345 | } |
345 | 346 |
|
346 | 347 | /// Initialization value for static `Once` values. |
347 | 348 | pub static ONCE_INIT: Once = Once { |
348 | 349 | mutex: MUTEX_INIT, |
349 | | - cnt: INIT_ATOMIC_INT, |
350 | | - lock_cnt: INIT_ATOMIC_INT, |
| 350 | + cnt: atomics::INIT_ATOMIC_INT, |
| 351 | + lock_cnt: atomics::INIT_ATOMIC_INT, |
351 | 352 | }; |
352 | 353 |
|
353 | 354 | impl Once { |
@@ -388,34 +389,36 @@ impl Once { |
388 | 389 | // calling `doit` will return immediately before the initialization has |
389 | 390 | // completed. |
390 | 391 |
|
391 | | - let prev = self.cnt.fetch_add(1, SeqCst); |
| 392 | + let prev = self.cnt.fetch_add(1, atomics::SeqCst); |
392 | 393 | if prev < 0 { |
393 | 394 | // Make sure we never overflow, we'll never have int::min_value |
394 | 395 | // simultaneous calls to `doit` to make this value go back to 0 |
395 | | - self.cnt.store(int::min_value, SeqCst); |
| 396 | + self.cnt.store(int::min_value, atomics::SeqCst); |
396 | 397 | return |
397 | 398 | } |
398 | 399 |
|
399 | 400 | // If the count is negative, then someone else finished the job, |
400 | 401 | // otherwise we run the job and record how many people will try to grab |
401 | 402 | // this lock |
402 | 403 | unsafe { self.mutex.lock() } |
403 | | - if self.cnt.load(SeqCst) > 0 { |
| 404 | + if self.cnt.load(atomics::SeqCst) > 0 { |
404 | 405 | f(); |
405 | | - let prev = self.cnt.swap(int::min_value, SeqCst); |
406 | | - self.lock_cnt.store(prev, SeqCst); |
| 406 | + let prev = self.cnt.swap(int::min_value, atomics::SeqCst); |
| 407 | + self.lock_cnt.store(prev, atomics::SeqCst); |
407 | 408 | } |
408 | 409 | unsafe { self.mutex.unlock() } |
409 | 410 |
|
410 | 411 | // Last one out cleans up after everyone else, no leaks! |
411 | | - if self.lock_cnt.fetch_add(-1, SeqCst) == 1 { |
| 412 | + if self.lock_cnt.fetch_add(-1, atomics::SeqCst) == 1 { |
412 | 413 | unsafe { self.mutex.destroy() } |
413 | 414 | } |
414 | 415 | } |
415 | 416 | } |
416 | 417 |
|
417 | 418 | #[cfg(test)] |
418 | 419 | mod test { |
| 420 | + use prelude::*; |
| 421 | + |
419 | 422 | use rt::thread::Thread; |
420 | 423 | use super::{ONCE_INIT, Once, Mutex, MUTEX_INIT}; |
421 | 424 | use task; |
|
0 commit comments