class_level_lock.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  2. // vim:tabstop=4:shiftwidth=4:expandtab:
  3. /*
  4. * Copyright (C) 2004-2008 Wu Yongwei <adah at users dot sourceforge dot net>
  5. *
  6. * This software is provided 'as-is', without any express or implied
  7. * warranty. In no event will the authors be held liable for any
  8. * damages arising from the use of this software.
  9. *
  10. * Permission is granted to anyone to use this software for any purpose,
  11. * including commercial applications, and to alter it and redistribute
  12. * it freely, subject to the following restrictions:
  13. *
  14. * 1. The origin of this software must not be misrepresented; you must
  15. * not claim that you wrote the original software. If you use this
  16. * software in a product, an acknowledgement in the product
  17. * documentation would be appreciated but is not required.
  18. * 2. Altered source versions must be plainly marked as such, and must
  19. * not be misrepresented as being the original software.
  20. * 3. This notice may not be removed or altered from any source
  21. * distribution.
  22. *
  23. * This file is part of Stones of Nvwa:
  24. * http://sourceforge.net/projects/nvwa
  25. *
  26. */
  27. /*!
  28. * \file class_level_lock.h
  29. * \ingroup QxMemLeak
  30. *
  31. * In essence Loki ClassLevelLockable re-engineered to use a fast_mutex class.
  32. *
  33. * \version 1.13, 2007/12/30
  34. * \author Wu Yongwei
  35. *
  36. */
  37. #ifndef QT_NO_DEBUG
  38. #ifndef _QX_MODE_RELEASE
  39. #if _QX_USE_MEM_LEAK_DETECTION
  40. #ifndef _CLASS_LEVEL_LOCK_H
  41. #define _CLASS_LEVEL_LOCK_H
  42. #ifdef _MSC_VER
  43. #pragma once
  44. #endif
  45. #include "fast_mutex.h"
  46. namespace qx {
  47. namespace memory {
  48. #ifdef _NOTHREADS
  49. /**
  50. * Helper class for class-level locking. This is the
  51. * single-threaded implementation.
  52. */
  53. template <class _Host, bool _RealLock = false>
  54. class class_level_lock
  55. {
  56. public:
  57. /** Type that provides locking/unlocking semantics. */
  58. class lock
  59. {
  60. public:
  61. lock() {}
  62. };
  63. typedef _Host volatile_type;
  64. };
  65. #else
  66. /**
  67. * Helper class for class-level locking. This is the multi-threaded
  68. * implementation. The main departure from Loki ClassLevelLockable
  69. * is that there is an additional template parameter which can make
  70. * the lock not lock at all even in multi-threaded environments.
  71. * See static_mem_pool.h for real usage.
  72. */
  73. template <class _Host, bool _RealLock = true>
  74. class class_level_lock
  75. {
  76. static fast_mutex _S_mtx;
  77. public:
  78. class lock;
  79. friend class lock;
  80. /** Type that provides locking/unlocking semantics. */
  81. class lock
  82. {
  83. lock(const lock&);
  84. lock& operator=(const lock&);
  85. public:
  86. lock()
  87. {
  88. if (_RealLock)
  89. _S_mtx.lock();
  90. }
  91. ~lock()
  92. {
  93. if (_RealLock)
  94. _S_mtx.unlock();
  95. }
  96. };
  97. typedef volatile _Host volatile_type;
  98. };
  99. #if HAVE_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION
  100. /** Partial specialization that makes null locking. */
  101. template <class _Host>
  102. class class_level_lock<_Host, false>
  103. {
  104. public:
  105. /** Type that provides locking/unlocking semantics. */
  106. class lock
  107. {
  108. public:
  109. lock() {}
  110. };
  111. typedef _Host volatile_type;
  112. };
  113. #endif // HAVE_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION
  114. template <class _Host, bool _RealLock>
  115. fast_mutex class_level_lock<_Host, _RealLock>::_S_mtx;
  116. #endif // _NOTHREADS
  117. } // namespace memory
  118. } // namespace qx
  119. #endif // _CLASS_LEVEL_LOCK_H
  120. #endif // _QX_USE_MEM_LEAK_DETECTION
  121. #endif // _QX_MODE_RELEASE
  122. #endif // QT_NO_DEBUG