YARP
Yet Another Robot Platform
LockGuard.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_OS_LOCKGUARD_H
7 #define YARP_OS_LOCKGUARD_H
8 
9 #include <yarp/conf/system.h>
10 
11 #if !defined(YARP_INCLUDING_DEPRECATED_HEADER_ON_PURPOSE)
12 YARP_COMPILER_WARNING("<yarp/os/LockGuard.h> file is deprecated")
13 #endif
14 
15 #ifndef YARP_NO_DEPRECATED // since YARP 3.3
16 
17 #include <yarp/os/api.h>
18 #include <mutex>
19 
20 namespace yarp {
21 namespace os {
22 
23 class Mutex;
24 class RecursiveMutex;
25 
36 template <typename Lockable>
37 class YARP_DEPRECATED_MSG("Use std::lock_guard or std::scoped_lock instead") AbstractLockGuard
38 {
39 public:
45  explicit AbstractLockGuard(Lockable& _lock);
46 
51 
52 private:
55 
57  AbstractLockGuard& operator=(const AbstractLockGuard&);
58 
59  Lockable& lock;
60 };
61 
62 // Implementation
63 
66 
67 template <typename Lockable>
69  lock(_lock)
70 {
71  lock.lock();
72 }
73 
74 template <typename Lockable>
76 {
77  lock.unlock();
78 }
79 
80 template <typename Lockable>
82  lock(lg.lock)
83 {
84 }
85 
86 template <typename Lockable>
88 {
89  return *this;
90 }
91 
92 YARP_DEPRECATED_TYPEDEF_MSG("use std::lock_guard or std::scoped_lock instead") AbstractLockGuard<Mutex> LockGuard;
93 YARP_DEPRECATED_TYPEDEF_MSG("use std::lock_guard or std::scoped_lock instead") AbstractLockGuard<RecursiveMutex> RecursiveLockGuard;
94 
96 
97 } // namespace os
98 } // namespace yarp
99 
100 #endif // YARP_NO_DEPRECATED
101 
102 #endif // YARP_OS_LOCKGUARD_H
This class is a mutex wrapper that provides a convenient RAII-style mechanism for owning a mutex for ...
Definition: LockGuard.h:38
~AbstractLockGuard()
destructs the LockGuard object, unlocks the underlying mutex
Definition: LockGuard.h:75
AbstractLockGuard(Lockable &_lock)
Acquires ownership of the given mutex _mutex.
Definition: LockGuard.h:68
Basic wrapper for mutual exclusion.
Definition: Mutex.h:32
RecursiveMutex offers exclusive, recursive ownership semantics:
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2885
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:332
#define YARP_COMPILER_WARNING(x)
Generate a warning at build time on supported compilers.
Definition: system.h:109
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:331
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:333
#define YARP_DEPRECATED_TYPEDEF_MSG(x)
Definition: api.h:99