Skip to content

Commit 404b63e

Browse files
committed
feat(sys): reimplement ngx_event_t macros and inline methods
Bindgen does not support functional macros or inline functions, so we have to maintain a copy of the implementation for any such item we need to use.
1 parent 467d04d commit 404b63e

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

nginx-sys/src/event.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use core::ptr;
2+
3+
use crate::{
4+
ngx_current_msec, ngx_event_t, ngx_event_timer_rbtree, ngx_msec_t, ngx_queue_insert_before, ngx_queue_remove,
5+
ngx_queue_t, ngx_rbtree_delete, ngx_rbtree_insert, NGX_TIMER_LAZY_DELAY,
6+
};
7+
8+
/// Sets a timeout for an event.
9+
///
10+
/// # Safety
11+
///
12+
///`ev` must be a valid pointer to an `ngx_event_t`.
13+
#[inline]
14+
pub unsafe fn ngx_add_timer(ev: *mut ngx_event_t, timer: ngx_msec_t) {
15+
let key: ngx_msec_t = ngx_current_msec.wrapping_add(timer);
16+
17+
if (*ev).timer_set() != 0 {
18+
/*
19+
* Use a previous timer value if difference between it and a new
20+
* value is less than NGX_TIMER_LAZY_DELAY milliseconds: this allows
21+
* to minimize the rbtree operations for fast connections.
22+
*/
23+
if key.abs_diff((*ev).timer.key) < NGX_TIMER_LAZY_DELAY as _ {
24+
return;
25+
}
26+
27+
ngx_del_timer(ev);
28+
}
29+
30+
(*ev).timer.key = key;
31+
32+
ngx_rbtree_insert(
33+
ptr::addr_of_mut!(ngx_event_timer_rbtree),
34+
ptr::addr_of_mut!((*ev).timer),
35+
);
36+
37+
(*ev).set_timer_set(1);
38+
}
39+
40+
/// Deletes a previously set timeout.
41+
///
42+
/// # Safety
43+
///
44+
/// `ev` must be a valid pointer to an `ngx_event_t`, previously armed with [ngx_add_timer].
45+
#[inline]
46+
pub unsafe fn ngx_del_timer(ev: *mut ngx_event_t) {
47+
ngx_rbtree_delete(
48+
ptr::addr_of_mut!(ngx_event_timer_rbtree),
49+
ptr::addr_of_mut!((*ev).timer),
50+
);
51+
52+
(*ev).timer.left = ptr::null_mut();
53+
(*ev).timer.right = ptr::null_mut();
54+
(*ev).timer.parent = ptr::null_mut();
55+
56+
(*ev).set_timer_set(0);
57+
}
58+
59+
/// Post the event `ev` to the post queue `q`.
60+
///
61+
/// # Safety
62+
///
63+
/// `ev` must be a valid pointer to an `ngx_event_t`.
64+
/// `q` is a valid pointer to a queue head.
65+
#[inline]
66+
pub unsafe fn ngx_post_event(ev: *mut ngx_event_t, q: *mut ngx_queue_t) {
67+
if (*ev).posted() == 0 {
68+
(*ev).set_posted(1);
69+
ngx_queue_insert_before(q, ptr::addr_of_mut!((*ev).queue));
70+
}
71+
}
72+
73+
/// Deletes the event `ev` from the queue it's currently posted in.
74+
///
75+
/// # Safety
76+
///
77+
/// `ev` must be a valid pointer to an `ngx_event_t`.
78+
/// `ev.queue` is initialized with `ngx_queue_init`.
79+
#[inline]
80+
pub unsafe fn ngx_delete_posted_event(ev: *mut ngx_event_t) {
81+
(*ev).set_posted(0);
82+
ngx_queue_remove(ptr::addr_of_mut!((*ev).queue));
83+
}

nginx-sys/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![warn(missing_docs)]
33
#![no_std]
44

5+
mod event;
56
mod queue;
67

78
use core::fmt;
@@ -24,6 +25,7 @@ mod bindings {
2425
#[doc(no_inline)]
2526
pub use bindings::*;
2627

28+
pub use event::*;
2729
pub use queue::*;
2830

2931
/// The offset of the `main_conf` field in the `ngx_http_conf_ctx_t` struct.

0 commit comments

Comments
 (0)