YARP
Yet Another Robot Platform
LockGuard.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2018 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_OS_LOCKGUARD_H
10 #define YARP_OS_LOCKGUARD_H
11 
12 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
13 # include <yarp/os/api.h>
14 #else // YARP_NO_DEPRECATED
15 # include <mutex>
16 #endif // YARP_NO_DEPRECATED
17 
18 namespace yarp {
19 namespace os {
20 
29 template <typename Lockable>
31 {
32 public:
38  explicit AbstractLockGuard(Lockable& _lock);
39 
44 
45 private:
48 
51 
52  Lockable& lock;
53 };
54 
55 
56 // Implementation
57 template <typename Lockable>
59  lock(_lock)
60 {
61  lock.lock();
62 }
63 
64 template <typename Lockable>
66 {
67  lock.unlock();
68 }
69 
70 template <typename Lockable>
72  lock(lg.lock)
73 {
74 }
75 
76 template <typename Lockable>
78 {
79  return *this;
80 }
81 
82 
83 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
84 class Mutex;
86 #else
87 using Mutex = std::mutex;
88 using RecursiveMutex = std::recursive_mutex;
89 #endif
90 
93 
94 
95 } // namespace os
96 } // namespace yarp
97 
98 #endif // YARP_OS_LOCKGUARD_H
AbstractLockGuard< RecursiveMutex > RecursiveLockGuard
Definition: LockGuard.h:92
The main, catch-all namespace for YARP.
Definition: numeric.h:47
This class is a mutex wrapper that provides a convenient RAII-style mechanism for owning a mutex for ...
Definition: LockGuard.h:30
AbstractLockGuard< Mutex > LockGuard
Definition: LockGuard.h:85
~AbstractLockGuard()
destructs the LockGuard object, unlocks the underlying mutex
Definition: LockGuard.h:65
AbstractLockGuard & operator=(const AbstractLockGuard &)
Assignment operator is disabled.
Definition: LockGuard.h:77
AbstractLockGuard(Lockable &_lock)
Acquires ownership of the given mutex _mutex.
Definition: LockGuard.h:58
RecursiveMutex offers exclusive, recursive ownership semantics:
Lockable & lock
underlining mutex
Definition: LockGuard.h:52
Basic wrapper for mutual exclusion.
Definition: Mutex.h:28