MboLockedVar.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 * Copyright (c) 2001 - 2008 Marcus Boerger.  All rights reserved.
00003 *
00004 * This library is free software; you can redistribute it and/or
00005 * modify it under the terms of the GNU Lesser General Public
00006 * License as published by the Free Software Foundation; either
00007 * version 2.1 of the License, or (at your option) any later version.
00008 *
00009 * This library is distributed in the hope that it will be useful,
00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 * Lesser General Public License for more details.
00013 *
00014 * You should have received a copy of the GNU Lesser General Public
00015 * License along with this library; if not, write to the Free Software
00016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00017 * ========================================================================= */
00018 
00019 /* ------------------------------------------------------------------------ */
00020 /* Name:      MboLockedVar.h
00021  *
00022  * Requires:  
00023  * - Mbo.h
00024  * - MboSynch.h / MboSynch.cpp
00025  */
00040 /* ------------------------------------------------------------------------ */
00041 
00042 #ifndef _MBOLOCKEDVAR_H_
00043 #define _MBOLOCKEDVAR_H_
00044 
00045 #include "Mbo.h"
00046 #include "MboSynch.h"
00047 
00048 namespace mbo {
00049 
00058 template <class _Ty, class _THREAD_MODEL = ThreadModel>
00059 class LockedVar
00060 {
00061 private:
00062 
00064     typedef typename _THREAD_MODEL::CriticalSection CriticalSection;
00065 
00066     mutable CriticalSection  m_mx; /* lock to use */
00067 
00068 protected:
00069 
00070     _Ty  m_val; 
00072 public:
00073 
00076     inline LockedVar()
00077         : m_val()
00078     {
00079     }
00080 
00084     inline LockedVar(_IN const _Ty& tIn)
00085         : m_val(tIn)
00086     {
00087     }
00088 
00094     inline operator _Ty ()
00095     {
00096         Lock lock(&m_mx);
00097         return m_val;
00098     }
00099 
00105     inline operator const _Ty () const
00106     {
00107         Lock lock(&m_mx);
00108         return m_val;
00109     }
00110 
00113     inline _Ty& operator*()
00114     {
00115         Lock lock(&m_mx);
00116         return m_val;
00117     }
00118 
00121     inline const _Ty& operator*() const
00122     {
00123         Lock lock(&m_mx);
00124         return m_val;
00125     }
00126 
00133     inline const _Ty& operator= (_IN const _Ty& tIn)
00134     {
00135         Lock lock(&m_mx);
00136         m_val = tIn;
00137         return tIn; /* tIn not t */
00138     }
00139 
00145     inline _Ty __exchange(_IN const _Ty& tIn)
00146     {
00147         Lock lock(&m_mx);
00148         T tOld(m_val);
00149         m_val = tIn;
00150         return tOld; /* the old value */
00151     }
00152 
00155     inline mbo::CLock __getLock() const
00156     {
00157         return mbo::CLockSingle(&m_mx);
00158     }
00159 };
00160 
00165 template <>
00166 class LockedVar<bool>
00167 {
00168 private:
00169 
00171     typedef ThreadModel::CriticalSection  CriticalSection;
00172     
00173     mutable CriticalSection  m_mx; /* lock to use */
00174 
00175 protected:
00176 
00177     typedef bool _Ty;
00178 
00179     bool m_val; 
00181 public:
00182 
00185     inline LockedVar()
00186         : m_val(false)
00187     {
00188     }
00189 
00193     inline explicit LockedVar(_IN const _Ty tIn)
00194         : m_val(tIn)
00195     {
00196     }
00197 
00203     inline operator _Ty ()
00204     {
00205         return m_val;
00206     }
00207 
00213     inline operator const _Ty () const
00214     {
00215         return m_val;
00216     }
00217 
00220     inline _Ty& operator*()
00221     {
00222         return m_val;
00223     }
00224 
00227     inline const _Ty& operator*() const
00228     {
00229         return m_val;
00230     }
00231 
00238     inline const _Ty& operator= (_IN const _Ty& tIn)
00239     {
00240         m_val = tIn;
00241         return tIn; /* tIn not t */
00242     }
00243 
00249     inline _Ty __exchange(_IN const _Ty tIn)
00250     {
00251         _Ty tOld(m_val);
00252         m_val = tIn;
00253         return tOld; /* the old value */
00254     }
00255 
00258     inline mbo::CLock __getLock() const
00259     {
00260         return mbo::CLockSingle(&m_mx);
00261     }
00262 };
00263 
00264 }; // namespace mbo
00265 
00266 #endif // _MBOLOCKEDVAR_H_

  Hosted on code.google.com  
© Marcus Börger
Generated on Fri Jan 18 21:21:08 2008 for MBO-lib by doxygen 1.5.4