summaryrefslogtreecommitdiffstats
path: root/raul
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2007-12-04 23:02:14 +0000
committerDavid Robillard <d@drobilla.net>2007-12-04 23:02:14 +0000
commitfeab87ff3c9dce87ef127c8c7e42e6d51950aa2e (patch)
tree384de5815f2da11fb4079209fd186e6a361fad52 /raul
parent6bb9c35360b3919236ed1df997e2021f817f3d19 (diff)
downloadraul-feab87ff3c9dce87ef127c8c7e42e6d51950aa2e.tar.gz
raul-feab87ff3c9dce87ef127c8c7e42e6d51950aa2e.tar.bz2
raul-feab87ff3c9dce87ef127c8c7e42e6d51950aa2e.zip
Added 'has_waiter' method for Semaphore.
Added new Command class, for e.g. coarse GUI->Engine RT safe events. git-svn-id: http://svn.drobilla.net/lad/raul@947 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'raul')
-rw-r--r--raul/Command.hpp54
-rw-r--r--raul/Makefile.am1
-rw-r--r--raul/Semaphore.hpp12
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).
*