11#![ no_std]
22#![ no_main]
3- #![ feature( core_intrinsics) ]
43
54#[ macro_use]
65extern crate user_lib;
76extern crate alloc;
87extern crate core;
98
109use alloc:: vec:: Vec ;
11- use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
10+ use core:: { ptr :: { addr_of , addr_of_mut , read_volatile , write_volatile } , sync:: atomic:: { AtomicUsize , Ordering } } ;
1211use user_lib:: { exit, sleep, thread_create, waittid} ;
1312
1413const N : usize = 2 ;
@@ -38,19 +37,19 @@ fn critical_test_exit() {
3837 assert_eq ! ( GUARD . fetch_sub( 1 , Ordering :: SeqCst ) , 1 ) ;
3938}
4039
41- fn eisenberg_enter_critical ( id : usize ) {
40+ unsafe fn eisenberg_enter_critical ( id : usize ) {
4241 /* announce that we want to enter */
4342 loop {
4443 println ! ( "Thread[{}] try enter" , id) ;
45- vstore ! ( & FLAG [ id] , FlagState :: Want ) ;
44+ write_volatile ( addr_of_mut ! ( FLAG [ id] ) , FlagState :: Want ) ;
4645 loop {
4746 /* check if any with higher priority is `Want` or `In` */
4847 let mut prior_thread: Option < usize > = None ;
49- let turn = vload ! ( & TURN ) ;
48+ let turn = read_volatile ( addr_of ! ( TURN ) ) ;
5049 let ring_id = if id < turn { id + THREAD_NUM } else { id } ;
5150 // FLAG.iter() may lead to some errors, use for-loop instead
5251 for i in turn..ring_id {
53- if vload ! ( & FLAG [ i % THREAD_NUM ] ) != FlagState :: Out {
52+ if read_volatile ( addr_of ! ( FLAG [ i % THREAD_NUM ] ) ) != FlagState :: Out {
5453 prior_thread = Some ( i % THREAD_NUM ) ;
5554 break ;
5655 }
@@ -66,13 +65,13 @@ fn eisenberg_enter_critical(id: usize) {
6665 sleep ( 1 ) ;
6766 }
6867 /* now tentatively claim the resource */
69- vstore ! ( & FLAG [ id] , FlagState :: In ) ;
68+ write_volatile ( addr_of_mut ! ( FLAG [ id] ) , FlagState :: In ) ;
7069 /* enforce the order of `claim` and `conflict check`*/
7170 memory_fence ! ( ) ;
7271 /* check if anthor thread is also `In`, which imply a conflict*/
7372 let mut conflict = false ;
7473 for i in 0 ..THREAD_NUM {
75- if i != id && vload ! ( & FLAG [ i] ) == FlagState :: In {
74+ if i != id && read_volatile ( addr_of ! ( FLAG [ i] ) ) == FlagState :: In {
7675 conflict = true ;
7776 }
7877 }
@@ -83,28 +82,28 @@ fn eisenberg_enter_critical(id: usize) {
8382 /* no need to sleep */
8483 }
8584 /* clain the trun */
86- vstore ! ( & TURN , id) ;
85+ write_volatile ( addr_of_mut ! ( TURN ) , id) ;
8786 println ! ( "Thread[{}] enter" , id) ;
8887}
8988
90- fn eisenberg_exit_critical ( id : usize ) {
89+ unsafe fn eisenberg_exit_critical ( id : usize ) {
9190 /* find next one who wants to enter and give the turn to it*/
9291 let mut next = id;
9392 let ring_id = id + THREAD_NUM ;
9493 for i in ( id + 1 ) ..ring_id {
9594 let idx = i % THREAD_NUM ;
96- if vload ! ( & FLAG [ idx] ) == FlagState :: Want {
95+ if read_volatile ( addr_of ! ( FLAG [ idx] ) ) == FlagState :: Want {
9796 next = idx;
9897 break ;
9998 }
10099 }
101- vstore ! ( & TURN , next) ;
100+ write_volatile ( addr_of_mut ! ( TURN ) , next) ;
102101 /* All done */
103- vstore ! ( & FLAG [ id] , FlagState :: Out ) ;
102+ write_volatile ( addr_of_mut ! ( FLAG [ id] ) , FlagState :: Out ) ;
104103 println ! ( "Thread[{}] exit, give turn to {}" , id, next) ;
105104}
106105
107- pub fn thread_fn ( id : usize ) -> ! {
106+ pub unsafe fn thread_fn ( id : usize ) -> ! {
108107 println ! ( "Thread[{}] init." , id) ;
109108 for _ in 0 ..N {
110109 eisenberg_enter_critical ( id) ;
0 commit comments