Kaynağa Gözat

Another Linux fix.

Adam Ierymenko 4 yıl önce
ebeveyn
işleme
954f6a77e5
2 değiştirilmiş dosya ile 14 ekleme ve 2 silme
  1. 12 2
      osdep/LinuxEthernetTap.cpp
  2. 2 0
      osdep/LinuxEthernetTap.hpp

+ 12 - 2
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<void *,int>(nullptr,0)); // causes processor thread to exit
 

+ 2 - 0
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<void *> _buffers;
 	BlockingQueue< std::pair<void *,int> > _tapq;
 };