Robust Distributed System Nucleus (rDSN)  ver 1.0.0
zlocks.h
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2015 Microsoft Corporation
5  *
6  * -=- Robust Distributed System Nucleus (rDSN) -=-
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 /*
28  * Description:
29  * lock implementation atop c service api
30  *
31  * Revision history:
32  * Mar., 2015, @imzhenyu (Zhenyu Guo), first version
33  * xxxx-xx-xx, author, fix bug about xxx
34  */
35 
36 #pragma once
37 
38 # include <dsn/service_api_c.h>
39 # include <atomic>
40 
41 namespace dsn { namespace service {
42 
47 class zlock
48 {
49 public:
50  zlock(bool recursive = false) { _h = dsn_exlock_create(recursive); }
51  ~zlock() { dsn_exlock_destroy(_h); }
52 
53  void lock() { dsn_exlock_lock(_h); }
54  bool try_lock() { return dsn_exlock_try_lock(_h); }
55  void unlock() { dsn_exlock_unlock(_h); }
56 
57 private:
58  dsn_handle_t _h;
59 
60 private:
61  // no assignment operator
62  zlock& operator=(const zlock& source);
63  zlock(const zlock& source);
64 };
72 {
73 public:
74  zrwlock_nr() { _h = dsn_rwlock_nr_create(); }
75  ~zrwlock_nr() { dsn_rwlock_nr_destroy(_h); }
76 
77  void lock_read() { dsn_rwlock_nr_lock_read(_h); }
78  void unlock_read() { dsn_rwlock_nr_unlock_read(_h); }
79  bool try_lock_read() { return dsn_rwlock_nr_try_lock_read(_h); }
80 
81  void lock_write() { dsn_rwlock_nr_lock_write(_h); }
82  void unlock_write() { dsn_rwlock_nr_unlock_write(_h); }
83  bool try_lock_write() { return dsn_rwlock_nr_try_lock_write(_h); }
84 
85 private:
86  dsn_handle_t _h;
87 
88 private:
89  // no assignment operator
90  zrwlock_nr& operator=(const zrwlock_nr& source);
91  zrwlock_nr(const zrwlock_nr& source);
92 };
100 {
101 public:
102  zsemaphore(int initial_count = 0) { _h = dsn_semaphore_create(initial_count); }
103  ~zsemaphore() { dsn_semaphore_destroy(_h); }
104 
105 public:
106  virtual void signal(int count = 1) { dsn_semaphore_signal(_h, count); }
107 
108  virtual bool wait(int timeout_milliseconds = TIME_MS_MAX)
109  {
110  if (static_cast<unsigned int>(timeout_milliseconds) == TIME_MS_MAX)
111  {
112  dsn_semaphore_wait(_h);
113  return true;
114  }
115  else
116  {
117  return dsn_semaphore_wait_timeout(_h, timeout_milliseconds);
118  }
119  }
120 
121 private:
122  dsn_handle_t _h;
123 
124 private:
125  // no assignment operator
126  zsemaphore& operator=(const zsemaphore& source);
127  zsemaphore(const zsemaphore& source);
128 };
129 
130 class zevent
131 {
132 public:
133  zevent(bool manualReset, bool initState = false);
134  ~zevent();
135 
136 public:
137  void set();
138  void reset();
139  bool wait(int timeout_milliseconds = TIME_MS_MAX);
140 
141 private:
142  zsemaphore _sema;
143  std::atomic<bool> _signaled;
144  bool _manualReset;
145 
146 private:
147  // no assignment operator
148  zevent& operator=(const zevent& source);
149  zevent(const zevent& source);
150 };
154 {
155 public:
156  zauto_lock (zlock & lock) : _lock(&lock) { _lock->lock(); }
157  ~zauto_lock() { _lock->unlock(); }
158 
159 private:
160  zlock * _lock;
161 };
162 
164 {
165 public:
166  zauto_read_lock (zrwlock_nr & lock) : _lock(&lock) { _lock->lock_read(); }
167  ~zauto_read_lock() { _lock->unlock_read(); }
168 
169 private:
170  zrwlock_nr * _lock;
171 };
172 
174 {
175 public:
176  zauto_write_lock (zrwlock_nr & lock) : _lock(&lock) { _lock->lock_write(); }
177  ~zauto_write_lock() { _lock->unlock_write(); }
178 
179 private:
180  zrwlock_nr * _lock;
181 };
182 
183 }} // end namespace dsn::service
Definition: zlocks.h:173
Definition: zlocks.h:99
Definition: zlocks.h:163
Definition: zlocks.h:71
Definition: zlocks.h:153
Definition: zlocks.h:47
DSN_API dsn_handle_t dsn_exlock_create(bool recursive)
create a recursive? or not exlusive lock
Definition: address.h:52
DSN_API dsn_handle_t dsn_semaphore_create(int initial_count)
create a semaphore with initial count equals to inital_count
Definition: zlocks.h:130