1 | /******************************************************************************* |
2 | * Copyright 2020 Intel Corporation |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | *******************************************************************************/ |
16 | |
17 | #ifndef COMMON_RW_MUTEX_HPP |
18 | #define COMMON_RW_MUTEX_HPP |
19 | |
20 | #include "utils.hpp" |
21 | |
22 | // As shared_mutex was introduced only in C++17 |
23 | // a custom implementation of read-write lock pattern is used |
24 | namespace dnnl { |
25 | namespace impl { |
26 | namespace utils { |
27 | |
28 | struct rw_mutex_t { |
29 | rw_mutex_t(); |
30 | void lock_read(); |
31 | void lock_write(); |
32 | void unlock_read(); |
33 | void unlock_write(); |
34 | ~rw_mutex_t(); |
35 | DNNL_DISALLOW_COPY_AND_ASSIGN(rw_mutex_t); |
36 | |
37 | private: |
38 | struct rw_mutex_impl_t; |
39 | std::unique_ptr<rw_mutex_impl_t> rw_mutex_impl_; |
40 | }; |
41 | |
42 | struct lock_read_t { |
43 | explicit lock_read_t(rw_mutex_t &rw_mutex); |
44 | ~lock_read_t(); |
45 | DNNL_DISALLOW_COPY_AND_ASSIGN(lock_read_t); |
46 | |
47 | private: |
48 | rw_mutex_t &rw_mutex_; |
49 | }; |
50 | |
51 | struct lock_write_t { |
52 | explicit lock_write_t(rw_mutex_t &rw_mutex_t); |
53 | ~lock_write_t(); |
54 | DNNL_DISALLOW_COPY_AND_ASSIGN(lock_write_t); |
55 | |
56 | private: |
57 | rw_mutex_t &rw_mutex_; |
58 | }; |
59 | |
60 | } // namespace utils |
61 | } // namespace impl |
62 | } // namespace dnnl |
63 | |
64 | #endif |
65 | // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s |
66 | |