RAUL  0.8.0
Slave.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_SLAVE_HPP
19 #define RAUL_SLAVE_HPP
20 
21 #include <pthread.h>
22 #include "raul/Semaphore.hpp"
23 #include "raul/Thread.hpp"
24 
25 namespace Raul {
26 
27 
35 class Slave : public Thread
36 {
37 public:
38  Slave() : _whip(0) {}
39 
41  inline void whip() { _whip.post(); }
42 
43 protected:
50  virtual void _whipped() = 0;
51 
52  Semaphore _whip;
53 
54 private:
55  inline void _run() {
56  while (true) {
57  _whip.wait();
58  _whipped();
59  }
60  }
61 };
62 
63 
64 } // namespace Raul
65 
66 #endif // RAUL_SLAVE_HPP
Counting semaphore.
Definition: Semaphore.hpp:37
void wait()
Wait until count is > 0, then decrement.
Definition: Semaphore.hpp:85
void post()
Increment (and signal any waiters).
Definition: Semaphore.hpp:73
Thread driven by (realtime safe) signals.
Definition: Slave.hpp:36
void whip()
Tell the slave to do whatever work it does.
Definition: Slave.hpp:41
virtual void _whipped()=0
Worker method.
Abstract base class for a thread.
Definition: Thread.hpp:42