diff options
author | Othmar Gsenger <otti@anytun.org> | 2008-06-19 21:57:00 +0000 |
---|---|---|
committer | Othmar Gsenger <otti@anytun.org> | 2008-06-19 21:57:00 +0000 |
commit | 0a421c36ccb98b80930cd67f66c55ee6e1ecdc97 (patch) | |
tree | 5c538b890a92495925859f7cd37d433a643c8a21 /src | |
parent | finished anyrtpproxy man page (diff) |
!!!!!!!!!!!!!!!!!!!!!!!!!!
Big changes: Moved from pThread to boost::threads
further testing needed. Version before this was testet
pretty well and should become the new release.
!!!!!!!!!!!!!!!!!!!!!!!!!!
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 2 | ||||
-rw-r--r-- | src/anytun.cpp | 38 | ||||
-rw-r--r-- | src/signalController.cpp | 2 | ||||
-rw-r--r-- | src/threadUtils.hpp | 151 |
4 files changed, 39 insertions, 154 deletions
diff --git a/src/Makefile b/src/Makefile index a3d009b..d8aeda5 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,7 +8,7 @@ CCFLAGS = -g -Wall CCFLAGS += -DSOCKETS_NAMESPACE=sockets CCFLAGS += -DSOCKETS_NAMESPACE_STR='"sockets"' LD = g++ -LDFLAGS = -g -Wall -O2 -lpthread -lgcrypt -lgpg-error -lboost_serialization +LDFLAGS = -g -Wall -O2 -lboost_thread -lgcrypt -lgpg-error -lboost_serialization ifeq ($(TARGET),Linux) CFLAGS += -D_XOPEN_SOURCE=600 diff --git a/src/anytun.cpp b/src/anytun.cpp index fdeaead..1d4ac5b 100644 --- a/src/anytun.cpp +++ b/src/anytun.cpp @@ -38,7 +38,7 @@ #include <sys/stat.h> #include <unistd.h> -#include <pthread.h> +#include <boost/bind.hpp> #include <gcrypt.h> #include <cerrno> // for ENOMEM @@ -114,7 +114,7 @@ bool checkPacketSeqNr(EncryptedPacket& pack,ConnectionParam& conn) return true; } -void* sender(void* p) +void sender(void* p) { try { @@ -199,11 +199,10 @@ void* sender(void* p) { cLog.msg(Log::PRIO_ERR) << "sender thread died due to an uncaught exception: " << e.what(); } - pthread_exit(NULL); } #ifndef ANYTUN_NOSYNC -void* syncConnector(void* p ) +void syncConnector(void* p ) { ThreadParam* param = reinterpret_cast<ThreadParam*>(p); @@ -216,10 +215,9 @@ void* syncConnector(void* p ) { h.Select(); } - pthread_exit(NULL); } -void* syncListener(void* p ) +void syncListener(void* p ) { ThreadParam* param = reinterpret_cast<ThreadParam*>(p); @@ -227,7 +225,7 @@ void* syncListener(void* p ) SyncListenSocket<SyncSocket,ConnectionList> l(h,param->cl); if (l.Bind(gOpt.getLocalSyncPort())) - pthread_exit(NULL); + return; Utility::ResolveLocal(); // resolve local hostname h.Add(&l); @@ -238,7 +236,7 @@ void* syncListener(void* p ) } #endif -void* receiver(void* p) +void receiver(void* p) { try { @@ -335,7 +333,6 @@ void* receiver(void* p) { cLog.msg(Log::PRIO_ERR) << "receiver thread died due to an uncaught exception: " << e.what(); } - pthread_exit(NULL); } #define MIN_GCRYPT_VERSION "1.2.0" @@ -534,26 +531,26 @@ int main(int argc, char* argv[]) // this must be called before any other libgcrypt call if(!initLibGCrypt()) return -1; - - pthread_t senderThread; - pthread_create(&senderThread, NULL, sender, &p); - pthread_t receiverThread; - pthread_create(&receiverThread, NULL, receiver, &p); + + boost::thread senderThread(boost::bind(sender,&p)); + boost::thread receiverThread(boost::bind(receiver,&p)); #ifndef ANYTUN_NOSYNC - pthread_t syncListenerThread; + boost::thread * syncListenerThread; if ( gOpt.getLocalSyncPort()) - pthread_create(&syncListenerThread, NULL, syncListener, &p); + syncListenerThread = new boost::thread(boost::bind(syncListener,&p)); - std::list<pthread_t> connectThreads; + std::list<boost::thread *> connectThreads; for(ConnectToList::iterator it = connect_to.begin() ;it != connect_to.end(); ++it) { - connectThreads.push_back(pthread_t()); ThreadParam * point = new ThreadParam(dev, *src, cl, queue,*it); - pthread_create(& connectThreads.back(), NULL, syncConnector, point); + connectThreads.push_back(new boost::thread(boost::bind(syncConnector,point))); } #endif int ret = sig.run(); + return ret; + // TODO cleanup here! + /* pthread_cancel(senderThread); pthread_cancel(receiverThread); #ifndef ANYTUN_NOSYNC @@ -575,7 +572,8 @@ int main(int argc, char* argv[]) delete src; delete &p.connto; - return ret; + return ret; + */ } catch(std::runtime_error e) { diff --git a/src/signalController.cpp b/src/signalController.cpp index f77abf6..5e87ba0 100644 --- a/src/signalController.cpp +++ b/src/signalController.cpp @@ -33,7 +33,7 @@ #include <iostream> -#include "threadUtils.hpp" +//#include "threadUtils.hpp" #include "signalController.h" #include "log.h" diff --git a/src/threadUtils.hpp b/src/threadUtils.hpp index f084980..65eb321 100644 --- a/src/threadUtils.hpp +++ b/src/threadUtils.hpp @@ -27,149 +27,36 @@ * distribution); if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - -#ifndef _THREADUTILS_HPP_ -#define _THREADUTILS_HPP_ - -#include <stdexcept> -#include <pthread.h> -#include <semaphore.h> - -class Mutex -{ -public: - Mutex() - { - if(pthread_mutex_init(&mutex,NULL)) - throw std::runtime_error("can't create mutex"); - } - - ~Mutex() - { - pthread_mutex_destroy(&mutex); - } - -private: - Mutex(const Mutex& src); - void operator=(const Mutex& src); - - void lock() - { - if(pthread_mutex_lock(&mutex)) - throw std::runtime_error("can't lock mutex"); - } - - void unlock() - { - if(pthread_mutex_unlock(&mutex)) - throw std::runtime_error("can't unlock mutex"); - } - friend class Lock; - friend class Condition; - pthread_mutex_t mutex; -}; - - -class Lock -{ -public: - Lock(Mutex &m) : mutex(m) - { - mutex.lock(); - } - - ~Lock() - { - mutex.unlock(); - } - -private: - Lock(const Lock& src); - void operator=(const Lock& src); - - Mutex &mutex; -}; - -class Condition -{ -public: - Condition() - { - if(pthread_cond_init(&cond, NULL)) - throw std::runtime_error("can't create condition"); - } - - ~Condition() - { - pthread_cond_destroy(&cond); - } - - void wait() - { - mutex.lock(); - if(pthread_cond_wait(&cond, &mutex.mutex)) - { - mutex.unlock(); - throw std::runtime_error("error on waiting for condition"); - } - mutex.unlock(); - } - - void signal() - { - mutex.lock(); - if(pthread_cond_signal(&cond)) - { - mutex.unlock(); - throw std::runtime_error("can't signal condition"); - } - mutex.unlock(); - } - - void broadcast() - { - mutex.lock(); - if(pthread_cond_broadcast(&cond)) - { - mutex.unlock(); - throw std::runtime_error("can't broadcast condition"); - } - mutex.unlock(); - } - -private: - pthread_cond_t cond; - Mutex mutex; -}; +#include <boost/thread.hpp> +#include <boost/thread/mutex.hpp> +#ifndef __THREADUTILS__ +#define __THREADUTILS__ +typedef boost::mutex::scoped_lock Lock; +typedef boost::mutex Mutex; class Semaphore { public: Semaphore(unsigned int initVal=0) + :count_(initVal){}; + void up() { - if(sem_init(&sem, 0, initVal)) - throw std::runtime_error("can't create semaphore"); - } - - ~Semaphore() - { - sem_destroy(&sem); + boost::mutex::scoped_lock lock(mutex_); + count_++; + lock.unlock(); + cond_.notify_one(); } - void down() { - if(sem_wait(&sem)) - throw std::runtime_error("error on semaphore down"); - } - - void up() - { - if(sem_post(&sem)) - throw std::runtime_error("error on semaphore up"); + boost::mutex::scoped_lock lock(mutex_); + while (count_ <= 0) + cond_.wait(lock); + count_--; } - private: - sem_t sem; + boost::mutex mutex_; + boost::condition cond_; + int16_t count_; }; #endif |