From 934a575a7447bb34360e9f9f27dace971f811ce5 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 27 Oct 2014 18:23:10 -0700 Subject: Testnet seems to work a bit better now... --- testnet/Condition.hpp | 142 ---------------------------------------- testnet/Semaphore.hpp | 142 ++++++++++++++++++++++++++++++++++++++++ testnet/SimNetSocketManager.hpp | 4 +- testnet/TestEthernetTap.hpp | 4 +- 4 files changed, 146 insertions(+), 146 deletions(-) delete mode 100644 testnet/Condition.hpp create mode 100644 testnet/Semaphore.hpp (limited to 'testnet') diff --git a/testnet/Condition.hpp b/testnet/Condition.hpp deleted file mode 100644 index 3794ee60..00000000 --- a/testnet/Condition.hpp +++ /dev/null @@ -1,142 +0,0 @@ -/* - * ZeroTier One - Global Peer to Peer Ethernet - * Copyright (C) 2012-2014 ZeroTier Networks LLC - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * -- - * - * ZeroTier may be used and distributed under the terms of the GPLv3, which - * are available at: http://www.gnu.org/licenses/gpl-3.0.html - * - * If you would like to embed ZeroTier into a commercial application or - * redistribute it in a modified binary form, please contact ZeroTier Networks - * LLC. Start here: http://www.zerotier.com/ - */ - -#ifndef ZT_CONDITION_HPP -#define ZT_CONDITION_HPP - -#include "../node/Constants.hpp" -#include "../node/NonCopyable.hpp" - -#ifdef __WINDOWS__ - -#include -#include - -namespace ZeroTier { - -class Condition : NonCopyable -{ -public: - Condition() - throw() - { - _sem = CreateSemaphore(NULL,0,1,NULL); - } - - ~Condition() - { - CloseHandle(_sem); - } - - inline void wait() const - throw() - { - WaitForSingleObject(_sem,INFINITE); - } - - inline void wait(unsigned long ms) const - throw() - { - if (ms) - WaitForSingleObject(_sem,(DWORD)ms); - else WaitForSingleObject(_sem,INFINITE); - } - - inline void signal() const - throw() - { - ReleaseSemaphore(_sem,1,NULL); - } - -private: - HANDLE _sem; -}; - -} // namespace ZeroTier - -#else // !__WINDOWS__ - -#include -#include -#include - -#include "../node/Utils.hpp" - -namespace ZeroTier { - -class Condition : NonCopyable -{ -public: - Condition() - throw() - { - pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0); - pthread_cond_init(&_cond,(const pthread_condattr_t *)0); - } - - ~Condition() - { - pthread_cond_destroy(&_cond); - pthread_mutex_destroy(&_mh); - } - - inline void wait() const - throw() - { - pthread_mutex_lock(const_cast (&_mh)); - pthread_cond_wait(const_cast (&_cond),const_cast (&_mh)); - pthread_mutex_unlock(const_cast (&_mh)); - } - - inline void wait(unsigned long ms) const - 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 (&_mh)); - pthread_cond_timedwait(const_cast (&_cond),const_cast (&_mh),&ts); - pthread_mutex_unlock(const_cast (&_mh)); - } - - inline void signal() const - throw() - { - pthread_cond_signal(const_cast (&_cond)); - } - -private: - pthread_cond_t _cond; - pthread_mutex_t _mh; -}; - -} // namespace ZeroTier - -#endif // !__WINDOWS__ - -#endif diff --git a/testnet/Semaphore.hpp b/testnet/Semaphore.hpp new file mode 100644 index 00000000..d1c0d416 --- /dev/null +++ b/testnet/Semaphore.hpp @@ -0,0 +1,142 @@ +/* + * ZeroTier One - Global Peer to Peer Ethernet + * Copyright (C) 2012-2014 ZeroTier Networks LLC + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +#ifndef ZT_SEMAPHORE_HPP +#define ZT_SEMAPHORE_HPP + +#include "../node/Constants.hpp" +#include "../node/NonCopyable.hpp" + +#ifdef __WINDOWS__ + +#include +#include + +namespace ZeroTier { + +class Semaphore : NonCopyable +{ +public: + Semaphore() throw() { _sem = CreateSemaphore(NULL,0,0x7fffffff,NULL); } + ~Semaphore() { CloseHandle(_sem); } + + inline void wait(unsigned long ms = 0) const + throw() + { + if (ms > 0) + WaitForSingleObject(_sem,(DWORD)ms); + else WaitForSingleObject(_sem,INFINITE); + } + + inline void signal() const + throw() + { + ReleaseSemaphore(_sem,1,NULL); + } + +private: + HANDLE _sem; +}; + +} // namespace ZeroTier + +#else // !__WINDOWS__ + +#include +#include +#include + +#include "../node/Utils.hpp" + +namespace ZeroTier { + +// 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: + Semaphore() + throw() + { + pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0); + pthread_cond_init(&_cond,(const pthread_condattr_t *)0); + _cnt = 0; + } + + ~Semaphore() + { + pthread_cond_destroy(&_cond); + pthread_mutex_destroy(&_mh); + } + + inline void wait() + throw() + { + pthread_mutex_lock(const_cast (&_mh)); + if (_cnt <= 0) + pthread_cond_wait(const_cast (&_cond),const_cast (&_mh)); + if (_cnt > 0) + --_cnt; + pthread_mutex_unlock(const_cast (&_mh)); + } + + 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 (&_mh)); + if (_cnt <= 0) + pthread_cond_timedwait(const_cast (&_cond),const_cast (&_mh),&ts); + if (_cnt > 0) + --_cnt; + pthread_mutex_unlock(const_cast (&_mh)); + } + + inline void signal() + throw() + { + pthread_mutex_lock(const_cast (&_mh)); + ++_cnt; + pthread_mutex_unlock(const_cast (&_mh)); + pthread_cond_signal(const_cast (&_cond)); + } + +private: + pthread_cond_t _cond; + pthread_mutex_t _mh; + volatile int _cnt; +}; + +} // namespace ZeroTier + +#endif // !__WINDOWS__ + +#endif 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; -- cgit v1.2.3