-
Notifications
You must be signed in to change notification settings - Fork 6
/
mutex.hpp
101 lines (77 loc) · 1.7 KB
/
mutex.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* @file mutex.hpp
* @brief
* @date 2020-07-21
* @author Peter
* @copyright
* Peter of [ThinkSpirit Laboratory](http://thinkspirit.org/)
* of [Nanjing University of Information Science & Technology](http://www.nuist.edu.cn/)
* all rights reserved
*/
#ifndef KERBAL_OPENMP_MUTEX_HPP
#define KERBAL_OPENMP_MUTEX_HPP
#include <kerbal/openmp/disable_warning.hpp>
#include <kerbal/compatibility/noexcept.hpp>
#include <kerbal/utility/noncopyable.hpp>
#include <omp.h>
namespace kerbal
{
namespace openmp
{
class mutex : private kerbal::utility::noncopyable
{
private:
::omp_lock_t omp_lock;
public:
mutex() KERBAL_NOEXCEPT :
omp_lock()
{
::omp_init_lock(&omp_lock);
}
~mutex() KERBAL_NOEXCEPT
{
::omp_destroy_lock(&omp_lock);
}
void lock() KERBAL_NOEXCEPT
{
::omp_set_lock(&omp_lock);
}
bool try_lock() KERBAL_NOEXCEPT
{
return ::omp_test_lock(&omp_lock);
}
void unlock() KERBAL_NOEXCEPT
{
::omp_unset_lock(&omp_lock);
}
};
class nest_mutex : private kerbal::utility::noncopyable
{
private:
::omp_nest_lock_t omp_lock;
public:
nest_mutex() KERBAL_NOEXCEPT :
omp_lock()
{
::omp_init_nest_lock(&omp_lock);
}
~nest_mutex() KERBAL_NOEXCEPT
{
::omp_destroy_nest_lock(&omp_lock);
}
void lock() KERBAL_NOEXCEPT
{
::omp_set_nest_lock(&omp_lock);
}
bool try_lock() KERBAL_NOEXCEPT
{
return ::omp_test_nest_lock(&omp_lock);
}
void unlock() KERBAL_NOEXCEPT
{
::omp_unset_nest_lock(&omp_lock);
}
};
} // namespace openmp
} // namespace kerbal
#endif // KERBAL_OPENMP_MUTEX_HPP