Skip to content

Commit 53e451f

Browse files
committed
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.
1 parent 3572a30 commit 53e451f

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/libsync/sync/one.rs renamed to src/libsync/one.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
1616
use std::int;
1717
use std::sync::atomics;
18-
use sync::mutex::{StaticMutex, MUTEX_INIT};
18+
19+
use mutex::{StaticMutex, MUTEX_INIT};
1920

2021
/// A type which can be used to run a one-time global initialization. This type
2122
/// is *unsafe* to use because it is built on top of the `Mutex` in this module.
@@ -62,7 +63,7 @@ impl Once {
6263
///
6364
/// When this function returns, it is guaranteed that some initialization
6465
/// has run and completed (it may not be the closure specified).
65-
pub fn doit(&mut self, f: ||) {
66+
pub fn doit(&self, f: ||) {
6667
// Implementation-wise, this would seem like a fairly trivial primitive.
6768
// The stickler part is where our mutexes currently require an
6869
// allocation, and usage of a `Once` should't leak this allocation.
@@ -101,14 +102,13 @@ impl Once {
101102
// If the count is negative, then someone else finished the job,
102103
// otherwise we run the job and record how many people will try to grab
103104
// this lock
104-
{
105-
let _guard = self.mutex.lock();
106-
if self.cnt.load(atomics::SeqCst) > 0 {
107-
f();
108-
let prev = self.cnt.swap(int::MIN, atomics::SeqCst);
109-
self.lock_cnt.store(prev, atomics::SeqCst);
110-
}
105+
let guard = self.mutex.lock();
106+
if self.cnt.load(atomics::SeqCst) > 0 {
107+
f();
108+
let prev = self.cnt.swap(int::MIN, atomics::SeqCst);
109+
self.lock_cnt.store(prev, atomics::SeqCst);
111110
}
111+
drop(guard);
112112

113113
// Last one out cleans up after everyone else, no leaks!
114114
if self.lock_cnt.fetch_add(-1, atomics::SeqCst) == 1 {

0 commit comments

Comments
 (0)