Skip to content

Commit 6a0dbf4

Browse files
committed
Don't use pthread API, kill(pid, SIGIO) instead and use ngx_thread_tid for oet
1 parent 35f97ff commit 6a0dbf4

File tree

3 files changed

+16
-24
lines changed

3 files changed

+16
-24
lines changed

examples/async.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use futures_util::FutureExt;
44
use http_body_util::Empty;
55
use hyper::body::Bytes;
66
use hyper_util::rt::TokioIo;
7-
use nginx_sys::{ngx_cycle_t, ngx_http_core_loc_conf_t, NGX_LOG_ERR};
7+
use nginx_sys::{ngx_http_core_loc_conf_t, NGX_LOG_ERR};
88
use ngx::async_::resolver::Resolver;
9-
use ngx::async_::{initialize_async, spawn, Task};
9+
use ngx::async_::{spawn, Task};
1010
use std::cell::RefCell;
1111
use std::ffi::{c_char, c_void};
1212
use std::future::Future;
@@ -98,16 +98,9 @@ pub static mut ngx_http_async_module: ngx_module_t = ngx_module_t {
9898
ctx: std::ptr::addr_of!(NGX_HTTP_ASYNC_MODULE_CTX) as _,
9999
commands: unsafe { &NGX_HTTP_ASYNC_COMMANDS[0] as *const _ as *mut _ },
100100
type_: NGX_HTTP_MODULE as _,
101-
init_process: Some(init_process),
102101
..ngx_module_t::default()
103102
};
104103

105-
extern "C" fn init_process(_cycle: *mut ngx_cycle_t) -> ngx_int_t {
106-
initialize_async();
107-
108-
Status::NGX_OK.into()
109-
}
110-
111104
impl http::Merge for ModuleConfig {
112105
fn merge(&mut self, prev: &ModuleConfig) -> Result<(), MergeConfigError> {
113106
if prev.enable {

src/async_/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Async runtime and set of utilities on top of the NGINX event loop.
22
pub use self::sleep::{sleep, Sleep};
3-
pub use self::spawn::{initialize_async, spawn, Task};
3+
pub use self::spawn::{spawn, Task};
44

55
pub mod resolver;
66

src/async_/spawn.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extern crate std;
22

33
use core::ffi::c_int;
4+
use core::sync::atomic::{AtomicI64, Ordering};
45
use core::{mem, ptr};
56
use std::sync::OnceLock;
67

@@ -10,29 +11,27 @@ pub use async_task::Task;
1011
use async_task::{Runnable, ScheduleInfo, WithInfo};
1112
use crossbeam_channel::{unbounded, Receiver, Sender};
1213
use 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

1718
use crate::log::ngx_cycle_log;
1819
use crate::ngx_log_debug;
1920
use 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]
3125
fn 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

3531
extern "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) {
4847
fn 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

Comments
 (0)