Skip to content

Commit

Permalink
Make sync::raw::Sem use Unsafe to manage mutability instead of transm…
Browse files Browse the repository at this point in the history
…uting an unsafe pointer
  • Loading branch information
Jonathan S committed Jun 6, 2014
1 parent 566b7b9 commit f2f1991
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions src/libsync/raw.rs
Expand Up @@ -18,6 +18,7 @@
use std::kinds::marker;
use std::mem;
use std::sync::atomics;
use std::ty::Unsafe;
use std::finally::Finally;

use mutex;
Expand Down Expand Up @@ -85,11 +86,8 @@ struct Sem<Q> {
// n.b, we need Sem to be `Share`, but the WaitQueue type is not send/share
// (for good reason). We have an internal invariant on this semaphore,
// however, that the queue is never accessed outside of a locked
// context. For this reason, we shove these behind a pointer which will
// be inferred to be `Share`.
//
// FIXME: this requires an extra allocation, which is bad.
inner: *()
// context.
inner: Unsafe<SemInner<Q>>
}

struct SemInner<Q> {
Expand All @@ -107,22 +105,20 @@ struct SemGuard<'a, Q> {

impl<Q: Send> Sem<Q> {
fn new(count: int, q: Q) -> Sem<Q> {
let inner = unsafe {
mem::transmute(box SemInner {
Sem {
lock: mutex::Mutex::new(),
inner: Unsafe::new(SemInner {
waiters: WaitQueue::new(),
count: count,
blocked: q,
})
};
Sem {
lock: mutex::Mutex::new(),
inner: inner,
}
}

unsafe fn with(&self, f: |&mut SemInner<Q>|) {
let _g = self.lock.lock();
f(&mut *(self.inner as *mut SemInner<Q>))
// This &mut is safe because, due to the lock, we are the only one who can touch the data
f(&mut *self.inner.get())
}

pub fn acquire(&self) {
Expand Down Expand Up @@ -163,16 +159,6 @@ impl<Q: Send> Sem<Q> {
}
}

#[unsafe_destructor]
impl<Q: Send> Drop for Sem<Q> {
fn drop(&mut self) {
let _waiters: Box<SemInner<Q>> = unsafe {
mem::transmute(self.inner)
};
self.inner = 0 as *();
}
}

#[unsafe_destructor]
impl<'a, Q: Send> Drop for SemGuard<'a, Q> {
fn drop(&mut self) {
Expand Down

5 comments on commit f2f1991

@bors
Copy link
Contributor

@bors bors commented on f2f1991 Jun 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at gereeter@f2f1991

@bors
Copy link
Contributor

@bors bors commented on f2f1991 Jun 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging gereeter/rust/faster-sem = f2f1991 into auto

@bors
Copy link
Contributor

@bors bors commented on f2f1991 Jun 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gereeter/rust/faster-sem = f2f1991 merged ok, testing candidate = 8e9e484

@bors
Copy link
Contributor

@bors bors commented on f2f1991 Jun 7, 2014

@bors
Copy link
Contributor

@bors bors commented on f2f1991 Jun 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 8e9e484

Please sign in to comment.