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 "IlmThreadExport.h"
17 #include "IlmThreadConfig.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 
36  ILMTHREAD_DEPRECATED ("replace with std::lock_guard or std::unique_lock")
37  Lock (const Mutex& m, bool autoLock = true):
38  _mutex (const_cast<Mutex &>(m)), _locked (false)
39  {
40  if (autoLock)
41  {
42  _mutex.lock();
43  _locked = true;
44  }
45  }
46 
47  ~Lock ()
48  {
49  if (_locked)
50  _mutex.unlock();
51  }
52  Lock (const Lock&) = delete;
53  Lock &operator= (const Lock&) = delete;
54  Lock (Lock&&) = delete;
55  Lock& operator= (Lock&&) = delete;
56 
57  void acquire ()
58  {
59  _mutex.lock();
60  _locked = true;
61  }
62 
63  void release ()
64  {
65  _locked = false;
66  _mutex.unlock();
67  }
68 
69  bool locked ()
70  {
71  return _locked;
72  }
73 
74  private:
75 
76  Mutex & _mutex;
77  bool _locked;
78 };
79 #endif
80 
82 
83 #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 const
Definition: zconf.h:214
#define ILMTHREAD_DEPRECATED(msg)