Skip to content

Commit 69356ec

Browse files
authored
Merge pull request #131 from larsewi/document-lch-log-init
Document lch_log_init thread-safety and re-entrance contract
2 parents a5c0406 + 0d13b3e commit 69356ec

3 files changed

Lines changed: 50 additions & 11 deletions

File tree

include/leech2.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,22 @@ typedef void (*lch_log_callback_t)(lch_log_level_t level, const char *msg,
4848
* Initialize logging with a callback.
4949
*
5050
* Installs a custom logger that delivers all log messages through @p callback.
51-
* Must be called before lch_init() for the callback to receive messages.
52-
* May be called again to replace the callback.
51+
* Must be called before lch_init() for the callback to receive messages from
52+
* initialization.
53+
*
54+
* May be called again to atomically replace @p callback and @p usr_data. After
55+
* a replacement, the previous callback is no longer invoked; the library does
56+
* not free the previous @p usr_data, so the caller is responsible for its
57+
* lifetime.
58+
*
59+
* Safe to call concurrently from multiple threads. Once installed, @p callback
60+
* itself may be invoked from any thread, possibly in parallel, so both
61+
* @p callback and @p usr_data must be thread-safe.
5362
*
5463
* @param callback Function to receive log messages (must not be NULL).
55-
* @param usr_data Opaque pointer forwarded to every callback invocation.
64+
* @param usr_data Opaque pointer forwarded to every callback invocation. Must
65+
* remain valid until the callback is replaced by a later
66+
* lch_log_init() call or the process exits.
5667
* @return LCH_SUCCESS on success, LCH_FAILURE on error.
5768
*/
5869
extern int lch_log_init(lch_log_callback_t callback, void *usr_data);

man/libleech2.3.in

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,33 @@ Install a custom logger that delivers all log messages through
4747
.IR callback .
4848
Must be called before
4949
.BR lch_init ()
50-
for the callback to receive messages from initialization. May be called again to
51-
replace the callback. Returns
50+
for the callback to receive messages from initialization. May be called again
51+
to atomically replace
52+
.I callback
53+
and
54+
.IR usr_data ;
55+
after a replacement the previous callback is no longer invoked, and the library
56+
does not free the previous
57+
.IR usr_data ,
58+
so the caller owns its lifetime. Safe to call concurrently from multiple
59+
threads. Once installed,
60+
.I callback
61+
itself may be invoked from any thread, possibly in parallel, so both
62+
.I callback
63+
and
64+
.I usr_data
65+
must be thread-safe.
66+
.I callback
67+
must not be NULL.
68+
.I usr_data
69+
is an opaque pointer forwarded to every callback invocation and must remain
70+
valid until the callback is replaced by a later
71+
.BR lch_log_init ()
72+
call or the process exits. Returns
5273
.B LCH_SUCCESS
5374
on success and
5475
.B LCH_FAILURE
5576
on error.
56-
.I callback
57-
must not be NULL.
58-
.I usr_data
59-
is an opaque pointer forwarded to every callback invocation.
6077
.SS Lifecycle
6178
.TP
6279
.BI "lch_config_t *lch_init(const char *" work_dir )

src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,21 @@ fn ffi_guard<T>(name: &str, default: T, body: impl FnOnce() -> T) -> T {
3636
}
3737
}
3838

39+
/// Install or replace the log callback.
40+
///
41+
/// The first call installs the global logger; subsequent calls atomically swap
42+
/// the callback and `user_data`. After a swap, the old callback is no longer
43+
/// invoked, but the library does not free or otherwise touch the previous
44+
/// `user_data` — the caller owns its lifetime and must release it if needed.
45+
///
46+
/// Safe to call concurrently from multiple threads. Once installed, the
47+
/// callback itself may be invoked from any thread (including in parallel),
48+
/// so both `callback` and `user_data` must be thread-safe.
49+
///
3950
/// # Safety
4051
/// `callback` must be a valid function pointer; passing NULL returns `LCH_FAILURE`.
41-
/// `user_data` must be valid for the lifetime of the callback and safe to
42-
/// access from any thread.
52+
/// `user_data` must remain valid until either the callback is replaced by a
53+
/// later `lch_log_init` call or the process exits.
4354
#[unsafe(no_mangle)]
4455
pub unsafe extern "C" fn lch_log_init(
4556
callback: Option<unsafe extern "C" fn(i32, *const c_char, *mut c_void)>,

0 commit comments

Comments
 (0)