@@ -14,7 +14,7 @@ use kernel::{
14
14
linked_list:: List ,
15
15
pages:: Pages ,
16
16
prelude:: * ,
17
- sync:: { Guard , Mutex , Ref , RefCount , RefCounted } ,
17
+ sync:: { Guard , Mutex , Ref } ,
18
18
user_ptr:: { UserSlicePtr , UserSlicePtrReader } ,
19
19
Error ,
20
20
} ;
@@ -275,7 +275,6 @@ impl ProcessNodeRefs {
275
275
276
276
pub ( crate ) struct Process {
277
277
ctx : Arc < Context > ,
278
- ref_count : RefCount ,
279
278
280
279
// TODO: For now this a mutex because we have allocations in BTreeMap and RangeAllocator while
281
280
// holding the lock. We may want to split up the process state at some point to use a spin lock
@@ -293,22 +292,23 @@ unsafe impl Sync for Process {}
293
292
294
293
impl Process {
295
294
fn new ( ctx : Arc < Context > ) -> Result < Ref < Self > > {
296
- let mut proc_ref = Ref :: try_new ( Self {
297
- ref_count : RefCount :: new ( ) ,
298
- ctx,
299
- // SAFETY: `inner` is initialised in the call to `mutex_init` below.
300
- inner : unsafe { Mutex :: new ( ProcessInner :: new ( ) ) } ,
301
- // SAFETY: `node_refs` is initialised in the call to `mutex_init` below.
302
- node_refs : unsafe { Mutex :: new ( ProcessNodeRefs :: new ( ) ) } ,
303
- } ) ?;
304
- let process = Ref :: get_mut ( & mut proc_ref) . ok_or ( Error :: EINVAL ) ?;
305
- // SAFETY: `inner` is pinned behind the `Arc` reference.
306
- let pinned = unsafe { Pin :: new_unchecked ( & process. inner ) } ;
307
- kernel:: mutex_init!( pinned, "Process::inner" ) ;
308
- // SAFETY: `node_refs` is pinned behind the `Arc` reference.
309
- let pinned = unsafe { Pin :: new_unchecked ( & process. node_refs ) } ;
310
- kernel:: mutex_init!( pinned, "Process::node_refs" ) ;
311
- Ok ( proc_ref)
295
+ Ref :: try_new_and_init (
296
+ Self {
297
+ ctx,
298
+ // SAFETY: `inner` is initialised in the call to `mutex_init` below.
299
+ inner : unsafe { Mutex :: new ( ProcessInner :: new ( ) ) } ,
300
+ // SAFETY: `node_refs` is initialised in the call to `mutex_init` below.
301
+ node_refs : unsafe { Mutex :: new ( ProcessNodeRefs :: new ( ) ) } ,
302
+ } ,
303
+ |process| {
304
+ // SAFETY: `inner` is pinned behind the `Ref` reference.
305
+ let pinned = unsafe { Pin :: new_unchecked ( & process. inner ) } ;
306
+ kernel:: mutex_init!( pinned, "Process::inner" ) ;
307
+ // SAFETY: `node_refs` is pinned behind the `Ref` reference.
308
+ let pinned = unsafe { Pin :: new_unchecked ( & process. node_refs ) } ;
309
+ kernel:: mutex_init!( pinned, "Process::node_refs" ) ;
310
+ } ,
311
+ )
312
312
}
313
313
314
314
/// Attemps to fetch a work item from the process queue.
@@ -337,7 +337,7 @@ impl Process {
337
337
Either :: Right ( Registration :: new ( self , thread, & mut inner) )
338
338
}
339
339
340
- fn get_thread ( & self , id : i32 ) -> Result < Arc < Thread > > {
340
+ fn get_thread ( self : & Ref < Self > , id : i32 ) -> Result < Arc < Thread > > {
341
341
// TODO: Consider using read/write locks here instead.
342
342
{
343
343
let inner = self . inner . lock ( ) ;
@@ -347,7 +347,7 @@ impl Process {
347
347
}
348
348
349
349
// Allocate a new `Thread` without holding any locks.
350
- let ta = Thread :: new ( id, Ref :: new_from ( self ) ) ?;
350
+ let ta = Thread :: new ( id, self . clone ( ) ) ?;
351
351
352
352
let mut inner = self . inner . lock ( ) ;
353
353
@@ -366,7 +366,7 @@ impl Process {
366
366
self . inner . lock ( ) . push_work ( work)
367
367
}
368
368
369
- fn set_as_manager ( & self , info : Option < FlatBinderObject > , thread : & Thread ) -> Result {
369
+ fn set_as_manager ( self : & Ref < Self > , info : Option < FlatBinderObject > , thread : & Thread ) -> Result {
370
370
let ( ptr, cookie, flags) = if let Some ( obj) = info {
371
371
(
372
372
// SAFETY: The object type for this ioctl is implicitly `BINDER_TYPE_BINDER`, so it
@@ -390,7 +390,7 @@ impl Process {
390
390
}
391
391
392
392
pub ( crate ) fn get_node (
393
- & self ,
393
+ self : & Ref < Self > ,
394
394
ptr : usize ,
395
395
cookie : usize ,
396
396
flags : u32 ,
@@ -406,7 +406,7 @@ impl Process {
406
406
}
407
407
408
408
// Allocate the node before reacquiring the lock.
409
- let node = Arc :: try_new ( Node :: new ( ptr, cookie, flags, Ref :: new_from ( self ) ) ) ?;
409
+ let node = Arc :: try_new ( Node :: new ( ptr, cookie, flags, self . clone ( ) ) ) ?;
410
410
411
411
let mut inner = self . inner . lock ( ) ;
412
412
if let Some ( node) = inner. get_existing_node_ref ( ptr, cookie, strong, thread) ? {
@@ -693,7 +693,11 @@ impl Process {
693
693
ret
694
694
}
695
695
696
- pub ( crate ) fn request_death ( & self , reader : & mut UserSlicePtrReader , thread : & Thread ) -> Result {
696
+ pub ( crate ) fn request_death (
697
+ self : & Ref < Self > ,
698
+ reader : & mut UserSlicePtrReader ,
699
+ thread : & Thread ,
700
+ ) -> Result {
697
701
let handle: u32 = reader. read ( ) ?;
698
702
let cookie: usize = reader. read ( ) ?;
699
703
@@ -716,9 +720,8 @@ impl Process {
716
720
}
717
721
718
722
// SAFETY: `init` is called below.
719
- let death = death. commit ( unsafe {
720
- NodeDeath :: new ( info. node_ref . node . clone ( ) , Ref :: new_from ( self ) , cookie)
721
- } ) ;
723
+ let death = death
724
+ . commit ( unsafe { NodeDeath :: new ( info. node_ref . node . clone ( ) , self . clone ( ) , cookie) } ) ;
722
725
// SAFETY: `death` is pinned behind the `Arc` reference.
723
726
unsafe { Pin :: new_unchecked ( death. as_ref ( ) ) } . init ( ) ;
724
727
info. death = Some ( death. clone ( ) ) ;
@@ -766,9 +769,14 @@ impl Process {
766
769
}
767
770
768
771
impl IoctlHandler for Process {
769
- type Target = Self ;
770
-
771
- fn write ( this : & Self , _file : & File , cmd : u32 , reader : & mut UserSlicePtrReader ) -> Result < i32 > {
772
+ type Target = Ref < Process > ;
773
+
774
+ fn write (
775
+ this : & Ref < Process > ,
776
+ _file : & File ,
777
+ cmd : u32 ,
778
+ reader : & mut UserSlicePtrReader ,
779
+ ) -> Result < i32 > {
772
780
let thread = this. get_thread ( unsafe { rust_helper_current_pid ( ) } ) ?;
773
781
match cmd {
774
782
bindings:: BINDER_SET_MAX_THREADS => this. set_max_threads ( reader. read ( ) ?) ,
@@ -782,7 +790,7 @@ impl IoctlHandler for Process {
782
790
Ok ( 0 )
783
791
}
784
792
785
- fn read_write ( this : & Self , file : & File , cmd : u32 , data : UserSlicePtr ) -> Result < i32 > {
793
+ fn read_write ( this : & Ref < Process > , file : & File , cmd : u32 , data : UserSlicePtr ) -> Result < i32 > {
786
794
let thread = this. get_thread ( unsafe { rust_helper_current_pid ( ) } ) ?;
787
795
match cmd {
788
796
bindings:: BINDER_WRITE_READ => thread. write_read ( data, file. is_blocking ( ) ) ?,
@@ -795,12 +803,6 @@ impl IoctlHandler for Process {
795
803
}
796
804
}
797
805
798
- unsafe impl RefCounted for Process {
799
- fn get_count ( & self ) -> & RefCount {
800
- & self . ref_count
801
- }
802
- }
803
-
804
806
impl FileOpener < Arc < Context > > for Process {
805
807
fn open ( ctx : & Arc < Context > ) -> Result < Self :: Wrapper > {
806
808
let process = Self :: new ( ctx. clone ( ) ) ?;
@@ -893,15 +895,15 @@ impl FileOperations for Process {
893
895
}
894
896
}
895
897
896
- fn ioctl ( this : & Process , file : & File , cmd : & mut IoctlCommand ) -> Result < i32 > {
898
+ fn ioctl ( this : & Ref < Process > , file : & File , cmd : & mut IoctlCommand ) -> Result < i32 > {
897
899
cmd. dispatch :: < Self > ( this, file)
898
900
}
899
901
900
- fn compat_ioctl ( this : & Process , file : & File , cmd : & mut IoctlCommand ) -> Result < i32 > {
902
+ fn compat_ioctl ( this : & Ref < Process > , file : & File , cmd : & mut IoctlCommand ) -> Result < i32 > {
901
903
cmd. dispatch :: < Self > ( this, file)
902
904
}
903
905
904
- fn mmap ( this : & Process , _file : & File , vma : & mut bindings:: vm_area_struct ) -> Result {
906
+ fn mmap ( this : & Ref < Process > , _file : & File , vma : & mut bindings:: vm_area_struct ) -> Result {
905
907
// TODO: Only group leader is allowed to create mappings.
906
908
907
909
if vma. vm_start == 0 {
@@ -919,7 +921,7 @@ impl FileOperations for Process {
919
921
this. create_mapping ( vma)
920
922
}
921
923
922
- fn poll ( this : & Process , file : & File , table : & PollTable ) -> Result < u32 > {
924
+ fn poll ( this : & Ref < Process > , file : & File , table : & PollTable ) -> Result < u32 > {
923
925
let thread = this. get_thread ( unsafe { rust_helper_current_pid ( ) } ) ?;
924
926
let ( from_proc, mut mask) = thread. poll ( file, table) ;
925
927
if mask == 0 && from_proc && !this. inner . lock ( ) . work . is_empty ( ) {
0 commit comments