YARP
Yet Another Robot Platform
LockGuard.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 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 #include <yarp/conf/system.h>
13 
14 #if !defined(YARP_INCLUDING_DEPRECATED_HEADER_ON_PURPOSE)
15 YARP_COMPILER_WARNING("<yarp/os/LockGuard.h> file is deprecated")
16 #endif
17 
18 #ifndef YARP_NO_DEPRECATED // since YARP 3.3
19 
20 #include <yarp/os/api.h>
21 #include <mutex>
22 
23 namespace yarp {
24 namespace os {
25 
26 class Mutex;
27 class RecursiveMutex;
28 
39 template <typename Lockable>
40 class YARP_DEPRECATED_MSG("Use std::lock_guard or std::scoped_lock instead") AbstractLockGuard
41 {
42 public:
48  explicit AbstractLockGuard(Lockable& _lock);
49 
54 
55 private:
58 
60  AbstractLockGuard& operator=(const AbstractLockGuard&);
61 
62  Lockable& lock;
63 };
64 
65 // Implementation
66 
69 
70 template <typename Lockable>
72  lock(_lock)
73 {
74  lock.lock();
75 }
76 
77 template <typename Lockable>
79 {
80  lock.unlock();
81 }
82 
83 template <typename Lockable>
85  lock(lg.lock)
86 {
87 }
88 
89 template <typename Lockable>
91 {
92  return *this;
93 }
94 
95 YARP_DEPRECATED_TYPEDEF_MSG("use std::lock_guard or std::scoped_lock instead") AbstractLockGuard<Mutex> LockGuard;
96 YARP_DEPRECATED_TYPEDEF_MSG("use std::lock_guard or std::scoped_lock instead") AbstractLockGuard<RecursiveMutex> RecursiveLockGuard;
97 
99 
100 } // namespace os
101 } // namespace yarp
102 
103 #endif // YARP_NO_DEPRECATED
104 
105 #endif // YARP_OS_LOCKGUARD_H
YARP_WARNING_PUSH
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:334
yarp::os::RecursiveMutex
RecursiveMutex offers exclusive, recursive ownership semantics:
Definition: RecursiveMutex.h:44
yarp::os::AbstractLockGuard::AbstractLockGuard
AbstractLockGuard(Lockable &_lock)
Acquires ownership of the given mutex _mutex.
Definition: LockGuard.h:71
yarp::os::AbstractLockGuard::~AbstractLockGuard
~AbstractLockGuard()
destructs the LockGuard object, unlocks the underlying mutex
Definition: LockGuard.h:78
api.h
yarp::os::AbstractLockGuard
This class is a mutex wrapper that provides a convenient RAII-style mechanism for owning a mutex for ...
Definition: LockGuard.h:41
system.h
YARP_WARNING_POP
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:335
YARP_DEPRECATED_MSG
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2883
YARP_DEPRECATED_TYPEDEF_MSG
#define YARP_DEPRECATED_TYPEDEF_MSG(x)
Definition: api.h:102
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::Mutex
Basic wrapper for mutual exclusion.
Definition: Mutex.h:35
YARP_COMPILER_WARNING
#define YARP_COMPILER_WARNING(x)
Generate a warning at build time on supported compilers.
Definition: system.h:112
YARP_DISABLE_DEPRECATED_WARNING
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:336