Skip to content

Commit 4f90536

Browse files
authored
Merge pull request #393 from wedsonaf/context
binder: replace `Arc<Context>` with `Ref<Context>`.
2 parents 4bfaa80 + 278fca2 commit 4f90536

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

drivers/android/context.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
extern crate alloc;
44

5-
use alloc::sync::Arc;
6-
use core::pin::Pin;
7-
use kernel::{bindings, prelude::*, sync::Mutex, Error};
5+
use kernel::{
6+
bindings,
7+
prelude::*,
8+
sync::{Mutex, Ref},
9+
Error,
10+
};
811

912
use crate::{
1013
node::NodeRef,
@@ -24,24 +27,23 @@ unsafe impl Send for Context {}
2427
unsafe impl Sync for Context {}
2528

2629
impl Context {
27-
pub(crate) fn new() -> Result<Pin<Arc<Self>>> {
28-
let mut ctx_ref = Arc::try_new(Self {
29-
// SAFETY: Init is called below.
30-
manager: unsafe {
31-
Mutex::new(Manager {
32-
node: None,
33-
uid: None,
34-
})
30+
pub(crate) fn new() -> Result<Ref<Self>> {
31+
Ref::try_new_and_init(
32+
Self {
33+
// SAFETY: Init is called below.
34+
manager: unsafe {
35+
Mutex::new(Manager {
36+
node: None,
37+
uid: None,
38+
})
39+
},
3540
},
36-
})?;
37-
let ctx = Arc::get_mut(&mut ctx_ref).unwrap();
38-
39-
// SAFETY: `manager` is also pinned when `ctx` is.
40-
let manager = unsafe { Pin::new_unchecked(&mut ctx.manager) };
41-
kernel::mutex_init!(manager, "Context::manager");
42-
43-
// SAFETY: `ctx_ref` is pinned behind the `Arc` reference.
44-
Ok(unsafe { Pin::new_unchecked(ctx_ref) })
41+
|mut ctx| {
42+
// SAFETY: `manager` is also pinned when `ctx` is.
43+
let manager = unsafe { ctx.as_mut().map_unchecked_mut(|c| &mut c.manager) };
44+
kernel::mutex_init!(manager, "Context::manager");
45+
},
46+
)
4547
}
4648

4749
pub(crate) fn set_manager_node(&self, node_ref: NodeRef) -> Result {

drivers/android/process.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ impl ProcessNodeRefs {
274274
}
275275

276276
pub(crate) struct Process {
277-
ctx: Arc<Context>,
277+
ctx: Ref<Context>,
278278

279279
// TODO: For now this a mutex because we have allocations in BTreeMap and RangeAllocator while
280280
// holding the lock. We may want to split up the process state at some point to use a spin lock
@@ -291,8 +291,8 @@ unsafe impl Send for Process {}
291291
unsafe impl Sync for Process {}
292292

293293
impl Process {
294-
fn new(ctx: Arc<Context>) -> Result<Ref<Self>> {
295-
Ref::try_new_and_init(
294+
fn new(ctx: Ref<Context>) -> Result<Pin<Ref<Self>>> {
295+
Ok(Ref::pinned(Ref::try_new_and_init(
296296
Self {
297297
ctx,
298298
// SAFETY: `inner` is initialised in the call to `mutex_init` below.
@@ -308,7 +308,7 @@ impl Process {
308308
let pinned = unsafe { process.as_mut().map_unchecked_mut(|p| &mut p.node_refs) };
309309
kernel::mutex_init!(pinned, "Process::node_refs");
310310
},
311-
)
311+
)?))
312312
}
313313

314314
/// Attemps to fetch a work item from the process queue.
@@ -808,11 +808,9 @@ impl IoctlHandler for Process {
808808
}
809809
}
810810

811-
impl FileOpener<Arc<Context>> for Process {
812-
fn open(ctx: &Arc<Context>) -> Result<Self::Wrapper> {
813-
let process = Self::new(ctx.clone())?;
814-
// SAFETY: Pointer is pinned behind `Ref`.
815-
Ok(unsafe { Pin::new_unchecked(process) })
811+
impl FileOpener<Ref<Context>> for Process {
812+
fn open(ctx: &Ref<Context>) -> Result<Self::Wrapper> {
813+
Self::new(ctx.clone())
816814
}
817815
}
818816

drivers/android/rust_binder.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use kernel::{
1515
linked_list::{GetLinks, GetLinksWrapped, Links},
1616
miscdev::Registration,
1717
prelude::*,
18+
sync::Ref,
1819
user_ptr::UserSlicePtrWriter,
1920
};
2021

@@ -103,18 +104,13 @@ const fn ptr_align(value: usize) -> usize {
103104
unsafe impl Sync for BinderModule {}
104105

105106
struct BinderModule {
106-
_reg: Pin<Box<Registration<Arc<Context>>>>,
107+
_reg: Pin<Box<Registration<Ref<Context>>>>,
107108
}
108109

109110
impl KernelModule for BinderModule {
110111
fn init() -> Result<Self> {
111-
let pinned_ctx = Context::new()?;
112-
let ctx = unsafe { Pin::into_inner_unchecked(pinned_ctx) };
113-
let reg = Registration::<Arc<Context>>::new_pinned::<process::Process>(
114-
c_str!("rust_binder"),
115-
None,
116-
ctx,
117-
)?;
112+
let ctx = Context::new()?;
113+
let reg = Registration::new_pinned::<process::Process>(c_str!("rust_binder"), None, ctx)?;
118114
Ok(Self { _reg: reg })
119115
}
120116
}

0 commit comments

Comments
 (0)