SheafSystem  0.0.0.0
read_write_monitor.h
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Interface for class READ_WRITE_MONITOR
19 
20 
21 #ifndef READ_WRITE_MONITOR_H
22 #define READ_WRITE_MONITOR_H
23 
24 #ifndef SHEAF_DLL_SPEC_H
25 #include "SheafSystem/sheaf_dll_spec.h"
26 #endif
27 
28 
29 #ifdef _PTHREADS
30 
31 // hash map only used with threads
32 // unordered_map.h includes hashtable.h includes stl_alloc.h includes time.h
33 // causes conflict in declaration of timespec in thread_condition_variable.h
34 // if unordered_map.h included when not _PTHREADS
35 #ifndef STD_UNORDERED_MAP_H
36 #include "SheafSystem/std_unordered_map.h"
37 #endif
38 
39 #include "SheafSystem/pthread.h"
40 #endif
41 
42 #include "SheafSystem/any.h"
43 
44 #ifndef STD_UTILITY_H
45 #include "SheafSystem/std_utility.h"
46 #endif
47 
48 namespace sheaf
49 {
50 
101 class SHEAF_DLL_SPEC read_write_monitor : public any
102 {
103  friend class read_write_monitor_handle;
104 
105 public:
106  // ===========================================================
108  // ===========================================================
110 
111 public:
112 
113  // Constructors:
114 
119 
123  virtual ~read_write_monitor();
124 
125 protected:
126 
127 private:
128 
130 
131  // ===========================================================
133  // ===========================================================
135 
136 public:
137 
145  static bool access_control_disabled();
146 
147  // ///
148  // /// Disables access control.
149  // /// Disabled (true) is equivalent to having read-write access
150  // /// at all times, irrespective of any access control requests.
151  // /// Should only be invoked once at beginning of a program, before any
152  // /// other SheafSystem calls. Once disabled, access control can not be re-enabled.
153  // ///
154  // static void disable_access_control();
155 
161  static void enable_access_control();
162 
163 protected:
164 
165 private:
166 
170  static bool& private_access_control_disabled();
171 
173 
174  // ===========================================================
176  // ===========================================================
178 
179 public:
180 
184  bool is_read_accessible() const;
185 
190  bool is_not_read_accessible() const;
191 
195  bool is_read_write_accessible() const;
196 
200  bool is_not_read_write_accessible() const;
201 
205  bool is_read_only_accessible() const;
206 
210  bool is_not_read_only_accessible() const;
211 
215  int access_request_depth() const;
216 
220  void get_read_access() const;
221 
228  void get_read_write_access(bool xrelease_read_only_access = false);
229 
234  void release_access(bool xall = false) const;
235 
236 protected:
237 
241  bool access_guards_disabled() const;
242 
247  void disable_access_guards();
248 
252  void enable_access_guards();
253 
254 private:
255 
259  bool uncontrolled_is_read_write_accessible() const;
260 
264  bool uncontrolled_is_read_only_accessible() const;
265 
269  bool _access_guards_disabled;
270 
274  mutable int _ro_ct;
275 
279  mutable int _rw_ct;
280 
281  // Strictly speaking, we don't need _ro_ct and _rw_ct in
282  // the unthreaded case, but we use them anyway to make the
283  // threaded and unthreaded implementations more similar.
284 
285  struct SHEAF_DLL_SPEC thread_state_t
286  {
291  int _access_mode;
292 
297  size_t _switch_depth;
298 
304  bool _is_modified;
305  };
306 
307 
308 #ifdef _PTHREADS
309 
313  mutable unordered::unordered_map<pthread_t, thread_state_t> _thread_state;
314 
318  mutable pthread_cond_t lock_free;
319 
323  mutable pthread_mutex_t read_write_monitor_mutex;
324 
325 #else
326 
330  mutable thread_state_t _thread_state;
331 
332 #endif
333 
334 
338  void initialize_thread_state();
339 
343  bool contains_thread_state() const;
344 
348  thread_state_t& thread_state() const;
349 
353  int& access_mode() const;
354 
358  size_t& switch_depth() const;
359 
363  void request_read_only_access() const;
364 
368  void request_read_write_access() const;
369 
373  void release_read_only_access() const;
374 
378  void release_read_write_access() const;
379 
380 private:
381 
383 
384  // ===========================================================
386  // ===========================================================
388 
389 public:
390 
395  bool is_modified() const;
396 
400  void clear_is_modified();
401 
402 
403 protected:
404 
405 private:
406 
408 
409 
410  // ===========================================================
412  // ===========================================================
414 
415 public:
416 
425  bool is_mode_locked() const;
426 
435  int mode_lock_ct() const;
436 
445  void get_mode_lock();
446 
455  void release_mode_lock();
456 
457 protected:
458 
459 private:
460 
464  int _mode_lock_ct;
465 
467 
468 
469  // ===========================================================
471  // ===========================================================
473 
474 public:
475 
479  virtual bool is_ancestor_of(const any* xother) const;
480 
481 
485  virtual read_write_monitor* clone() const;
486 
487 
491  virtual bool invariant() const;
492 
493 protected:
494 
495 private:
496 
498 
499 
500 };
501 
502 } // namespace sheaf
503 
504 #endif // ifndef READ_WRITE_MONITOR_H
The monitor concurrency control interface. Class READ_WRITE_MONITOR implements the monitor concurrenc...
A handle for a hidden read_write_monitor state.
Abstract base class with useful features for all objects.
Definition: any.h:39
Namespace for the sheaves component of the sheaf system.