Ver código fonte

Testnet seems to work a bit better now...

Adam Ierymenko 10 anos atrás
pai
commit
934a575a74
5 arquivos alterados com 37 adições e 38 exclusões
  1. 0 1
      node/Node.cpp
  2. 4 4
      testnet.cpp
  3. 29 29
      testnet/Semaphore.hpp
  4. 2 2
      testnet/SimNetSocketManager.hpp
  5. 2 2
      testnet/TestEthernetTap.hpp

+ 0 - 1
node/Node.cpp

@@ -100,7 +100,6 @@ struct _NodeImpl
 		LOG("terminating: %s",reasonForTerminationStr.c_str());
 
 		renv.shutdownInProgress = true;
-		Thread::sleep(500);
 
 		running = false;
 

+ 4 - 4
testnet.cpp

@@ -510,7 +510,7 @@ static void doUnicast(const std::vector<std::string> &cmd)
 	for(unsigned int i=0;i<frameLen;++i)
 		pkt.data[i] = (unsigned char)prng.next32();
 
-	unsigned long sentCount = 0;
+	std::set< std::pair<Address,Address> > sentPairs;
 	for(std::vector<Address>::iterator s(senders.begin());s!=senders.end();++s) {
 		for(std::vector<Address>::iterator r(receivers.begin());r!=receivers.end();++r) {
 			if (*s == *r)
@@ -526,7 +526,7 @@ static void doUnicast(const std::vector<std::string> &cmd)
 				pkt.i[1] = Utils::now();
 				stap->injectPacketFromHost(stap->mac(),rtap->mac(),0xdead,pkt.data,frameLen);
 				printf("%s -> %s etherType 0xdead network %.16llx length %u"ZT_EOL_S,s->toString().c_str(),r->toString().c_str(),nwid,frameLen);
-				++sentCount;
+				sentPairs.insert(std::pair<Address,Address>(*s,*r));
 			} else if (stap) {
 				printf("%s -> !%s (receiver not a member of %.16llx)"ZT_EOL_S,s->toString().c_str(),r->toString().c_str(),nwid);
 			} else if (rtap) {
@@ -561,13 +561,13 @@ static void doUnicast(const std::vector<std::string> &cmd)
 			}
 		}
 		Thread::sleep(250);
-	} while ((receivedPairs.size() < sentCount)&&(Utils::now() < toutend));
+	} while ((receivedPairs.size() < sentPairs.size())&&(Utils::now() < toutend));
 
 	for(std::vector<Address>::iterator s(senders.begin());s!=senders.end();++s) {
 		for(std::vector<Address>::iterator r(receivers.begin());r!=receivers.end();++r) {
 			if (*s == *r)
 				continue;
-			if (!receivedPairs.count(std::pair<Address,Address>(*s,*r))) {
+			if ((sentPairs.count(std::pair<Address,Address>(*s,*r)))&&(!receivedPairs.count(std::pair<Address,Address>(*s,*r)))) {
 				printf("%s <- %s was never received (timed out)"ZT_EOL_S,r->toString().c_str(),s->toString().c_str());
 			}
 		}

+ 29 - 29
testnet/Condition.hpp → testnet/Semaphore.hpp

@@ -25,8 +25,8 @@
  * LLC. Start here: http://www.zerotier.com/
  */
 
-#ifndef ZT_CONDITION_HPP
-#define ZT_CONDITION_HPP
+#ifndef ZT_SEMAPHORE_HPP
+#define ZT_SEMAPHORE_HPP
 
 #include "../node/Constants.hpp"
 #include "../node/NonCopyable.hpp"
@@ -38,30 +38,16 @@
 
 namespace ZeroTier {
 
-class Condition : NonCopyable
+class Semaphore : NonCopyable
 {
 public:
-	Condition()
-		throw()
-	{
-		_sem = CreateSemaphore(NULL,0,1,NULL);
-	}
-
-	~Condition()
-	{
-		CloseHandle(_sem);
-	}
-
-	inline void wait() const
-		throw()
-	{
-		WaitForSingleObject(_sem,INFINITE);
-	}
+	Semaphore() throw() { _sem = CreateSemaphore(NULL,0,0x7fffffff,NULL); }
+	~Semaphore() { CloseHandle(_sem); }
 
-	inline void wait(unsigned long ms) const
+	inline void wait(unsigned long ms = 0) const
 		throw()
 	{
-		if (ms)
+		if (ms > 0)
 			WaitForSingleObject(_sem,(DWORD)ms);
 		else WaitForSingleObject(_sem,INFINITE);
 	}
@@ -88,51 +74,65 @@ private:
 
 namespace ZeroTier {
 
-class Condition : NonCopyable
+// This isn't quite a perfect semaphore, but the way we use it it's fine... we
+// just want this to signal when queues are ready.
+class Semaphore : NonCopyable
 {
 public:
-	Condition()
+	Semaphore()
 		throw()
 	{
 		pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
 		pthread_cond_init(&_cond,(const pthread_condattr_t *)0);
+		_cnt = 0;
 	}
 
-	~Condition()
+	~Semaphore()
 	{
 		pthread_cond_destroy(&_cond);
 		pthread_mutex_destroy(&_mh);
 	}
 
-	inline void wait() const
+	inline void wait()
 		throw()
 	{
 		pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
-		pthread_cond_wait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh));
+		if (_cnt <= 0)
+			pthread_cond_wait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh));
+		if (_cnt > 0)
+			--_cnt;
 		pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
 	}
 
-	inline void wait(unsigned long ms) const
+	inline void wait(unsigned long ms)
 		throw()
 	{
 		uint64_t when = Utils::now() + (uint64_t)ms;
 		struct timespec ts;
 		ts.tv_sec = (unsigned long)(when / 1000);
 		ts.tv_nsec = (unsigned long)(when % 1000) * 1000000;
+
 		pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
-		pthread_cond_timedwait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh),&ts);
+		if (_cnt <= 0)
+			pthread_cond_timedwait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh),&ts);
+		if (_cnt > 0)
+			--_cnt;
 		pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
 	}
 
-	inline void signal() const
+	inline void signal()
 		throw()
 	{
+		pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
+		++_cnt;
+		pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
 		pthread_cond_signal(const_cast <pthread_cond_t *>(&_cond));
 	}
 
 private:
 	pthread_cond_t _cond;
 	pthread_mutex_t _mh;
+	volatile int _cnt;
 };
 
 } // namespace ZeroTier

+ 2 - 2
testnet/SimNetSocketManager.hpp

@@ -35,7 +35,7 @@
 #include "../node/Constants.hpp"
 #include "../node/SocketManager.hpp"
 #include "../node/Mutex.hpp"
-#include "Condition.hpp"
+#include "Semaphore.hpp"
 
 namespace ZeroTier {
 
@@ -120,7 +120,7 @@ private:
 	std::map< InetAddress,TransferStats > _stats;
 	Mutex _stats_m;
 
-	Condition _waitCond;
+	Semaphore _waitCond;
 };
 
 } // namespace ZeroTier

+ 2 - 2
testnet/TestEthernetTap.hpp

@@ -40,7 +40,7 @@
 #include "../node/SharedPtr.hpp"
 #include "../node/Thread.hpp"
 #include "../node/Mutex.hpp"
-#include "Condition.hpp"
+#include "Semaphore.hpp"
 
 namespace ZeroTier {
 
@@ -129,7 +129,7 @@ private:
 
 	std::vector< TestFrame > _pq;
 	Mutex _pq_m;
-	Condition _pq_c;
+	Semaphore _pq_c;
 
 	std::vector< TestFrame > _gq;
 	Mutex _gq_m;