RWLocker.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 Novell, Inc. All rights reserved.
00004 *
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 *
00008 *  - Redistributions of source code must retain the above copyright notice,
00009 *    this list of conditions and the following disclaimer.
00010 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 *******************************************************************************/
00031 
00032 
00038 #ifndef BLOCXX_RWLOCKER_HPP_INCLUDE_GUARD_
00039 #define BLOCXX_RWLOCKER_HPP_INCLUDE_GUARD_
00040 #include "blocxx/BLOCXX_config.h"
00041 #include "blocxx/NonRecursiveMutexLock.hpp"
00042 #include "blocxx/Condition.hpp"
00043 #include "blocxx/Exception.hpp"
00044 #include "blocxx/Array.hpp"
00045 
00046 namespace BLOCXX_NAMESPACE
00047 {
00048 
00049 BLOCXX_DECLARE_APIEXCEPTION(RWLocker, BLOCXX_COMMON_API);
00051 class BLOCXX_COMMON_API RWLocker
00052 {
00053 public:
00054    RWLocker();
00055    ~RWLocker();
00056    void getReadLock(UInt32 sTimeout, UInt32 usTimeout=0);
00057    void getWriteLock(UInt32 sTimeout, UInt32 usTimeout=0);
00058    void releaseReadLock();
00059    void releaseWriteLock();
00060 private:
00061    void doWakeups();
00062    Condition   m_waiting_writers;
00063    Condition   m_waiting_readers;
00064    
00065    int         m_num_waiting_writers;
00066    int         m_num_waiting_readers;
00067    int         m_readers_next;
00068    
00069    NonRecursiveMutex m_guard;
00070    // -1 means writer has lock.  0 means no one has the lock. 
00071    // > 0 means readers have the lock.
00072    int m_state;
00073 
00074 #ifdef BLOCXX_WIN32
00075 #pragma warning (push)
00076 #pragma warning (disable: 4251)
00077 #endif
00078 
00079    Array<Thread_t> m_readers;
00080 
00081 #ifdef BLOCXX_WIN32
00082 #pragma warning (pop)
00083 #endif
00084 
00085    Thread_t m_writer;
00086 };
00088 class BLOCXX_COMMON_API ReadLock
00089 {
00090 public:
00091    ReadLock(RWLocker& locker, UInt32 sTimeout, UInt32 usTimeout=0)
00092       : m_locker(&locker)
00093       , m_released(false)
00094    {
00095       m_locker->getReadLock(sTimeout, usTimeout);
00096    }
00097    ~ReadLock()
00098    {
00099       release();
00100    }
00101    void lock(UInt32 sTimeout, UInt32 usTimeout=0)
00102    {
00103       if (m_released)
00104       {
00105          m_locker->getReadLock(sTimeout, usTimeout);
00106          m_released = false;
00107       }
00108    }
00109    void release()
00110    {
00111       if (!m_released)
00112       {
00113          m_locker->releaseReadLock();
00114          m_released = true;
00115       }
00116    }
00117 private:
00118    RWLocker* m_locker;
00119    bool m_released;
00120    // noncopyable
00121    ReadLock(const ReadLock&);
00122    ReadLock& operator=(const ReadLock&);
00123 };
00125 class BLOCXX_COMMON_API WriteLock
00126 {
00127 public:
00128    WriteLock(RWLocker& locker, UInt32 sTimeout, UInt32 usTimeout=0)
00129       : m_locker(&locker)
00130       , m_released(false)
00131    {
00132       m_locker->getWriteLock(sTimeout, usTimeout);
00133    }
00134    ~WriteLock()
00135    {
00136       release();
00137    }
00138    void lock(UInt32 sTimeout, UInt32 usTimeout=0)
00139    {
00140       if (m_released)
00141       {
00142          m_locker->getWriteLock(sTimeout, usTimeout);
00143          m_released = false;
00144       }
00145    }
00146    void release()
00147    {
00148       if (!m_released)
00149       {
00150          m_locker->releaseWriteLock();
00151          m_released = true;
00152       }
00153    }
00154 private:
00155    RWLocker* m_locker;
00156    bool m_released;
00157    
00158    // noncopyable
00159    WriteLock(const WriteLock&);
00160    WriteLock& operator=(const WriteLock&);
00161 };
00162 
00163 } // end namespace BLOCXX_NAMESPACE
00164 
00165 #endif

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6