diff --git a/osdep/LinuxEthernetTap.cpp b/osdep/LinuxEthernetTap.cpp index d75fcdf97..f36147140 100644 --- a/osdep/LinuxEthernetTap.cpp +++ b/osdep/LinuxEthernetTap.cpp @@ -92,7 +92,8 @@ LinuxEthernetTap::LinuxEthernetTap( _homePath(homePath), _mtu(mtu), _fd(0), - _enabled(true) + _enabled(true), + _run(true) { static std::mutex s_tapCreateLock; char procpath[128],nwids[32]; @@ -184,6 +185,7 @@ LinuxEthernetTap::LinuxEthernetTap( (void)::pipe(_shutdownSignalPipe); + _thread_init_l.lock(); for(unsigned int t=0;t<2;++t) { _tapReaderThread[t] = std::thread([this, t]{ fd_set readfds,nullfds; @@ -236,10 +238,16 @@ LinuxEthernetTap::LinuxEthernetTap( fcntl(_fd,F_SETFL,O_NONBLOCK); ::close(sock); + + _thread.init_l.unlock(); } else { - usleep(1500000); + _thread_init_l.lock(); + _thread_init_l.unlock(); } + if (!_run) + return; + FD_ZERO(&readfds); FD_ZERO(&nullfds); nfds = (int)std::max(_shutdownSignalPipe[0],_fd) + 1; @@ -324,6 +332,8 @@ LinuxEthernetTap::LinuxEthernetTap( LinuxEthernetTap::~LinuxEthernetTap() { + _run = false; + (void)::write(_shutdownSignalPipe[1],"\0",1); // causes reader thread(s) to exit _tapq.post(std::pair(nullptr,0)); // causes processor thread to exit diff --git a/osdep/LinuxEthernetTap.hpp b/osdep/LinuxEthernetTap.hpp index a21a53a2c..e9696fa60 100644 --- a/osdep/LinuxEthernetTap.hpp +++ b/osdep/LinuxEthernetTap.hpp @@ -70,9 +70,11 @@ private: int _fd; int _shutdownSignalPipe[2]; std::atomic_bool _enabled; + std::atomic_bool _run; std::thread _tapReaderThread[2]; std::thread _tapProcessorThread; std::mutex _buffers_l; + std::mutex _thread_init_l; std::vector _buffers; BlockingQueue< std::pair > _tapq; };