threadUtils.hpp

Go to the documentation of this file.
00001 /*
00002  *  anytun
00003  *
00004  *  The secure anycast tunneling protocol (satp) defines a protocol used
00005  *  for communication between any combination of unicast and anycast
00006  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
00007  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
00008  *  ethernet, ip, arp ...). satp directly includes cryptography and
00009  *  message authentication based on the methodes used by SRTP.  It is
00010  *  intended to deliver a generic, scaleable and secure solution for
00011  *  tunneling and relaying of packets of any protocol.
00012  *
00013  *
00014  *  Copyright (C) 2007 anytun.org <satp@wirdorange.org>
00015  *
00016  *  This program is free software; you can redistribute it and/or modify
00017  *  it under the terms of the GNU General Public License version 2
00018  *  as published by the Free Software Foundation.
00019  *
00020  *  This program is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  You should have received a copy of the GNU General Public License
00026  *  along with this program (see the file COPYING included with this
00027  *  distribution); if not, write to the Free Software Foundation, Inc.,
00028  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00029  */
00030 
00031 #ifndef _THREADUTILS_HPP_
00032 #define _THREADUTILS_HPP_
00033 
00034 #include <stdexcept>
00035 #include <semaphore.h>
00036 
00037 class Mutex 
00038 {
00039 public:
00040   Mutex() 
00041   { 
00042     if(pthread_mutex_init(&mutex,NULL)) 
00043       throw std::runtime_error("can't create mutex");
00044   }
00045 
00046   ~Mutex()
00047   {
00048     pthread_mutex_destroy(&mutex);
00049   }
00050   
00051 private:
00052   Mutex(const Mutex& src);
00053   void operator=(const Mutex& src);
00054   
00055   void lock()
00056   {
00057     if(pthread_mutex_lock(&mutex)) 
00058       throw std::runtime_error("can't lock mutex");
00059   }
00060   
00061   void unlock()
00062   {
00063     if(pthread_mutex_unlock(&mutex)) 
00064       throw std::runtime_error("can't unlock mutex");
00065   }
00066   friend class Lock;
00067   friend class Condition;
00068   pthread_mutex_t mutex;
00069 };
00070 
00071 
00072 class Lock
00073 {
00074 public:
00075   Lock(Mutex &m) : mutex(m)
00076   {
00077     mutex.lock();
00078   }
00079   
00080   ~Lock()
00081   {
00082     mutex.unlock();
00083   }
00084 
00085 private:
00086   Lock(const Lock& src);
00087   void operator=(const Lock& src);
00088 
00089   Mutex &mutex;
00090 };
00091 
00092 class Condition
00093 {
00094 public:
00095   Condition()
00096   {
00097     if(pthread_cond_init(&cond, NULL)) 
00098       throw std::runtime_error("can't create condition");
00099   }
00100 
00101   ~Condition()
00102   {
00103     pthread_cond_destroy(&cond);
00104   }
00105   
00106   void wait()
00107   {
00108     mutex.lock();
00109     if(pthread_cond_wait(&cond, &mutex.mutex)) 
00110     {
00111       mutex.unlock();
00112       throw std::runtime_error("error on waiting for condition");
00113     }
00114     mutex.unlock();
00115   }
00116 
00117   void signal()
00118   {
00119     mutex.lock();
00120     if(pthread_cond_signal(&cond)) 
00121     {
00122       mutex.unlock();
00123       throw std::runtime_error("can't signal condition");
00124     }
00125     mutex.unlock();
00126   }
00127 
00128   void broadcast()
00129   {
00130     mutex.lock();
00131     if(pthread_cond_broadcast(&cond)) 
00132     {
00133       mutex.unlock();
00134       throw std::runtime_error("can't broadcast condition");
00135     }
00136     mutex.unlock();
00137   }
00138   
00139 private:
00140   pthread_cond_t cond;
00141   Mutex mutex;
00142 };
00143 
00144 class Semaphore
00145 {
00146 public:
00147   Semaphore(unsigned int initVal=0)
00148   {
00149     if(sem_init(&sem, 0, initVal))
00150       throw std::runtime_error("can't create semaphore");
00151   }
00152 
00153   ~Semaphore()
00154   {
00155     sem_destroy(&sem);
00156   }
00157   
00158   void down()
00159   {
00160     if(sem_wait(&sem)) 
00161       throw std::runtime_error("error on semaphore down");
00162   }
00163 
00164   void up()
00165   {
00166     if(sem_post(&sem)) 
00167       throw std::runtime_error("error on semaphore up");
00168   }
00169 
00170 private:
00171   sem_t sem;
00172 };
00173 
00174 #endif

Generated on Sun Oct 7 23:43:49 2007 for anytun by  doxygen 1.5.1