yamc_shared_lock.hpp 5.07 KB
Newer Older
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
/*
 * yamc_shared_lock.hpp
 *
 * MIT License
 *
 * Copyright (c) 2017 yohhoy
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
26
27
#ifndef LIGHTGBM_INCLUDE_LIGHTGBM_UTILS_YAMC_YAMC_SHARED_LOCK_HPP_
#define LIGHTGBM_INCLUDE_LIGHTGBM_UTILS_YAMC_YAMC_SHARED_LOCK_HPP_
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

#include <cassert>
#include <chrono>
#include <mutex>
#include <system_error>
#include <utility>  // std::swap

/*
 * std::shared_lock in C++14 Standard Library
 *
 * - yamc::shared_lock<Mutex>
 */
namespace yamc {

template <typename Mutex>
class shared_lock {
  void locking_precondition(const char* emsg) {
    if (pm_ == nullptr) {
      throw std::system_error(
          std::make_error_code(std::errc::operation_not_permitted), emsg);
    }
    if (owns_) {
      throw std::system_error(
          std::make_error_code(std::errc::resource_deadlock_would_occur), emsg);
    }
  }

 public:
  using mutex_type = Mutex;

  shared_lock() noexcept = default;

  explicit shared_lock(mutex_type* m) {
    m->lock_shared();
    pm_ = m;
    owns_ = true;
  }

  shared_lock(const mutex_type& m, std::defer_lock_t) noexcept {
    pm_ = &m;
    owns_ = false;
  }

  shared_lock(const mutex_type& m, std::try_to_lock_t) {
    pm_ = &m;
    owns_ = m.try_lock_shared();
  }

  shared_lock(const mutex_type& m, std::adopt_lock_t) {
    pm_ = &m;
    owns_ = true;
  }

  template <typename Clock, typename Duration>
  shared_lock(const mutex_type& m,
              const std::chrono::time_point<Clock, Duration>& abs_time) {
    pm_ = &m;
    owns_ = m.try_lock_shared_until(abs_time);
  }

  template <typename Rep, typename Period>
  shared_lock(const mutex_type& m,
              const std::chrono::duration<Rep, Period>& rel_time) {
    pm_ = &m;
    owns_ = m.try_lock_shared_for(rel_time);
  }

  ~shared_lock() {
    if (owns_) {
      assert(pm_ != nullptr);
      pm_->unlock_shared();
    }
  }

  shared_lock(const shared_lock&) = delete;
  shared_lock& operator=(const shared_lock&) = delete;

  shared_lock(shared_lock&& rhs) noexcept {
    if (pm_ && owns_) {
      pm_->unlock_shared();
    }
    pm_ = rhs.pm_;
    owns_ = rhs.owns_;
    rhs.pm_ = nullptr;
    rhs.owns_ = false;
  }

  shared_lock& operator=(shared_lock&& rhs) noexcept {
    if (pm_ && owns_) {
      pm_->unlock_shared();
    }
    pm_ = rhs.pm_;
    owns_ = rhs.owns_;
    rhs.pm_ = nullptr;
    rhs.owns_ = false;
    return *this;
  }

  void lock() {
    locking_precondition("shared_lock::lock");
    pm_->lock_shared();
    owns_ = true;
  }

  bool try_lock() {
    locking_precondition("shared_lock::try_lock");
    return (owns_ = pm_->try_lock_shared());
  }

  template <typename Rep, typename Period>
  bool try_lock_for(const std::chrono::duration<Rep, Period>& rel_time) {
    locking_precondition("shared_lock::try_lock_for");
    return (owns_ = pm_->try_lock_shared_for(rel_time));
  }

  template <typename Clock, typename Duration>
  bool try_lock_until(
      const std::chrono::time_point<Clock, Duration>& abs_time) {
    locking_precondition("shared_lock::try_lock_until");
    return (owns_ = pm_->try_lock_shared_until(abs_time));
  }

  void unlock() {
    assert(pm_ != nullptr);
    if (!owns_) {
      throw std::system_error(
          std::make_error_code(std::errc::operation_not_permitted),
          "shared_lock::unlock");
    }
    pm_->unlock_shared();
    owns_ = false;
  }

  void swap(shared_lock& sl) noexcept {
    std::swap(pm_, sl.pm_);
    std::swap(owns_, sl.owns_);
  }

  mutex_type* release() noexcept {
    mutex_type* result = pm_;
    pm_ = nullptr;
    owns_ = false;
    return result;
  }

  bool owns_lock() const noexcept { return owns_; }

  explicit operator bool() const noexcept { return owns_; }

  mutex_type* mutex() const noexcept { return pm_; }

 private:
  mutex_type* pm_ = nullptr;
  bool owns_ = false;
};

}  // namespace yamc

namespace std {

/// std::swap() specialization for yamc::shared_lock<Mutex> type
template <typename Mutex>
void swap(yamc::shared_lock<Mutex>& lhs,
          yamc::shared_lock<Mutex>& rhs) noexcept {
  lhs.swap(rhs);
}

}  // namespace std

197
#endif  // LIGHTGBM_INCLUDE_LIGHTGBM_UTILS_YAMC_YAMC_SHARED_LOCK_HPP_