@@ -2,24 +2,31 @@ use crate::base::*;
22use crate :: prelude:: v1:: String ;
33use crate :: utils:: * ;
44
5+ use core:: cell:: OnceCell ;
6+
57type Callback = fn ( ) ;
68
79pub struct FreeRtosHooks {
8- on_assert : Callback ,
10+ on_assert : OnceCell < Callback > ,
911}
1012
1113impl FreeRtosHooks {
12- pub fn set_on_assert ( & mut self , c : Callback ) {
13- self . on_assert = c ;
14+ pub fn set_on_assert ( & mut self , c : Callback ) -> Result < ( ) , Callback > {
15+ self . on_assert . set ( c )
1416 }
1517
1618 fn do_on_assert ( & self ) {
17- ( self . on_assert ) ( ) ;
19+ if let Some ( cb) = self . on_assert . get ( ) {
20+ cb ( )
21+ }
1822 }
1923}
2024
21- // TODO: It's unsafe to use, we should build some safe wrapper around
22- pub static mut FREERTOS_HOOKS : FreeRtosHooks = FreeRtosHooks { on_assert : || { } } ;
25+ // SAFETY: must only be set before the scheduler starts and accessed after the
26+ // kernel has asserted, both being single threaded situations.
27+ unsafe impl Sync for FreeRtosHooks { }
28+
29+ pub static FREERTOS_HOOKS : FreeRtosHooks = FreeRtosHooks { on_assert : OnceCell :: new ( ) } ;
2330
2431#[ allow( unused_doc_comments) ]
2532#[ no_mangle]
@@ -29,9 +36,7 @@ pub extern "C" fn vAssertCalled(file_name_ptr: FreeRtosCharPtr, line: FreeRtosUB
2936 file_name = str_from_c_string ( file_name_ptr) . unwrap ( ) ;
3037 }
3138
32- unsafe {
33- FREERTOS_HOOKS . do_on_assert ( ) ;
34- }
39+ FREERTOS_HOOKS . do_on_assert ( ) ;
3540
3641 // we can't print without std yet.
3742 // TODO: make the macro work for debug UART? Or use Panic here?
0 commit comments