Skip to content

Commit

Permalink
binder: remove the call to Arc::get_mut from ThreadError.
Browse files Browse the repository at this point in the history
This is in preparation for switching to `Ref`. Since it doesn't support
`get_mut`, we switch to using relaxed atomics because there is already
synchronisation between setting the error code and using it.

Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com>
  • Loading branch information
wedsonaf committed Jul 5, 2021
1 parent 1b842c3 commit 3ae4254
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions drivers/android/thread.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// SPDX-License-Identifier: GPL-2.0

use core::{alloc::AllocError, mem::size_of};
use core::{
alloc::AllocError,
mem::size_of,
sync::atomic::{AtomicU32, Ordering},
};
use kernel::{
bindings,
file::File,
Expand Down Expand Up @@ -120,11 +124,13 @@ impl InnerThread {
fn push_existing_work(&mut self, owork: Option<Arc<ThreadError>>, code: u32) {
// TODO: Write some warning when the following fails. It should not happen, and
// if it does, there is likely something wrong.
if let Some(mut work) = owork {
if let Some(work_mut) = Arc::get_mut(&mut work) {
work_mut.error_code = code;
self.push_work(work);
}
if let Some(work) = owork {
// `error_code` is written to with relaxed semantics because the queue onto which it is
// being inserted is protected by a lock. The release barrier when the lock is released
// by the caller matches with the acquire barrier of the future reader to guarantee
// that `error_code` is visible.
work.error_code.store(code, Ordering::Relaxed);
self.push_work(work);
}
}

Expand Down Expand Up @@ -824,15 +830,15 @@ impl GetLinks for Thread {
}

struct ThreadError {
error_code: u32,
error_code: AtomicU32,
return_fn: fn(&mut InnerThread, Arc<ThreadError>),
links: Links<dyn DeliverToRead>,
}

impl ThreadError {
fn new(return_fn: fn(&mut InnerThread, Arc<ThreadError>)) -> Self {
Self {
error_code: BR_OK,
error_code: AtomicU32::new(BR_OK),
return_fn,
links: Links::new(),
}
Expand All @@ -841,7 +847,9 @@ impl ThreadError {

impl DeliverToRead for ThreadError {
fn do_work(self: Arc<Self>, thread: &Thread, writer: &mut UserSlicePtrWriter) -> Result<bool> {
let code = self.error_code;
// See `ThreadInner::push_existing_work` for the reason why `error_code` is up to date even
// though we use relaxed semantics.
let code = self.error_code.load(Ordering::Relaxed);

// Return the `ThreadError` to the thread.
(self.return_fn)(&mut *thread.inner.lock(), self);
Expand Down

0 comments on commit 3ae4254

Please sign in to comment.