WSharedObjectTicketRead.h

00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WSHAREDOBJECTTICKETREAD_H
00026 #define WSHAREDOBJECTTICKETREAD_H
00027 
00028 #include <boost/shared_ptr.hpp>
00029 
00030 #include "WCondition.h"
00031 #include "WSharedObjectTicket.h"
00032 
00033 /**
00034  * Class which represents granted access to a locked object. It contains a reference to the object and a lock. The lock is freed after the ticket
00035  * has been destroyed.  This class especially implements the shared (read) lock.
00036  */
00037 template < typename Data >
00038 class WSharedObjectTicketRead: public WSharedObjectTicket< Data >
00039 {
00040 // the shared object class needs protected access to create new instances
00041 friend class WSharedObject< Data >;
00042 public:
00043 
00044     /**
00045      * Destroys the ticket and releases the lock.
00046      */
00047     virtual ~WSharedObjectTicketRead()
00048     {
00049         // explicitly unlock to ensure the WSharedObjectTicket destructor can call the update callback AFTER the lock has been released
00050         unlock();
00051     };
00052 
00053     /**
00054      * Returns the protected data. As long as you own the ticket, you are allowed to use it.
00055      *
00056      * \note making it const enforces const correctness for contained types!
00057      *
00058      * \return the data (const!)
00059      */
00060     const Data& get() const
00061     {
00062         return WSharedObjectTicket< Data >::m_data;
00063     };
00064 
00065 protected:
00066 
00067     /**
00068      * Create a new instance. It is protected to avoid someone to create them. It locks the mutex for read.
00069      *
00070      * \param data the data to protect
00071      * \param mutex the mutex used to lock
00072      * \param condition a condition that should be fired upon unlock. Can be NULL.
00073      */
00074     WSharedObjectTicketRead( Data& data, boost::shared_ptr< boost::shared_mutex > mutex, boost::shared_ptr< WCondition > condition ): // NOLINT
00075         WSharedObjectTicket< Data >( data, mutex, condition ),
00076         m_lock( boost::shared_lock< boost::shared_mutex >( *mutex ) )
00077     {
00078     };
00079 
00080     /**
00081      * The lock.
00082      */
00083     boost::shared_lock< boost::shared_mutex > m_lock;
00084 
00085     /**
00086      * Unlocks the mutex.
00087      */
00088     virtual void unlock()
00089     {
00090         m_lock.unlock();
00091     }
00092 
00093 private:
00094 };
00095 
00096 #endif  // WSHAREDOBJECTTICKETREAD_H
00097 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends