Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicated code on RwLock #127

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 17 additions & 36 deletions src/rt/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,16 @@ impl RwLock {
}

pub(crate) fn release_read_lock(&self) {
super::execution(|execution| {
let state = self.state.get_mut(&mut execution.objects);

state.lock = None;

state
.synchronize
.sync_store(&mut execution.threads, Release);

// Establish sequential consistency between the lock's operations.
execution.threads.seq_cst();

let thread_id = execution.threads.active_id();

self.unlock_threads(execution, thread_id);
});
// Both release_read_lock and release_write_lock uses the same mechanism
self.release_lock();
}

pub(crate) fn release_write_lock(&self) {
// Both release_read_lock and release_write_lock uses the same mechanism
self.release_lock();
}

fn release_lock(&self) {
super::execution(|execution| {
let state = self.state.get_mut(&mut execution.objects);

Expand All @@ -111,9 +102,10 @@ impl RwLock {
});
}

fn lock_out_threads(&self, execution: &mut Execution, thread_id: thread::Id) {
// TODO: This and the following function look very similar.
// Refactor the two to DRY the code.
fn drain_threads<F>(&self, execution: &mut Execution, thread_id: thread::Id, func: F)
where
F: Fn(&mut thread::Thread),
{
for (id, thread) in execution.threads.iter_mut() {
if id == thread_id {
continue;
Expand All @@ -125,28 +117,17 @@ impl RwLock {
.map(|operation| operation.object());

if obj == Some(self.state.erase()) {
thread.set_blocked();
func(thread);
}
}
}

fn unlock_threads(&self, execution: &mut Execution, thread_id: thread::Id) {
// TODO: This and the above function look very similar.
// Refactor the two to DRY the code.
for (id, thread) in execution.threads.iter_mut() {
if id == thread_id {
continue;
}

let obj = thread
.operation
.as_ref()
.map(|operation| operation.object());
fn lock_out_threads(&self, execution: &mut Execution, thread_id: thread::Id) {
self.drain_threads(execution, thread_id, thread::Thread::set_blocked);
}

if obj == Some(self.state.erase()) {
thread.set_runnable();
}
}
fn unlock_threads(&self, execution: &mut Execution, thread_id: thread::Id) {
self.drain_threads(execution, thread_id, thread::Thread::set_runnable);
}

/// Returns `true` if RwLock is read locked
Expand Down