00001 // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- 00002 // vim:tabstop=4:shiftwidth=4:expandtab: 00003 00004 /* 00005 * Copyright (C) 2004 Wu Yongwei <adah at users dot sourceforge dot net> 00006 * 00007 * This software is provided 'as-is', without any express or implied 00008 * warranty. In no event will the authors be held liable for any 00009 * damages arising from the use of this software. 00010 * 00011 * Permission is granted to anyone to use this software for any purpose, 00012 * including commercial applications, and to alter it and redistribute 00013 * it freely, subject to the following restrictions: 00014 * 00015 * 1. The origin of this software must not be misrepresented; you must 00016 * not claim that you wrote the original software. If you use this 00017 * software in a product, an acknowledgment in the product 00018 * documentation would be appreciated but is not required. 00019 * 2. Altered source versions must be plainly marked as such, and must 00020 * not be misrepresented as being the original software. 00021 * 3. This notice may not be removed or altered from any source 00022 * distribution. 00023 * 00024 * This file is part of Stones of Nvwa: 00025 * http://sourceforge.net/projects/nvwa 00026 * 00027 */ 00028 00042 #ifndef _OBJECT_LEVEL_LOCK_H 00043 #define _OBJECT_LEVEL_LOCK_H 00044 00045 #include "fast_mutex.h" 00046 00047 # ifdef _NOTHREADS 00048 00052 template <class _Host> 00053 class object_level_lock 00054 { 00055 public: 00057 class lock 00058 { 00059 # ifndef NDEBUG 00060 const object_level_lock& _M_host; 00061 # endif 00062 00063 lock(const lock&); 00064 lock& operator=(const lock&); 00065 public: 00066 explicit lock(const object_level_lock& __host) 00067 # ifndef NDEBUG 00068 : _M_host(__host) 00069 # endif 00070 {} 00071 # ifndef NDEBUG 00072 // The purpose of this method is allow one to write code 00073 // like "assert(guard.get_locked_object() == this)" to 00074 // ensure that the locked object is exactly the object being 00075 // accessed. 00076 const object_level_lock* get_locked_object() const 00077 { 00078 return &_M_host; 00079 } 00080 # endif 00081 }; 00082 00083 typedef _Host volatile_type; 00084 }; 00085 # else 00086 00090 template <class _Host> 00091 class object_level_lock 00092 { 00093 mutable fast_mutex _M_mtx; 00094 00095 public: 00096 class lock; 00097 friend class lock; 00098 00100 class lock 00101 { 00102 const object_level_lock& _M_host; 00103 00104 lock(const lock&); 00105 lock& operator=(const lock&); 00106 public: 00107 explicit lock(const object_level_lock& __host) : _M_host(__host) 00108 { 00109 _M_host._M_mtx.lock(); 00110 } 00111 ~lock() 00112 { 00113 _M_host._M_mtx.unlock(); 00114 } 00115 # ifndef NDEBUG 00116 // The purpose of this method is allow one to write code 00117 // like "assert(guard.get_locked_object() == this)" to 00118 // ensure that the locked object is exactly the object being 00119 // accessed. 00120 const object_level_lock* get_locked_object() const 00121 { 00122 return &_M_host; 00123 } 00124 # endif 00125 }; 00126 00127 typedef volatile _Host volatile_type; 00128 }; 00129 # endif // _NOTHREADS 00130 00131 #endif // _OBJECT_LEVEL_LOCK_H
1.5.6