55#![ no_std]
66#![ feature( allocator_api, global_asm) ]
77
8- use alloc:: { boxed:: Box , sync :: Arc } ;
8+ use alloc:: boxed:: Box ;
99use core:: pin:: Pin ;
1010use kernel:: prelude:: * ;
1111use kernel:: {
@@ -14,7 +14,7 @@ use kernel::{
1414 file_operations:: { FileOpener , FileOperations } ,
1515 io_buffer:: { IoBufferReader , IoBufferWriter } ,
1616 miscdev,
17- sync:: { CondVar , Mutex } ,
17+ sync:: { CondVar , Mutex , Ref } ,
1818 Error ,
1919} ;
2020
@@ -38,38 +38,41 @@ struct SharedState {
3838}
3939
4040impl SharedState {
41- fn try_new ( ) -> Result < Pin < Arc < Self > > > {
42- let state = Arc :: try_pin ( Self {
43- // SAFETY: `condvar_init!` is called below.
44- state_changed : unsafe { CondVar :: new ( ) } ,
45- // SAFETY: `mutex_init!` is called below.
46- inner : unsafe { Mutex :: new ( SharedStateInner { token_count : 0 } ) } ,
47- } ) ?;
48- // SAFETY: `state_changed` is pinned behind `Pin<Arc>`.
49- let state_changed = unsafe { Pin :: new_unchecked ( & state. state_changed ) } ;
50- kernel:: condvar_init!( state_changed, "SharedState::state_changed" ) ;
51- // SAFETY: `inner` is pinned behind `Pin<Arc>`.
52- let inner = unsafe { Pin :: new_unchecked ( & state. inner ) } ;
53- kernel:: mutex_init!( inner, "SharedState::inner" ) ;
54- Ok ( state)
41+ fn try_new ( ) -> Result < Pin < Ref < Self > > > {
42+ Ok ( Ref :: pinned ( Ref :: try_new_and_init (
43+ Self {
44+ // SAFETY: `condvar_init!` is called below.
45+ state_changed : unsafe { CondVar :: new ( ) } ,
46+ // SAFETY: `mutex_init!` is called below.
47+ inner : unsafe { Mutex :: new ( SharedStateInner { token_count : 0 } ) } ,
48+ } ,
49+ |state| {
50+ // SAFETY: `state_changed` is pinned when `state` is.
51+ let state_changed = unsafe { Pin :: new_unchecked ( & state. state_changed ) } ;
52+ kernel:: condvar_init!( state_changed, "SharedState::state_changed" ) ;
53+ // SAFETY: `inner` is pinned when `state` is.
54+ let inner = unsafe { Pin :: new_unchecked ( & state. inner ) } ;
55+ kernel:: mutex_init!( inner, "SharedState::inner" ) ;
56+ } ,
57+ ) ?) )
5558 }
5659}
5760
5861struct Token ;
5962
60- impl FileOpener < Pin < Arc < SharedState > > > for Token {
61- fn open ( shared : & Pin < Arc < SharedState > > ) -> Result < Self :: Wrapper > {
63+ impl FileOpener < Pin < Ref < SharedState > > > for Token {
64+ fn open ( shared : & Pin < Ref < SharedState > > ) -> Result < Self :: Wrapper > {
6265 Ok ( shared. clone ( ) )
6366 }
6467}
6568
6669impl FileOperations for Token {
67- type Wrapper = Pin < Arc < SharedState > > ;
70+ type Wrapper = Pin < Ref < SharedState > > ;
6871
6972 kernel:: declare_file_operations!( read, write) ;
7073
7174 fn read < T : IoBufferWriter > (
72- shared : & SharedState ,
75+ shared : & Ref < SharedState > ,
7376 _: & File ,
7477 data : & mut T ,
7578 offset : u64 ,
@@ -102,10 +105,10 @@ impl FileOperations for Token {
102105 }
103106
104107 fn write < T : IoBufferReader > (
105- shared : & SharedState ,
108+ shared : & Ref < SharedState > ,
106109 _: & File ,
107110 data : & mut T ,
108- _offs : u64 ,
111+ _offset : u64 ,
109112 ) -> Result < usize > {
110113 {
111114 let mut inner = shared. inner . lock ( ) ;
@@ -128,7 +131,7 @@ impl FileOperations for Token {
128131}
129132
130133struct RustMiscdev {
131- _dev : Pin < Box < miscdev:: Registration < Pin < Arc < SharedState > > > > > ,
134+ _dev : Pin < Box < miscdev:: Registration < Pin < Ref < SharedState > > > > > ,
132135}
133136
134137impl KernelModule for RustMiscdev {
0 commit comments