@@ -4,6 +4,7 @@ use log::*;
4
4
use pcap_sys:: { pcap_fileno, pcap_set_immediate_mode} ;
5
5
use std:: os:: raw:: c_int;
6
6
use std:: path:: Path ;
7
+ use std:: sync:: atomic:: { AtomicBool , Ordering } ;
7
8
8
9
fn compile_bpf ( handle : * mut pcap_sys:: pcap_t , bpf : & str ) -> Result < Bpf , Error > {
9
10
let mut bpf_program = pcap_sys:: bpf_program {
@@ -187,7 +188,7 @@ impl PendingHandle {
187
188
let h = Handle {
188
189
handle : self . handle ,
189
190
live_capture : self . live_capture ,
190
- interrupted : std :: sync :: Arc :: new ( std :: sync :: Mutex :: new ( false ) ) ,
191
+ interrupted : AtomicBool :: new ( false ) ,
191
192
} ;
192
193
if self . live_capture {
193
194
if 0 != unsafe { pcap_sys:: pcap_activate ( h. handle ) } {
@@ -226,7 +227,7 @@ impl std::convert::TryFrom<&Config> for PendingHandle {
226
227
pub struct Handle {
227
228
handle : * mut pcap_sys:: pcap_t ,
228
229
live_capture : bool ,
229
- interrupted : std :: sync :: Arc < std :: sync :: Mutex < bool > > ,
230
+ interrupted : AtomicBool ,
230
231
}
231
232
232
233
unsafe impl Send for Handle { }
@@ -258,28 +259,19 @@ impl Handle {
258
259
}
259
260
260
261
pub fn interrupted ( & self ) -> bool {
261
- self . interrupted . lock ( ) . map ( |l| * l ) . unwrap_or ( true )
262
+ self . interrupted . load ( Ordering :: Relaxed )
262
263
}
263
264
264
265
pub fn interrupt ( & self ) {
265
- let interrupted = self
266
- . interrupted
267
- . lock ( )
268
- . map ( |mut l| {
269
- * l = true ;
270
- false
271
- } )
272
- . unwrap_or ( true ) ;
266
+ let interrupted = self . interrupted . swap ( true , Ordering :: Relaxed ) ;
273
267
if !interrupted {
274
268
unsafe {
275
269
pcap_sys:: pcap_breakloop ( self . handle ) ;
276
270
}
277
271
}
278
272
}
279
273
280
- pub fn set_bpf ( self , bpf : Bpf ) -> Result < Self , Error > {
281
- let mut bpf = bpf;
282
-
274
+ pub fn set_bpf ( self , mut bpf : Bpf ) -> Result < Self , Error > {
283
275
let ret_code = unsafe { pcap_sys:: pcap_setfilter ( self . handle , bpf. inner_mut ( ) ) } ;
284
276
if ret_code != 0 {
285
277
return Err ( pcap_util:: convert_libpcap_error ( self . handle ) ) ;
0 commit comments