KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
lock_object.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Pooyan Dadvand
11 // Philipp Bucher (https://github.com/philbucher)
12 //
13 
14 #pragma once
15 
16 // System includes
17 #include <mutex>
18 
19 // External includes
20 #ifdef KRATOS_SMP_OPENMP
21 #include <omp.h>
22 #endif
23 
24 // Project includes
25 #include "includes/define.h"
26 
27 
28 namespace Kratos
29 {
32 
35 
37 
42 {
43 public:
46 
48  LockObject() noexcept
49  {
50 #ifdef KRATOS_SMP_OPENMP
51  omp_init_lock(&mLock);
52 #endif
53  }
54 
56  LockObject(LockObject const& rOther) = delete;
57 
59  ~LockObject() noexcept
60  {
61 #ifdef KRATOS_SMP_OPENMP
62  omp_destroy_lock(&mLock);
63 #endif
64  }
65 
69 
71  LockObject& operator=(LockObject const& rOther) = delete;
72 
76 
77  inline void lock() const
78  {
79 #ifdef KRATOS_SMP_CXX11
80  mLock.lock();
81 #elif KRATOS_SMP_OPENMP
82  omp_set_lock(&mLock);
83 #endif
84  }
85 
86  KRATOS_DEPRECATED_MESSAGE("Please use lock instead")
87  inline void SetLock() const
88  {
89  this->lock();
90  }
91 
92  inline void unlock() const
93  {
94 #ifdef KRATOS_SMP_CXX11
95  mLock.unlock();
96 #elif KRATOS_SMP_OPENMP
97  omp_unset_lock(&mLock);
98 #endif
99  }
100 
101  KRATOS_DEPRECATED_MESSAGE("Please use unlock instead")
102  inline void UnSetLock() const
103  {
104  this->unlock();
105  }
106 
107  inline bool try_lock() const
108  {
109 #ifdef KRATOS_SMP_CXX11
110  return mLock.try_lock();
111 #elif KRATOS_SMP_OPENMP
112  return omp_test_lock(&mLock);
113 #endif
114  return true;
115  }
116 
118 
119 private:
122 
123 #ifdef KRATOS_SMP_CXX11
124  mutable std::mutex mLock;
125 #elif KRATOS_SMP_OPENMP
126  mutable omp_lock_t mLock;
127 #endif
128 
130 
131 }; // Class LockObject
132 
134 
136 
137 } // namespace Kratos.
This class defines and stores a lock and gives an interface to it.
Definition: lock_object.h:42
LockObject(LockObject const &rOther)=delete
Copy constructor.
LockObject & operator=(LockObject const &rOther)=delete
Assignment operator.
void UnSetLock() const
Definition: lock_object.h:102
LockObject() noexcept
Default constructor.
Definition: lock_object.h:48
void lock() const
Definition: lock_object.h:77
~LockObject() noexcept
Destructor.
Definition: lock_object.h:59
bool try_lock() const
Definition: lock_object.h:107
void unlock() const
Definition: lock_object.h:92
void SetLock() const
Definition: lock_object.h:87
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
namespace KRATOS_DEPRECATED_MESSAGE("Please use std::filesystem directly") filesystem
Definition: kratos_filesystem.h:33