/builddir/build/BUILD/raul-0.4.0/raul/Semaphore.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
00003  * 
00004  * Raul is free software; you can redistribute it and/or modify it under the
00005  * terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  * 
00009  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
00012  * 
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00016  */
00017 
00018 #ifndef RAUL_SEMAPHORE_HPP
00019 #define RAUL_SEMAPHORE_HPP
00020 
00021 #include <semaphore.h>
00022 #include <boost/utility.hpp>
00023 
00024 namespace Raul {
00025 
00026 
00039 class Semaphore : boost::noncopyable {
00040 public:
00041     inline Semaphore(unsigned int initial) { sem_init(&_sem, 0, initial); }
00042     
00043     inline ~Semaphore() { sem_destroy(&_sem); }
00044 
00045     inline void reset(unsigned int initial) {
00046         sem_destroy(&_sem);
00047         sem_init(&_sem, 0, initial);
00048     }
00049 
00050     inline bool has_waiter() {
00051         int val;
00052         sem_getvalue(&_sem, &val);
00053         return (val <= 0);
00054     }
00055 
00060     inline void post() { sem_post(&_sem); }
00061 
00069     inline void wait() { while (sem_wait(&_sem) != 0) ; }
00070     
00077     inline bool try_wait() { return (sem_trywait(&_sem) == 0); }
00078 
00079 private:
00080     sem_t _sem;
00081 };
00082 
00083 
00084 } // namespace Raul
00085 
00086 #endif // RAUL_SEMAPHORE_HPP

Generated on Wed Apr 9 08:14:41 2008 for RAUL by  doxygen 1.5.1