@@ -4,6 +4,7 @@ use log::*;
44use pcap_sys:: { pcap_fileno, pcap_set_immediate_mode} ;
55use std:: os:: raw:: c_int;
66use std:: path:: Path ;
7+ use std:: sync:: atomic:: { AtomicBool , Ordering } ;
78
89fn compile_bpf ( handle : * mut pcap_sys:: pcap_t , bpf : & str ) -> Result < Bpf , Error > {
910 let mut bpf_program = pcap_sys:: bpf_program {
@@ -187,7 +188,7 @@ impl PendingHandle {
187188 let h = Handle {
188189 handle : self . handle ,
189190 live_capture : self . live_capture ,
190- interrupted : std :: sync :: Arc :: new ( std :: sync :: Mutex :: new ( false ) ) ,
191+ interrupted : AtomicBool :: new ( false ) ,
191192 } ;
192193 if self . live_capture {
193194 if 0 != unsafe { pcap_sys:: pcap_activate ( h. handle ) } {
@@ -226,7 +227,7 @@ impl std::convert::TryFrom<&Config> for PendingHandle {
226227pub struct Handle {
227228 handle : * mut pcap_sys:: pcap_t ,
228229 live_capture : bool ,
229- interrupted : std :: sync :: Arc < std :: sync :: Mutex < bool > > ,
230+ interrupted : AtomicBool ,
230231}
231232
232233unsafe impl Send for Handle { }
@@ -258,28 +259,19 @@ impl Handle {
258259 }
259260
260261 pub fn interrupted ( & self ) -> bool {
261- self . interrupted . lock ( ) . map ( |l| * l ) . unwrap_or ( true )
262+ self . interrupted . load ( Ordering :: Relaxed )
262263 }
263264
264265 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 ) ;
273267 if !interrupted {
274268 unsafe {
275269 pcap_sys:: pcap_breakloop ( self . handle ) ;
276270 }
277271 }
278272 }
279273
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 > {
283275 let ret_code = unsafe { pcap_sys:: pcap_setfilter ( self . handle , bpf. inner_mut ( ) ) } ;
284276 if ret_code != 0 {
285277 return Err ( pcap_util:: convert_libpcap_error ( self . handle ) ) ;
0 commit comments