Skip to content

Commit 3ae4254

Browse files
committed
binder: remove the call to Arc::get_mut from ThreadError.
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 <[email protected]>
1 parent 1b842c3 commit 3ae4254

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

drivers/android/thread.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
use core::{alloc::AllocError, mem::size_of};
3+
use core::{
4+
alloc::AllocError,
5+
mem::size_of,
6+
sync::atomic::{AtomicU32, Ordering},
7+
};
48
use kernel::{
59
bindings,
610
file::File,
@@ -120,11 +124,13 @@ impl InnerThread {
120124
fn push_existing_work(&mut self, owork: Option<Arc<ThreadError>>, code: u32) {
121125
// TODO: Write some warning when the following fails. It should not happen, and
122126
// if it does, there is likely something wrong.
123-
if let Some(mut work) = owork {
124-
if let Some(work_mut) = Arc::get_mut(&mut work) {
125-
work_mut.error_code = code;
126-
self.push_work(work);
127-
}
127+
if let Some(work) = owork {
128+
// `error_code` is written to with relaxed semantics because the queue onto which it is
129+
// being inserted is protected by a lock. The release barrier when the lock is released
130+
// by the caller matches with the acquire barrier of the future reader to guarantee
131+
// that `error_code` is visible.
132+
work.error_code.store(code, Ordering::Relaxed);
133+
self.push_work(work);
128134
}
129135
}
130136

@@ -824,15 +830,15 @@ impl GetLinks for Thread {
824830
}
825831

826832
struct ThreadError {
827-
error_code: u32,
833+
error_code: AtomicU32,
828834
return_fn: fn(&mut InnerThread, Arc<ThreadError>),
829835
links: Links<dyn DeliverToRead>,
830836
}
831837

832838
impl ThreadError {
833839
fn new(return_fn: fn(&mut InnerThread, Arc<ThreadError>)) -> Self {
834840
Self {
835-
error_code: BR_OK,
841+
error_code: AtomicU32::new(BR_OK),
836842
return_fn,
837843
links: Links::new(),
838844
}
@@ -841,7 +847,9 @@ impl ThreadError {
841847

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

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

0 commit comments

Comments
 (0)