class_level_lock.h

Go to the documentation of this file.
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-2007 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 acknowledgement 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 
00039 #ifndef _CLASS_LEVEL_LOCK_H
00040 #define _CLASS_LEVEL_LOCK_H
00041 
00042 #include "fast_mutex.h"
00043 
00044 # ifdef _NOTHREADS
00045 
00049     template <class _Host, bool _RealLock = false>
00050     class class_level_lock
00051     {
00052     public:
00054         class lock
00055         {
00056         public:
00057             lock() {}
00058         };
00059 
00060         typedef _Host volatile_type;
00061     };
00062 # else
00063 
00070     template <class _Host, bool _RealLock = true>
00071     class class_level_lock
00072     {
00073         static fast_mutex _S_mtx;
00074 
00075     public:
00076         class lock;
00077         friend class lock;
00078 
00080         class lock
00081         {
00082             lock(const lock&);
00083             lock& operator=(const lock&);
00084         public:
00085             lock()
00086             {
00087                 if (_RealLock)
00088                     _S_mtx.lock();
00089             }
00090             ~lock()
00091             {
00092                 if (_RealLock)
00093                     _S_mtx.unlock();
00094             }
00095         };
00096 
00097         typedef volatile _Host volatile_type;
00098     };
00099 
00100 #   if HAVE_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION
00101 
00102     template <class _Host>
00103     class class_level_lock<_Host, false>
00104     {
00105     public:
00107         class lock
00108         {
00109         public:
00110             lock() {}
00111         };
00112 
00113         typedef _Host volatile_type;
00114     };
00115 #   endif // HAVE_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION
00116 
00117     template <class _Host, bool _RealLock>
00118     fast_mutex class_level_lock<_Host, _RealLock>::_S_mtx;
00119 # endif // _NOTHREADS
00120 
00121 #endif // _CLASS_LEVEL_LOCK_H

Generated on Mon Dec 31 15:07:24 2007 for Nvwa by  doxygen 1.5.1