Skip to content

Commit

Permalink
Wrap pthread types in UnsafeCell in benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanieu committed May 25, 2016
1 parent 61ddee6 commit 358b21a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
15 changes: 11 additions & 4 deletions benchmark/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,33 @@ impl<T> Mutex<T> for parking_lot::Mutex<T> {
}
}

struct PthreadMutex<T>(UnsafeCell<T>, libc::pthread_mutex_t);
struct PthreadMutex<T>(UnsafeCell<T>, UnsafeCell<libc::pthread_mutex_t>);
unsafe impl<T> Sync for PthreadMutex<T> {}
impl<T> Mutex<T> for PthreadMutex<T> {
fn new(v: T) -> Self {
PthreadMutex(UnsafeCell::new(v), libc::PTHREAD_MUTEX_INITIALIZER)
PthreadMutex(UnsafeCell::new(v), UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER))
}
fn lock<F, R>(&self, f: F) -> R
where F: FnOnce(&mut T) -> R
{
unsafe {
libc::pthread_mutex_lock(&self.1 as *const _ as *mut _);
libc::pthread_mutex_lock(self.1.get());
let res = f(&mut *self.0.get());
libc::pthread_mutex_unlock(&self.1 as *const _ as *mut _);
libc::pthread_mutex_unlock(self.1.get());
res
}
}
fn name() -> &'static str {
"pthread_mutex_t"
}
}
impl<T> Drop for PthreadMutex<T> {
fn drop(&mut self) {
unsafe {
libc::pthread_mutex_destroy(self.1.get());
}
}
}

fn run_benchmark<M: Mutex<f64> + Send + Sync + 'static>(num_threads: usize,
work_per_critical_section: usize,
Expand Down
19 changes: 13 additions & 6 deletions benchmark/src/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,43 @@ impl<T> RwLock<T> for parking_lot::RwLock<T> {
}
}

struct PthreadRwLock<T>(UnsafeCell<T>, libc::pthread_rwlock_t);
struct PthreadRwLock<T>(UnsafeCell<T>, UnsafeCell<libc::pthread_rwlock_t>);
unsafe impl<T> Sync for PthreadRwLock<T> {}
impl<T> RwLock<T> for PthreadRwLock<T> {
fn new(v: T) -> Self {
PthreadRwLock(UnsafeCell::new(v), libc::PTHREAD_RWLOCK_INITIALIZER)
PthreadRwLock(UnsafeCell::new(v), UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER))
}
fn read<F, R>(&self, f: F) -> R
where F: FnOnce(&T) -> R
{
unsafe {
libc::pthread_rwlock_wrlock(&self.1 as *const _ as *mut _);
libc::pthread_rwlock_wrlock(self.1.get());
let res = f(&*self.0.get());
libc::pthread_rwlock_unlock(&self.1 as *const _ as *mut _);
libc::pthread_rwlock_unlock(self.1.get());
res
}
}
fn write<F, R>(&self, f: F) -> R
where F: FnOnce(&mut T) -> R
{
unsafe {
libc::pthread_rwlock_wrlock(&self.1 as *const _ as *mut _);
libc::pthread_rwlock_wrlock(self.1.get());
let res = f(&mut *self.0.get());
libc::pthread_rwlock_unlock(&self.1 as *const _ as *mut _);
libc::pthread_rwlock_unlock(self.1.get());
res
}
}
fn name() -> &'static str {
"pthread_rwlock_t"
}
}
impl<T> Drop for PthreadRwLock<T> {
fn drop(&mut self) {
unsafe {
libc::pthread_rwlock_destroy(self.1.get());
}
}
}

fn run_benchmark<M: RwLock<f64> + Send + Sync + 'static>(num_writer_threads: usize,
num_reader_threads: usize,
Expand Down

0 comments on commit 358b21a

Please sign in to comment.