diff options
-rw-r--r-- | raul/Command.hpp | 54 | ||||
-rw-r--r-- | raul/Makefile.am | 1 | ||||
-rw-r--r-- | raul/Semaphore.hpp | 12 |
3 files changed, 65 insertions, 2 deletions
diff --git a/raul/Command.hpp b/raul/Command.hpp new file mode 100644 index 0000000..296af10 --- /dev/null +++ b/raul/Command.hpp @@ -0,0 +1,54 @@ +/* This file is part of Raul. + * Copyright (C) 2007 Dave Robillard <http://drobilla.net> + * + * Raul 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 2 of the License, or (at your option) any later + * version. + * + * Raul 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 details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef RAUL_COMMAND_HPP +#define RAUL_COMMAND_HPP + +#include <raul/Semaphore.hpp> +#include <boost/utility.hpp> + +namespace Raul { + + +/** A blocking command to be executed in the audio thread. + * + * This is useful for calling simple parameterless commands from another thread + * (OSC, GUI, etc) and waiting on the result. Works well for coarsely timed + * events. Realtime safe on the commend executing side. + * + * \ingroup raul + */ +class Command : boost::noncopyable { +public: + + inline Command() : _sem(0) {} + + /** Caller context */ + inline void operator()() { _sem.wait(); } + + /** Execution context */ + inline bool pending() { return _sem.has_waiter(); } + inline void finish() { _sem.post(); } + +private: + Semaphore _sem; +}; + + +} // namespace Raul + +#endif // RAUL_COMMAND_HPP diff --git a/raul/Makefile.am b/raul/Makefile.am index ef15513..c8f25a2 100644 --- a/raul/Makefile.am +++ b/raul/Makefile.am @@ -5,6 +5,7 @@ raulinclude_HEADERS = \ Atom.hpp \ AtomicInt.hpp \ AtomicPtr.hpp \ + Command.hpp \ Deletable.hpp \ DoubleBuffer.hpp \ JackDriver.hpp \ diff --git a/raul/Semaphore.hpp b/raul/Semaphore.hpp index 3bd6ac4..09f3f7a 100644 --- a/raul/Semaphore.hpp +++ b/raul/Semaphore.hpp @@ -42,8 +42,16 @@ public: inline ~Semaphore() { sem_destroy(&_sem); } - inline void reset(unsigned int initial) - { sem_destroy(&_sem); sem_init(&_sem, 0, initial); } + inline void reset(unsigned int initial) { + sem_destroy(&_sem); + sem_init(&_sem, 0, initial); + } + + inline bool has_waiter() { + int val; + sem_getvalue(&_sem, &val); + return (val <= 0); + } /** Increment (and signal any waiters). * |