Skip to content
This repository has been archived by the owner on Dec 9, 2018. It is now read-only.

Commit

Permalink
Implement sync::RwLock
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol committed May 6, 2017
1 parent 9419e72 commit eb57de9
Show file tree
Hide file tree
Showing 12 changed files with 1,237 additions and 6 deletions.
57 changes: 57 additions & 0 deletions examples/rwlock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#[cfg(not(any(target_arch = "aarch64",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "x86",
target_arch = "x86_64")))]
fn main() {}

#[cfg(any(target_arch = "aarch64",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "x86",
target_arch = "x86_64"))]
fn main() {
use std::{time, thread};
use std::sync::{Arc, RwLock};

let mut threads = vec![];
const NTHREADS: i32 = 100;
const ITERATIONS: i32 = 100;

let lock = Arc::new(RwLock::new(5));
for _ in 0..NTHREADS {
let lock = lock.clone();
let t = thread::spawn(move ||
for _ in 0..ITERATIONS {
// sleep for 1 ms
thread::sleep(time::Duration::from_millis(1));
{
let r1 = lock.read().unwrap();
assert!(*r1 >= 5);

match lock.try_read() {
Ok(r2) => assert!(*r2 >= 5),
Err(_) => {},
}
}

{
//let mut w = lock.try_write().unwrap();
//*w += 1;
}

{
let mut w = lock.write().unwrap();
*w += 1;
}
});

threads.push(t);
}

for t in threads {
t.join().unwrap();
}

assert_eq!(*lock.read().unwrap(), 5 + NTHREADS * ITERATIONS);
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#![feature(macro_reexport)]
#![feature(naked_functions)]
#![feature(oom)]
#![feature(optin_builtin_traits)]
#![feature(prelude_import)]
#![feature(rand)]
#![feature(raw)]
Expand Down
4 changes: 4 additions & 0 deletions src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ pub const CLONE_VM: c_ulong = 0x00000100;

// include/uapi/linux/futex.h
pub const FUTEX_WAIT: c_int = 0;
pub const FUTEX_WAKE: c_int = 1;
pub const FUTEX_PRIVATE_FLAG: c_int = 128;
pub const FUTEX_WAIT_PRIVATE: c_int = FUTEX_WAIT | FUTEX_PRIVATE_FLAG;
pub const FUTEX_WAKE_PRIVATE: c_int = FUTEX_WAKE | FUTEX_PRIVATE_FLAG;

// kernel/time/posix-timers.c
#[inline(always)]
Expand Down
6 changes: 0 additions & 6 deletions src/sync.rs

This file was deleted.

44 changes: 44 additions & 0 deletions src/sync/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Useful synchronization primitives.
//!
//! This module contains useful safe and unsafe synchronization primitives.
//! Most of the primitives in this module do not provide any sort of locking
//! and/or blocking at all, but rather provide the necessary tools to build
//! other types of concurrent primitives.

#![stable(feature = "rust1", since = "1.0.0")]

#[stable(feature = "rust1", since = "1.0.0")]
pub use alloc::arc::{Arc, Weak};
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::sync::atomic;

// #[stable(feature = "rust1", since = "1.0.0")]
// pub use self::barrier::{Barrier, BarrierWaitResult};
// #[stable(feature = "rust1", since = "1.0.0")]
// pub use self::condvar::{Condvar, WaitTimeoutResult};
// #[stable(feature = "rust1", since = "1.0.0")]
// pub use self::mutex::{Mutex, MutexGuard};
// #[stable(feature = "rust1", since = "1.0.0")]
// pub use self::once::{Once, OnceState, ONCE_INIT};
// #[stable(feature = "rust1", since = "1.0.0")]
// pub use sys_common::poison::{PoisonError, TryLockError, TryLockResult, LockResult};
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};

// pub mod mpsc;

// mod barrier;
// mod condvar;
// mod mutex;
// mod once;
mod rwlock;
Loading

0 comments on commit eb57de9

Please sign in to comment.