switched from PracticalSocket to libasio
[anytun.git] / src / threadUtils.hpp
blob25afebdf75ed935def6cdfe77083cb917195db5f
1 /*
2 * anytun
4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
31 #include <boost/thread.hpp>
32 #include <boost/thread/mutex.hpp>
33 #ifndef __THREADUTILS__
34 #define __THREADUTILS__
35 typedef boost::mutex::scoped_lock Lock;
36 typedef boost::mutex Mutex;
38 class Semaphore
40 public:
41 Semaphore(unsigned int initVal=0)
42 :count_(initVal){};
43 void up()
45 boost::mutex::scoped_lock lock(mutex_);
46 count_++;
47 lock.unlock();
48 cond_.notify_one();
50 void down()
52 boost::mutex::scoped_lock lock(mutex_);
53 while (count_ <= 0)
54 cond_.wait(lock);
55 count_--;
57 private:
58 boost::mutex mutex_;
59 boost::condition cond_;
60 int16_t count_;
63 #endif