Skip to content

Commit caddfc3

Browse files
committed
feat(sys): reimplement ngx_time/ngx_timeofday
1 parent 2de2d86 commit caddfc3

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

nginx-sys/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,26 @@ pub fn ngx_random() -> core::ffi::c_long {
163163
}
164164
}
165165

166+
/// Returns cached timestamp in seconds, updated at the start of the event loop iteration.
167+
///
168+
/// Can be stale when accessing from threads, see [ngx_time_update].
169+
#[inline]
170+
pub fn ngx_time() -> time_t {
171+
// SAFETY: ngx_cached_time is initialized before any module code can run
172+
unsafe { (*ngx_cached_time).sec }
173+
}
174+
175+
/// Returns cached time, updated at the start of the event loop iteration.
176+
///
177+
/// Can be stale when accessing from threads, see [ngx_time_update].
178+
/// A cached reference to the ngx_timeofday() result is guaranteed to remain unmodified for the next
179+
/// NGX_TIME_SLOTS seconds.
180+
#[inline]
181+
pub fn ngx_timeofday() -> &'static ngx_time_t {
182+
// SAFETY: ngx_cached_time is initialized before any module code can run
183+
unsafe { &*ngx_cached_time }
184+
}
185+
166186
/// Add a key-value pair to an nginx table entry (`ngx_table_elt_t`) in the given nginx memory pool.
167187
///
168188
/// # Arguments

0 commit comments

Comments
 (0)