Nvwa  1.1
class_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 
37 #ifndef NVWA_CLASS_LEVEL_LOCK_H
38 #define NVWA_CLASS_LEVEL_LOCK_H
39 
40 #include "fast_mutex.h" // nvwa::fast_mutex/_NOTHREADS
41 #include "_nvwa.h" // NVWA_NAMESPACE_*
42 
43 #ifndef HAVE_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION
44 #define HAVE_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION 1
45 #endif
46 
47 NVWA_NAMESPACE_BEGIN
48 
49 # ifdef _NOTHREADS
50 
54  template <class _Host, bool _RealLock = false>
55  class class_level_lock
56  {
57  public:
59  class lock
60  {
61  public:
62  lock() {}
63  };
64 
65  typedef _Host volatile_type;
66  };
67 # else
68 
75  template <class _Host, bool _RealLock = true>
77  {
78  static fast_mutex _S_mtx;
79 
80  public:
81  // The C++ 1998 Standard required the use of `friend' here, but
82  // this requirement was considered a defect and subsequently
83  // changed. It is still used here for compatibility with older
84  // compilers.
85  class lock;
86  friend class lock;
87 
89  class lock
90  {
91  lock(const lock&);
92  lock& operator=(const lock&);
93  public:
94  lock()
95  {
96  if (_RealLock)
97  _S_mtx.lock();
98  }
99  ~lock()
100  {
101  if (_RealLock)
102  _S_mtx.unlock();
103  }
104  };
105 
106  typedef volatile _Host volatile_type;
107  };
108 
109 # if HAVE_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION
110 
111  template <class _Host>
112  class class_level_lock<_Host, false>
113  {
114  public:
116  class lock
117  {
118  public:
119  lock() {}
120  };
121 
122  typedef _Host volatile_type;
123  };
124 # endif // HAVE_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION
125 
126  template <class _Host, bool _RealLock>
128 # endif // _NOTHREADS
129 
130 NVWA_NAMESPACE_END
131 
132 #endif // NVWA_CLASS_LEVEL_LOCK_H
Helper class for class-level locking.
Definition: class_level_lock.h:76
Class for non-reentrant fast mutexes.
Definition: fast_mutex.h:225
Type that provides locking/unlocking semantics.
Definition: class_level_lock.h:89
A fast mutex implementation for POSIX, Win32, and modern C++.
Common definitions for preprocessing.