OpenWalnut 1.2.5
WSharedObjectTicket.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 WSHAREDOBJECTTICKET_H
00026 #define WSHAREDOBJECTTICKET_H
00027 
00028 #include <boost/shared_ptr.hpp>
00029 
00030 #include "WCondition.h"
00031 
00032 // The shared object class
00033 template < typename T >
00034 class WSharedObject;
00035 
00036 /**
00037  * 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
00038  * has been destroyed.
00039  *
00040  * \note This class does not provide any member to actually get the contained value/instance. This is done in read and write tickets.
00041  */
00042 template < typename Data >
00043 class WSharedObjectTicket
00044 {
00045 // the shared object class needs protected access to create new instances
00046 friend class WSharedObject< Data >;
00047 public:
00048 
00049     /**
00050      * Destroys the ticket and releases the lock.
00051      */
00052     virtual ~WSharedObjectTicket()
00053     {
00054         // NOTE: the derived destructor already unlocks.
00055         if( m_condition )
00056         {
00057             m_condition->notify();
00058         }
00059     };
00060 
00061     /**
00062      * If called, the unlock will NOT fire the condition. This is useful in some situations if you find out "hey there actually was nothing
00063      * changed".
00064      */
00065     void suppressUnlockCondition()
00066     {
00067         m_condition = boost::shared_ptr< WCondition >();
00068     }
00069 
00070 protected:
00071 
00072     /**
00073      * Create a new instance. It is protected to avoid someone to create them. It locks the mutex.
00074      *
00075      * \param data the data to protect
00076      * \param mutex the mutex used to lock
00077      * \param condition a condition that should be fired upon unlock. Can be NULL.
00078      */
00079     WSharedObjectTicket( Data& data, boost::shared_ptr< boost::shared_mutex > mutex, boost::shared_ptr< WCondition > condition ): // NOLINT
00080         m_data( data ),
00081         m_mutex( mutex ),
00082         m_condition( condition )
00083     {
00084     };
00085 
00086     /**
00087      * The data to which access is allowed by the ticket
00088      */
00089     Data& m_data;
00090 
00091     /**
00092      * The mutex used for locking.
00093      */
00094     boost::shared_ptr< boost::shared_mutex > m_mutex;
00095 
00096     /**
00097      * A condition which gets notified after unlocking. Especially useful to notify waiting threads about a change in the object.
00098      */
00099     boost::shared_ptr< WCondition > m_condition;
00100 
00101     /**
00102      * Unlocks the mutex.
00103      */
00104     virtual void unlock() = 0;
00105 
00106 private:
00107 };
00108 
00109 #endif  // WSHAREDOBJECTTICKET_H
00110 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends