diff options
| author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2014-10-27 18:23:10 -0700 |
|---|---|---|
| committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2014-10-27 18:23:10 -0700 |
| commit | 934a575a7447bb34360e9f9f27dace971f811ce5 (patch) | |
| tree | 5867614349a3714008c68f7dc06b6f2cb09f454d /testnet | |
| parent | fe7b429866693debe0057d81f11e527f61dfd24b (diff) | |
| download | infinitytier-934a575a7447bb34360e9f9f27dace971f811ce5.tar.gz infinitytier-934a575a7447bb34360e9f9f27dace971f811ce5.zip | |
Testnet seems to work a bit better now...
Diffstat (limited to 'testnet')
| -rw-r--r-- | testnet/Semaphore.hpp (renamed from testnet/Condition.hpp) | 58 | ||||
| -rw-r--r-- | testnet/SimNetSocketManager.hpp | 4 | ||||
| -rw-r--r-- | testnet/TestEthernetTap.hpp | 4 |
3 files changed, 33 insertions, 33 deletions
diff --git a/testnet/Condition.hpp b/testnet/Semaphore.hpp index 3794ee60..d1c0d416 100644 --- a/testnet/Condition.hpp +++ b/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 diff --git a/testnet/SimNetSocketManager.hpp b/testnet/SimNetSocketManager.hpp index 1dde7fe8..df587072 100644 --- a/testnet/SimNetSocketManager.hpp +++ b/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 diff --git a/testnet/TestEthernetTap.hpp b/testnet/TestEthernetTap.hpp index babd35ad..3b1782e3 100644 --- a/testnet/TestEthernetTap.hpp +++ b/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; |
