Skip to content

Commit 20da2c1

Browse files
committed
Add safety docs for BreakLoop::breakloop
1 parent 31dfb04 commit 20da2c1

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/capture/activated/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,21 @@ pub struct BreakLoop {
364364
handle: Weak<Handle>,
365365
}
366366

367+
unsafe impl Send for BreakLoop {}
368+
unsafe impl Sync for BreakLoop {}
369+
367370
impl BreakLoop {
368371
/// Calls `pcap_breakloop` to make the blocking loop of a pcap capture return.
369372
/// The call is a no-op if the handle is invalid.
373+
///
374+
/// # Safety
375+
///
376+
/// Can be called from any thread, but **must not** be used inside a
377+
/// signal handler unless the owning `Capture` is guaranteed to still
378+
/// be alive.
379+
///
380+
/// The signal handler should defer the execution of `BreakLoop::breakloop()`
381+
/// to a thread instead for safety.
370382
pub fn breakloop(&self) {
371383
if let Some(handle) = self.handle.upgrade() {
372384
unsafe { raw::pcap_breakloop(handle.as_ptr()) };

src/capture/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,17 @@ impl Handle {
105105
}
106106
}
107107

108+
// `PcapHandle` is safe to Send as it encapsulates the entire lifetime of `raw::pcap_t *`, but it is
109+
// not safe to Sync as libpcap does not promise thread-safe access to the same `raw::pcap_t *` from
110+
// multiple threads.
108111
unsafe impl Send for Handle {}
109-
unsafe impl Sync for Handle {}
110112

111113
impl Drop for Handle {
112114
fn drop(&mut self) {
113115
unsafe { raw::pcap_close(self.handle.as_ptr()) }
114116
}
115117
}
116118

117-
// A Capture is safe to Send as it encapsulates the entire lifetime of `raw::pcap_t *`, but it is
118-
// not safe to Sync as libpcap does not promise thread-safe access to the same `raw::pcap_t *` from
119-
// multiple threads.
120119
unsafe impl<T: State + ?Sized> Send for Capture<T> {}
121120

122121
impl<T: State + ?Sized> From<NonNull<raw::pcap_t>> for Capture<T> {

0 commit comments

Comments
 (0)