11extern crate std;
22
33use core:: ffi:: c_int;
4+ use core:: sync:: atomic:: { AtomicI64 , Ordering } ;
45use core:: { mem, ptr} ;
56use std:: sync:: OnceLock ;
67
@@ -10,29 +11,27 @@ pub use async_task::Task;
1011use async_task:: { Runnable , ScheduleInfo , WithInfo } ;
1112use crossbeam_channel:: { unbounded, Receiver , Sender } ;
1213use nginx_sys:: {
13- ngx_del_timer, ngx_delete_posted_event, ngx_event_t, ngx_post_event, ngx_posted_events,
14- pthread_kill , pthread_self , pthread_t , SIGIO ,
14+ kill , ngx_del_timer, ngx_delete_posted_event, ngx_event_t, ngx_post_event, ngx_posted_events,
15+ ngx_thread_tid , SIGIO ,
1516} ;
1617
1718use crate :: log:: ngx_cycle_log;
1819use crate :: ngx_log_debug;
1920use crate :: sync:: RwLock ;
2021
21- static MAIN_THREAD : OnceLock < pthread_t > = OnceLock :: new ( ) ;
22-
23- /// Initialize async by storing MAIN_THREAD
24- pub fn initialize_async ( ) {
25- MAIN_THREAD
26- . set ( unsafe { pthread_self ( ) } )
27- . expect ( "async: double initialize" )
28- }
22+ static MAIN_TID : AtomicI64 = AtomicI64 :: new ( -1 ) ;
2923
3024#[ inline]
3125fn on_event_thread ( ) -> bool {
32- * MAIN_THREAD . get ( ) . expect ( "async: not initialized" ) == unsafe { pthread_self ( ) }
26+ let main_tid = MAIN_TID . load ( Ordering :: Relaxed ) ;
27+ let tid: i64 = unsafe { ngx_thread_tid ( ) . into ( ) } ;
28+ main_tid == tid
3329}
3430
3531extern "C" fn async_handler ( ev : * mut ngx_event_t ) {
32+ // initialize MAIN_TID on first execution
33+ let tid = unsafe { ngx_thread_tid ( ) . into ( ) } ;
34+ let _ = MAIN_TID . compare_exchange ( -1 , tid, Ordering :: Relaxed , Ordering :: Relaxed ) ;
3635 let scheduler = scheduler ( ) ;
3736 let mut cnt = 0 ;
3837 while let Ok ( r) = scheduler. rx . try_recv ( ) {
@@ -48,8 +47,8 @@ extern "C" fn async_handler(ev: *mut ngx_event_t) {
4847fn notify ( ) -> c_int {
4948 ngx_log_debug ! ( ngx_cycle_log( ) . as_ptr( ) , "async: ngx_notify" ) ;
5049 unsafe {
51- pthread_kill (
52- * MAIN_THREAD . get ( ) . expect ( "async: not initialized" ) ,
50+ kill (
51+ std :: process :: id ( ) . try_into ( ) . unwrap ( ) ,
5352 SIGIO . try_into ( ) . unwrap ( ) ,
5453 )
5554 }
@@ -100,7 +99,7 @@ impl Scheduler {
10099 if !oet {
101100 let rc = notify ( ) ;
102101 if rc != 0 {
103- panic ! ( "pthread_kill : {rc}" )
102+ panic ! ( "kill : {rc}" )
104103 }
105104 }
106105 }
0 commit comments