Synchronization.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::core
19 * @author Jan Issac (jan dot issac at gmx dot de)
20 * @date 2012
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
28 
29 #include <boost/thread/locks.hpp>
30 
31 #include <boost/thread/mutex.hpp>
32 #include <boost/thread/recursive_mutex.hpp>
33 #include <boost/thread/shared_mutex.hpp>
34 #include <boost/thread/condition_variable.hpp>
35 
36 #define MUTEX_TIMEOUT_MSEC 10000
38 {
39  /**
40  * @struct MutexTimeoutException
41  * @ingroup Threads
42  * @brief The MutexTimeoutException struct
43  */
44  struct MutexTimeoutException : LocalException
45  {
46  MutexTimeoutException(int timeoutDelayMs, std::string mutexName = "")
47  {
48  std::stringstream reason;
49  reason << "A Thread failed to get a mutex '" + mutexName + "' after " <<
50  timeoutDelayMs * 0.001 <<
51  " seconds!";
52  setReason(reason.str());
53  }
54  std::string name() const override
55  {
56  return "armarx::exceptions::local::MutexTimeoutException";
57  }
58  };
59 
60  struct MutexDestructionException : LocalException
61  {
62  MutexDestructionException(std::string mutexName = "") : LocalException("A mutex '" + mutexName + "' was not already unlocked in the destructor!") {}
63  std::string name() const override
64  {
65  return "armarx::exceptions::local::MutexDestructionException";
66  }
67  };
68 }
69 
70 namespace armarx
71 {
72  /**
73  * @class HiddenTimedMutex
74  * @defgroup Threads
75  * @brief The HiddenTimedMutex class is a mutex, that has a normal boost::mutex
76  * interface, but will timeout after fixed (defined in MUTEX_TIMEOUT_MSEC)
77  * interval and throw a MutexTimeoutException, if it could not aquire the
78  * mutex in that time.<br/>
79  * This way, the stacktrace to the deadlock can be easily retrieved.
80  */
82  {
83  boost::timed_mutex timedMutex;
84  int timeoutDelayMs;
85  std::string mutexName;
86  public:
87  HiddenTimedMutex(int timeout = MUTEX_TIMEOUT_MSEC, std::string mutexName = ""): timeoutDelayMs(timeout), mutexName(mutexName) {}
89  {
90  if (!try_lock())
91  {
92  // throw exceptions::local::MutexDestructionException();
93  std::cout << "A mutex ('" << mutexName << "') was still locked in the destructor! Fix Application!\nBacktrace:\n" << LocalException::generateBacktrace() << std::endl;
94  }
95  else
96  {
97  unlock();
98  }
99  }
100 
101  void lock()
102  {
103  boost::system_time timeout = boost::get_system_time() +
104  boost::posix_time::milliseconds(timeoutDelayMs);
105 
106  if (!timedMutex.timed_lock(timeout))
107  {
108  throw exceptions::local::MutexTimeoutException(timeoutDelayMs, mutexName);
109  }
110  }
111 
112  bool try_lock()
113  {
114  return timedMutex.try_lock();
115  }
116 
117  void unlock()
118  {
119  timedMutex.unlock();
120  }
121 
122  using ScopedLock = boost::unique_lock<HiddenTimedMutex>;
123  using ScopedTryLock = boost::detail::try_lock_wrapper<HiddenTimedMutex>;
124  };
125  /**
126  \addtogroup Threads
127  @{
128  */
129 
130 
131  using Mutex = boost::mutex ;
132  using ScopedLock = Mutex::scoped_lock ;
133  using ScopedLockPtr = std::shared_ptr<ScopedLock> ;
134  using ScopedTryLock = Mutex::scoped_try_lock ;
135 
136  using TimedMutex = boost::timed_mutex ;
137  using ScopedTimedMutex = TimedMutex::scoped_lock ;
138  using ScopedTimedMutexPtr = std::shared_ptr<ScopedTimedMutex> ;
139  using ScopedTimedTryMutex = TimedMutex::scoped_try_lock ;
140 
141  using RecursiveMutex = boost::recursive_mutex ;
142  using ScopedRecursiveLock = RecursiveMutex::scoped_lock ;
143  using ScopedRecursiveLockPtr = std::shared_ptr<ScopedRecursiveLock> ;
144  using ScopedRecursiveTryLock = RecursiveMutex::scoped_try_lock ;
145 
146  using TimedRecursiveMutex = boost::recursive_timed_mutex ;
147  using ScopedTimedRecursiveLock = TimedRecursiveMutex::scoped_lock ;
148  using ScopedTimedRecursiveLockPtr = std::shared_ptr<ScopedTimedRecursiveLock>;
149  using ScopedTimedRecursiveTryLock = TimedRecursiveMutex::scoped_try_lock ;
150 
151  using SharedMutex = boost::shared_mutex ;
152 
153  using ScopedUniqueLock = boost::unique_lock<boost::shared_mutex>;
154  using ScopedSharedLock = boost::shared_lock<boost::shared_mutex>;
155 
156  using ScopedUniqueLockPtr = std::shared_ptr<ScopedUniqueLock>;
157  using ScopedSharedLockPtr = std::shared_ptr<ScopedSharedLock >;
158 
159  using ConditionalVariable = boost::condition_variable ;
160  /*
161  @}
162  */
163 }
164 
MUTEX_TIMEOUT_MSEC
#define MUTEX_TIMEOUT_MSEC
Definition: Synchronization.h:36
armarx::ScopedUniqueLock
boost::unique_lock< boost::shared_mutex > ScopedUniqueLock
Definition: Synchronization.h:153
armarx::HiddenTimedMutex::ScopedTryLock
boost::detail::try_lock_wrapper< HiddenTimedMutex > ScopedTryLock
Definition: Synchronization.h:123
armarx::exceptions::local::MutexTimeoutException
The MutexTimeoutException struct.
Definition: Synchronization.h:44
armarx::HiddenTimedMutex::try_lock
bool try_lock()
Definition: Synchronization.h:112
armarx::ScopedTimedMutex
TimedMutex::scoped_lock ScopedTimedMutex
Definition: Synchronization.h:137
armarx::TimedMutex
boost::timed_mutex TimedMutex
Definition: Synchronization.h:136
armarx::HiddenTimedMutex
Definition: Synchronization.h:81
armarx::HiddenTimedMutex::lock
void lock()
Definition: Synchronization.h:101
armarx::ScopedLock
Mutex::scoped_lock ScopedLock
Definition: Synchronization.h:132
armarx::ScopedTimedTryMutex
TimedMutex::scoped_try_lock ScopedTimedTryMutex
Definition: Synchronization.h:139
armarx::exceptions::local::MutexTimeoutException::MutexTimeoutException
MutexTimeoutException(int timeoutDelayMs, std::string mutexName="")
Definition: Synchronization.h:46
armarx::ConditionalVariable
boost::condition_variable ConditionalVariable
Definition: Synchronization.h:159
armarx::ScopedSharedLockPtr
std::shared_ptr< ScopedSharedLock > ScopedSharedLockPtr
Definition: Synchronization.h:157
armarx::exceptions::local
Definition: DynamicLibraryException.h:31
armarx::exceptions::local::MutexTimeoutException::name
std::string name() const override
Definition: Synchronization.h:54
armarx::ScopedRecursiveLock
RecursiveMutex::scoped_lock ScopedRecursiveLock
Definition: Synchronization.h:142
armarx::ScopedTimedRecursiveLockPtr
std::shared_ptr< ScopedTimedRecursiveLock > ScopedTimedRecursiveLockPtr
Definition: Synchronization.h:148
armarx::ScopedLockPtr
std::shared_ptr< ScopedLock > ScopedLockPtr
Definition: Synchronization.h:133
armarx::ScopedTimedRecursiveTryLock
TimedRecursiveMutex::scoped_try_lock ScopedTimedRecursiveTryLock
Definition: Synchronization.h:149
armarx::ScopedTryLock
Mutex::scoped_try_lock ScopedTryLock
Definition: Synchronization.h:134
armarx::SharedMutex
boost::shared_mutex SharedMutex
Definition: Synchronization.h:151
armarx::HiddenTimedMutex::~HiddenTimedMutex
~HiddenTimedMutex()
Definition: Synchronization.h:88
armarx::ScopedSharedLock
boost::shared_lock< boost::shared_mutex > ScopedSharedLock
Definition: Synchronization.h:154
armarx::ScopedRecursiveTryLock
RecursiveMutex::scoped_try_lock ScopedRecursiveTryLock
Definition: Synchronization.h:144
armarx::Mutex
boost::mutex Mutex
Definition: Synchronization.h:131
armarx::TimedRecursiveMutex
boost::recursive_timed_mutex TimedRecursiveMutex
Definition: Synchronization.h:146
armarx::ScopedRecursiveLockPtr
std::shared_ptr< ScopedRecursiveLock > ScopedRecursiveLockPtr
Definition: Synchronization.h:143
armarx::HiddenTimedMutex::HiddenTimedMutex
HiddenTimedMutex(int timeout=MUTEX_TIMEOUT_MSEC, std::string mutexName="")
Definition: Synchronization.h:87
armarx::HiddenTimedMutex::ScopedLock
boost::unique_lock< HiddenTimedMutex > ScopedLock
Definition: Synchronization.h:122
armarx::ScopedUniqueLockPtr
std::shared_ptr< ScopedUniqueLock > ScopedUniqueLockPtr
Definition: Synchronization.h:156
armarx::HiddenTimedMutex::unlock
void unlock()
Definition: Synchronization.h:117
armarx::ScopedTimedMutexPtr
std::shared_ptr< ScopedTimedMutex > ScopedTimedMutexPtr
Definition: Synchronization.h:138
armarx::RecursiveMutex
boost::recursive_mutex RecursiveMutex
Definition: Synchronization.h:141
armarx::exceptions::local::MutexDestructionException::MutexDestructionException
MutexDestructionException(std::string mutexName="")
Definition: Synchronization.h:62
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::exceptions::local::MutexDestructionException
Definition: Synchronization.h:60
Exception.h
armarx::exceptions::local::MutexDestructionException::name
std::string name() const override
Definition: Synchronization.h:63
armarx::ScopedTimedRecursiveLock
TimedRecursiveMutex::scoped_lock ScopedTimedRecursiveLock
Definition: Synchronization.h:147