ConditionSynchronization.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore
19  * @author Kai Welke (welke at kit dot edu)
20  * @date 2013
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 #if 0 // I think this header is no longer used (?)
28 
29 #include <boost/thread.hpp>
30 #include <boost/thread/recursive_mutex.hpp>
31 
32 namespace armarx
33 {
34 
35  /**
36  * @class ConditionSynchronization
37  * @ingroup Threads
38  * Encapsules the synchronization members for multiple purposes.
39  * The synchronization uses a single condition, protected by a mutex.
40  * Typical usage is to transfer data from one thread to another
41  * with a signal (producer / consumer).
42  *
43  * Example usage in producer / consumer:
44  * @code
45  * ConditionSynchronization sync;
46  * int sharedData;
47  *
48  * int consumer_thread()
49  * {
50  * while(true)
51  * {
52  * sync.lockNotify();
53  * sharedData = rand();
54  * sync.notify();
55  * sync.unlockNotify();
56  * }
57  *
58  * consumerSync.abort();
59  * }
60  *
61  * int producer_thread()
62  * {
63  * bool abort = false;
64  * while(!abort)
65  * {
66  * consumerSync.lockWait();
67  * abort = consumerSync.wait();
68  * std::cout << "Got " << sharedData << std::endl
69  * consumerSync.unlockWait();
70  * }
71  * }
72  * @endcode
73  **/
74  class ConditionSynchronization
75  {
76  public:
77  /**
78  * Initializes all synchronization objects
79  **/
80  ConditionSynchronization()
81  {
82  reset();
83 
84  // create two locks, do not lock
85  condLockWait = boost::unique_lock<boost::mutex>(condMutex, boost::defer_lock_t());
86  condLockNotify = boost::unique_lock<boost::mutex>(condMutex, boost::defer_lock_t());
87  }
88 
89  void reset()
90  {
91  // work around spurious wakeups
92  condition = false;
93  abortRequested = false;
94  }
95 
96  /**
97  * Locks the mutex for waiting. Use this before calling ConditionSynchronization::wait().
98  **/
99  void lockWait()
100  {
101  condLockWait.lock();
102  }
103 
104  /**
105  * Unlocks the mutex after waiting. Use this after calling ConditionSynchronization::wait().
106  **/
107  void unlockWait()
108  {
109  condLockWait.unlock();
110  }
111 
112  /**
113  * Locks the mutex for notifying. Use this before calling ConditionSynchronization::notify().
114  **/
115  void lockNotify()
116  {
117  condLockNotify.lock();
118  }
119 
120  /**
121  * Unlocks the mutex after notifying. Use this after calling ConditionSynchronization::notify().
122  **/
123  void unlockNotify()
124  {
125  condLockNotify.unlock();
126  }
127 
128  /**
129  * Notifies the condition
130  **/
131  void notify()
132  {
133  condition = true;
134  condVar.notify_one();
135  }
136 
137  /**
138  * Waits for condition condition
139  *
140  * @return false if abort has been requested, true otherwise
141  **/
142  bool wait()
143  {
144  while (!condition)
145  {
146  condVar.wait(condLockWait);
147  }
148 
149  condition = false;
150 
151  return !abortRequested;
152  }
153 
154  /**
155  * Aborts the synchronization. Results in notifying the condition, thus waking up a waiting thread. The abort is indicated via the return
156  * value of wait.
157  **/
158  void abort()
159  {
160  lockNotify();
161  abortRequested = true;
162  notify();
163  unlockNotify();
164  }
165 
166  private:
167  boost::mutex condMutex;
168  boost::unique_lock<boost::mutex> condLockWait;
169  boost::unique_lock<boost::mutex> condLockNotify;
170  boost::condition_variable condVar;
171  bool condition;
172  bool abortRequested;
173  };
174 
175 }
176 
177 #endif
178 
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28