Nvwa  1.1
object_level_lock.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
2 // vim:tabstop=4:shiftwidth=4:expandtab:
3 
4 /*
5  * Copyright (C) 2004-2013 Wu Yongwei <adah at users dot sourceforge dot net>
6  *
7  * This software is provided 'as-is', without any express or implied
8  * warranty. In no event will the authors be held liable for any
9  * damages arising from the use of this software.
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute
13  * it freely, subject to the following restrictions:
14  *
15  * 1. The origin of this software must not be misrepresented; you must
16  * not claim that you wrote the original software. If you use this
17  * software in a product, an acknowledgement in the product
18  * documentation would be appreciated but is not required.
19  * 2. Altered source versions must be plainly marked as such, and must
20  * not be misrepresented as being the original software.
21  * 3. This notice may not be removed or altered from any source
22  * distribution.
23  *
24  * This file is part of Stones of Nvwa:
25  * http://sourceforge.net/projects/nvwa
26  *
27  */
28 
40 #ifndef NVWA_OBJECT_LEVEL_LOCK_H
41 #define NVWA_OBJECT_LEVEL_LOCK_H
42 
43 #include "fast_mutex.h" // nvwa::fast_mutex/_NOTHREADS
44 #include "_nvwa.h" // NVWA_NAMESPACE_*
45 
46 NVWA_NAMESPACE_BEGIN
47 
48 # ifdef _NOTHREADS
49 
53  template <class _Host>
54  class object_level_lock
55  {
56  public:
58  class lock
59  {
60 # ifndef NDEBUG
61  const object_level_lock& _M_host;
62 # endif
63 
64  lock(const lock&);
65  lock& operator=(const lock&);
66  public:
67  explicit lock(const object_level_lock& host)
68 # ifndef NDEBUG
69  : _M_host(host)
70 # endif
71  {}
72 # ifndef NDEBUG
73  // The purpose of this method is allow one to write code
74  // like "assert(guard.get_locked_object() == this)" to
75  // ensure that the locked object is exactly the object being
76  // accessed.
77  const object_level_lock* get_locked_object() const
78  {
79  return &_M_host;
80  }
81 # endif
82  };
83 
84  typedef _Host volatile_type;
85  };
86 # else
87 
91  template <class _Host>
93  {
94  mutable fast_mutex _M_mtx;
95 
96  public:
97  // The C++ 1998 Standard required the use of `friend' here, but
98  // this requirement was considered a defect and subsequently
99  // changed. It is still used here for compatibility with older
100  // compilers.
101  class lock;
102  friend class lock;
103 
105  class lock
106  {
107  const object_level_lock& _M_host;
108 
109  lock(const lock&);
110  lock& operator=(const lock&);
111  public:
112  explicit lock(const object_level_lock& host) : _M_host(host)
113  {
114  _M_host._M_mtx.lock();
115  }
116  ~lock()
117  {
118  _M_host._M_mtx.unlock();
119  }
120 # ifndef NDEBUG
121  // The purpose of this method is allow one to write code
122  // like "assert(guard.get_locked_object() == this)" to
123  // ensure that the locked object is exactly the object being
124  // accessed.
125  const object_level_lock* get_locked_object() const
126  {
127  return &_M_host;
128  }
129 # endif
130  };
131 
132  typedef volatile _Host volatile_type;
133  };
134 # endif // _NOTHREADS
135 
136 NVWA_NAMESPACE_END
137 
138 #endif // NVWA_OBJECT_LEVEL_LOCK_H
Type that provides locking/unlocking semantics.
Definition: object_level_lock.h:105
Class for non-reentrant fast mutexes.
Definition: fast_mutex.h:225
A fast mutex implementation for POSIX, Win32, and modern C++.
Helper class for object-level locking.
Definition: object_level_lock.h:92
Common definitions for preprocessing.