NodeLock.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <lrtslock.h>
7 
8 #include "Utilities/Gsl.hpp"
9 #include "Utilities/NoSuchType.hpp"
10 
11 namespace Parallel {
12 /*!
13  * \ingroup ParallelGroup
14  * \brief Create a converse CmiNodeLock
15  */
16 inline CmiNodeLock create_lock() noexcept { return CmiCreateLock(); }
17 
18 /*!
19  * \ingroup ParallelGroup
20  * \brief Free a converse CmiNodeLock. Using the lock after free is undefined
21  * behavior.
22  */
23 inline void free_lock(const gsl::not_null<CmiNodeLock*> node_lock) noexcept {
24 #pragma GCC diagnostic push
25 #pragma GCC diagnostic ignored "-Wold-style-cast"
26  CmiDestroyLock(*node_lock);
27 #pragma GCC diagnostic pop
28 }
29 
30 /*!
31  * \ingroup ParallelGroup
32  * \brief Lock a converse CmiNodeLock
33  */
34 inline void lock(const gsl::not_null<CmiNodeLock*> node_lock) noexcept {
35 #pragma GCC diagnostic push
36 #pragma GCC diagnostic ignored "-Wold-style-cast"
37  CmiLock(*node_lock);
38 #pragma GCC diagnostic pop
39 }
40 
41 /// \cond
42 constexpr inline void lock(
43  const gsl::not_null<NoSuchType*> /*unused*/) noexcept {}
44 /// \endcond
45 
46 /*!
47  * \ingroup ParallelGroup
48  * \brief Returns true if the lock was successfully acquired and false if the
49  * lock is already acquired by another processor.
50  */
51 inline bool try_lock(const gsl::not_null<CmiNodeLock*> node_lock) noexcept {
52 #pragma GCC diagnostic push
53 #pragma GCC diagnostic ignored "-Wold-style-cast"
54  return CmiTryLock(*node_lock) == 0;
55 #pragma GCC diagnostic pop
56 }
57 
58 /*!
59  * \ingroup ParallelGroup
60  * \brief Unlock a converse CmiNodeLock
61  */
62 inline void unlock(const gsl::not_null<CmiNodeLock*> node_lock) noexcept {
63 #pragma GCC diagnostic push
64 #pragma GCC diagnostic ignored "-Wold-style-cast"
65  CmiUnlock(*node_lock);
66 #pragma GCC diagnostic pop
67 }
68 
69 /// \cond
70 constexpr inline void unlock(
71  const gsl::not_null<NoSuchType*> /*unused*/) noexcept {}
72 /// \endcond
73 } // namespace Parallel
CmiNodeLock create_lock() noexcept
Create a converse CmiNodeLock.
Definition: NodeLock.hpp:16
bool try_lock(const gsl::not_null< CmiNodeLock *> node_lock) noexcept
Returns true if the lock was successfully acquired and false if the lock is already acquired by anoth...
Definition: NodeLock.hpp:51
Contains functions that forward to Charm++ parallel functions.
Definition: Abort.hpp:13
void free_lock(const gsl::not_null< CmiNodeLock *> node_lock) noexcept
Free a converse CmiNodeLock. Using the lock after free is undefined behavior.
Definition: NodeLock.hpp:23
void unlock(const gsl::not_null< CmiNodeLock *> node_lock) noexcept
Unlock a converse CmiNodeLock.
Definition: NodeLock.hpp:62
void lock(const gsl::not_null< CmiNodeLock *> node_lock) noexcept
Lock a converse CmiNodeLock.
Definition: NodeLock.hpp:34
Defines functions and classes from the GSL.
Require a pointer to not be a nullptr
Definition: ConservativeFromPrimitive.hpp:12