diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2014-03-22 00:49:16 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-03-23 09:45:20 -0700 |
| commit | 53e451f4106c0eb6614b4c534744e81c6100cbbd (patch) | |
| tree | d39c44c95baf7409fb76616b2cd24471a6c69e29 /src/libsync | |
| parent | 3572a30e7a4fec7f0bb0957fc72588757111f14e (diff) | |
| download | rust-53e451f4106c0eb6614b4c534744e81c6100cbbd.tar.gz rust-53e451f4106c0eb6614b4c534744e81c6100cbbd.zip | |
sync: Move Once to using &self
Similarly to the rest of the previous commits, this moves the once primitive to using &self instead of &mut self for proper sharing among many threads now.
Diffstat (limited to 'src/libsync')
| -rw-r--r-- | src/libsync/one.rs (renamed from src/libsync/sync/one.rs) | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/src/libsync/sync/one.rs b/src/libsync/one.rs index c5e83bed0ed..161f759ca2d 100644 --- a/src/libsync/sync/one.rs +++ b/src/libsync/one.rs @@ -15,7 +15,8 @@ use std::int; use std::sync::atomics; -use sync::mutex::{StaticMutex, MUTEX_INIT}; + +use mutex::{StaticMutex, MUTEX_INIT}; /// A type which can be used to run a one-time global initialization. This type /// is *unsafe* to use because it is built on top of the `Mutex` in this module. @@ -62,7 +63,7 @@ impl Once { /// /// When this function returns, it is guaranteed that some initialization /// has run and completed (it may not be the closure specified). - pub fn doit(&mut self, f: ||) { + pub fn doit(&self, f: ||) { // Implementation-wise, this would seem like a fairly trivial primitive. // The stickler part is where our mutexes currently require an // allocation, and usage of a `Once` should't leak this allocation. @@ -101,14 +102,13 @@ impl Once { // If the count is negative, then someone else finished the job, // otherwise we run the job and record how many people will try to grab // this lock - { - let _guard = self.mutex.lock(); - if self.cnt.load(atomics::SeqCst) > 0 { - f(); - let prev = self.cnt.swap(int::MIN, atomics::SeqCst); - self.lock_cnt.store(prev, atomics::SeqCst); - } + let guard = self.mutex.lock(); + if self.cnt.load(atomics::SeqCst) > 0 { + f(); + let prev = self.cnt.swap(int::MIN, atomics::SeqCst); + self.lock_cnt.store(prev, atomics::SeqCst); } + drop(guard); // Last one out cleans up after everyone else, no leaks! if self.lock_cnt.fetch_add(-1, atomics::SeqCst) == 1 { |
