HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IlmThreadMutex.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright (c) Contributors to the OpenEXR Project.
4 //
5 
6 #ifndef INCLUDED_ILM_THREAD_MUTEX_H
7 #define INCLUDED_ILM_THREAD_MUTEX_H
8 
9 //-----------------------------------------------------------------------------
10 //
11 // NB: Maintained for backward compatibility with header files only. This
12 // has been entirely replaced by c++11 and the std::mutex layer
13 //
14 //-----------------------------------------------------------------------------
15 
16 #include "IlmThreadConfig.h"
17 #include "IlmThreadExport.h"
18 #include "IlmThreadNamespace.h"
19 
20 #if ILMTHREAD_THREADING_ENABLED
21 # include <mutex>
22 #endif
23 
25 
26 #if ILMTHREAD_THREADING_ENABLED
27 using Mutex ILMTHREAD_DEPRECATED ("replace with std::mutex") = std::mutex;
28 
29 // unfortunately we can't use std::unique_lock as a replacement for Lock since
30 // they have different API. Let us deprecate for now and give people a chance
31 // to clean up their code.
32 class Lock
33 {
34 public:
35  ILMTHREAD_DEPRECATED ("replace with std::lock_guard or std::unique_lock")
36  Lock (const Mutex& m, bool autoLock = true)
37  : _mutex (const_cast<Mutex&> (m)), _locked (false)
38  {
39  if (autoLock)
40  {
41  _mutex.lock ();
42  _locked = true;
43  }
44  }
45 
46  ~Lock ()
47  {
48  if (_locked) _mutex.unlock ();
49  }
50  Lock (const Lock&) = delete;
51  Lock& operator= (const Lock&) = delete;
52  Lock (Lock&&) = delete;
53  Lock& operator= (Lock&&) = delete;
54 
55  void acquire ()
56  {
57  _mutex.lock ();
58  _locked = true;
59  }
60 
61  void release ()
62  {
63  _locked = false;
64  _mutex.unlock ();
65  }
66 
67  bool locked () { return _locked; }
68 
69 private:
70  Mutex& _mutex;
71  bool _locked;
72 };
73 #endif
74 
76 
77 #endif // INCLUDED_ILM_THREAD_MUTEX_H
std::mutex Mutex
Lock(const Mutex &m, bool autoLock=true)
Lock & operator=(const Lock &)=delete
#define ILMTHREAD_INTERNAL_NAMESPACE_HEADER_ENTER
#define ILMTHREAD_INTERNAL_NAMESPACE_HEADER_EXIT
void acquire()
bool locked()
void release()
#define ILMTHREAD_DEPRECATED(msg)