Skip to content

Commit b2b16c6

Browse files
committed
doc: document rust thread life cycle api
Ticket: OISF#8605
1 parent bb9d255 commit b2b16c6

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

doc/userguide/devguide/extending/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ Extending Suricata
1212
output/index.rst
1313
output/eve-filetypes.rst
1414
output/eve-hooks.rst
15+
threads.rst
1516
flow-lifecycle-callbacks.rst
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Threads
2+
#######
3+
4+
Rust API
5+
********
6+
7+
The ``suricata_ffi::thread`` module provides Rust wrappers for thread
8+
lifecycle callbacks.
9+
10+
Thread Init Callback
11+
====================
12+
13+
Register a callback with ``thread::register_init_callback`` to run code for
14+
each Suricata thread as it is initialized. The callback receives a raw
15+
``ThreadVars`` pointer for the thread that has just been initialized.
16+
17+
The current Rust thread lifecycle API exposes an init callback only; there is
18+
no Rust thread deinit callback.
19+
20+
.. code-block:: rust
21+
22+
use suricata_ffi::thread;
23+
use suricata_ffi::SCLogNotice;
24+
use suricata_sys::sys::ThreadVars;
25+
26+
fn on_thread_init(tv: *mut ThreadVars) {
27+
SCLogNotice!("thread initialized: {:p}", tv);
28+
}
29+
30+
fn register_thread_callbacks() -> Result<(), &'static str> {
31+
thread::register_init_callback(on_thread_init)
32+
}
33+
34+
The wrapper accepts function items or closures that implement
35+
``Fn(*mut ThreadVars) + Send + Sync + 'static`` and returns
36+
``Result<(), &'static str>``. An error means the callback could not be
37+
registered. Registered callbacks are kept for the Suricata process lifetime.
38+
39+
The ``ThreadVars`` pointer is only valid for the duration of the callback
40+
invocation and must not be stored. Rust callbacks must not panic.

0 commit comments

Comments
 (0)