library of assembled shared sources

http://lass.cocamware.com

rw_lock.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_RW_LOCK_H
00044 #define LASS_GUARDIAN_OF_INCLUSION_UTIL_RW_LOCK_H
00045 
00046 #include "util_common.h"
00047 #include "atomic.h"
00048 #include "thread.h" 
00049 
00050 namespace lass
00051 {
00052 namespace util
00053 {
00054 
00055 namespace impl
00056 {
00057 } //namespace impl
00058 
00059 
00060 /** Lean and mean synchronisation object, without OS support.
00061 *   @ingroup Threading
00062 *   @see RWLocker
00063 *   @author Tom De Muer
00064 *   @date 2006
00065 *   
00066 *   This lock is built upon atomic operations that are programmed in assembly.  It makes a difference
00067 *   between reading and writing.  A maximum number of readers is allowed while only 1 writer is allowed.
00068 *   The writer will only enter if there are no readers are reading at the same moment.  This RWLock is 
00069 *   a blocking and spinning synchronization object.  Priority is given over writers, as soon as a writer is
00070 *   trying to enter, only one subsequent reader will in worst case be able to enter.
00071 */
00072 
00073 class RWLock : NonCopyable
00074 {
00075 
00076 public:
00077     RWLock(util::CallTraits<size_t>::TParam iMaxReaders);
00078     ~RWLock();
00079 
00080     void lockr();
00081     void lockw();
00082     void unlockr();
00083     void unlockw();
00084 
00085     const LockResult tryLockr();
00086     const LockResult tryLockw();
00087 
00088 private:
00089     size_t maxReaders_;     /**< the maximum of simultaneous readers allowed */
00090     int spinLock_;
00091     int writersTrying_;     /**< the number of writers trying to enter */
00092 };
00093 
00094 
00095 RWLock::RWLock(util::CallTraits<size_t>::TParam iMaxReaders)
00096 {
00097     maxReaders_ = iMaxReaders;
00098     spinLock_ = maxReaders_;
00099     writersTrying_ = 0;
00100 }
00101 
00102 RWLock::~RWLock()
00103 {
00104     LASS_ENFORCE(spinLock_==maxReaders_);
00105 }
00106 
00107 void RWLock::lockr()
00108 {
00109     int newSpinLock;
00110     int oldSpinLock;
00111     do
00112     {
00113         oldSpinLock = spinLock_;
00114         LASS_ASSERT(oldSpinLock>=0);
00115         newSpinLock = oldSpinLock-1;
00116     } while (writersTrying_!=0 || oldSpinLock==0 || !lass::util::atomicCompareAndSwap(spinLock_,oldSpinLock,newSpinLock));
00117 }
00118 
00119 void RWLock::lockw()
00120 {
00121     lass::util::atomicIncrement(writersTrying_);
00122     do
00123     {
00124     } while (!lass::util::atomicCompareAndSwap<int>(spinLock_,maxReaders_,0));
00125 }
00126 
00127 void RWLock::unlockw()
00128 {
00129     LASS_ENFORCE(spinLock_==0);
00130     do
00131     {
00132     } while (!lass::util::atomicCompareAndSwap<int>(spinLock_,0,maxReaders_));
00133     lass::util::atomicDecrement(writersTrying_);
00134 }
00135 
00136 void RWLock::unlockr()
00137 {
00138     lass::util::atomicIncrement(spinLock_);
00139     LASS_ASSERT(spinLock_<=maxReaders_);
00140 }
00141 
00142 
00143 const LockResult RWLock::tryLockr()
00144 {
00145     int oldSpinLock;
00146     int newSpinLock;
00147     do
00148     {
00149         oldSpinLock = spinLock_;
00150         LASS_ASSERT(spinLock_ >= 0);
00151         if (spinLock_ == 0)
00152         {
00153             return lockBusy;
00154         }
00155         newSpinLock = oldSpinLock - 1;
00156     }
00157     while (!atomicCompareAndSwap(spinLock_, oldSpinLock, newSpinLock));
00158     return lockSuccess;
00159 }
00160 
00161 
00162 const LockResult RWLock::tryLockw()
00163 {
00164     lass::util::atomicIncrement(writersTrying_);
00165     do
00166     {
00167         LASS_ASSERT(spinLock_ >= 0);
00168         if (spinLock_ != maxReaders_)
00169         {
00170             lass::util::atomicDecrement(writersTrying_);
00171             return lockBusy;
00172         }
00173     }
00174     while (!atomicCompareAndSwap<int>(spinLock_, maxReaders_, 0));
00175     return lockSuccess;
00176 }
00177 
00178 } //namespace util
00179 } //namespace lass
00180 
00181 
00182 #endif
00183 
00184 // EOF

Generated on Mon Nov 10 14:21:21 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo