Reverse_Lock_T.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //==========================================================================
00004 /**
00005  *  @file    Reverse_Lock_T.h
00006  *
00007  *  $Id: Reverse_Lock_T.h 80826 2008-03-04 14:51:23Z wotte $
00008  *
00009  *   Moved from Synch.h.
00010  *
00011  *  @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
00012  */
00013 //==========================================================================
00014 
00015 #ifndef ACE_REVERSE_LOCK_T_H
00016 #define ACE_REVERSE_LOCK_T_H
00017 #include /**/ "ace/pre.h"
00018 
00019 #include "ace/Lock.h"
00020 
00021 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00022 # pragma once
00023 #endif /* ACE_LACKS_PRAGMA_ONCE */
00024 
00025 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00026 
00027 /**
00028  * @namespace ACE_Acquire_Method
00029  *
00030  * @brief An enum namespace.
00031  *
00032  * These enums should have been inside the reverse lock class, but
00033  * some lame compilers cannot handle enums inside template classes.
00034  *
00035  * The METHOD_TYPE is used to indicate which acquire() method will be
00036  * called on the real lock when the release() method is called on the
00037  * reverse lock. REGULAR indicated the acquire() method, READ
00038  * indicates the acquire_read() method, and WRITE indicates the
00039  * acquire_write() method.  Note that the try_*() methods are not
00040  * represented here because we have to make sure that the release()
00041  * method on the reverse lock acquires a lock on the real lock.
00042  **/
00043 namespace ACE_Acquire_Method
00044 {
00045   enum METHOD_TYPE
00046   {
00047     ACE_REGULAR,
00048     ACE_READ,
00049     ACE_WRITE
00050   };
00051 }
00052 
00053 /**
00054  * @class ACE_Reverse_Lock
00055  *
00056  * @brief A reverse (or anti) lock.
00057  *
00058  * This is an interesting adapter class that changes a lock into
00059  * a reverse lock, i.e., <acquire> on this class calls <release>
00060  * on the lock, and <release> on this class calls <acquire> on
00061  * the lock.
00062  * One motivation for this class is when we temporarily want to
00063  * release a lock (which we have already acquired) but then
00064  * reacquire it soon after.  An alternative design would be to
00065  * add a Anti_Guard or Reverse_Guard class which would <release>
00066  * on construction and <acquire> destruction.  However, there
00067  * are *many* varieties of the Guard class and this design
00068  * choice would lead to at least 6 new classes.  One new
00069  * ACE_Reverse_Lock class seemed more reasonable.
00070  */
00071 template <class ACE_LOCKING_MECHANISM>
00072 class ACE_Reverse_Lock : public ACE_Lock
00073 {
00074 public:
00075 
00076   typedef ACE_LOCKING_MECHANISM ACE_LOCK;
00077 
00078   // = Initialization/Finalization methods.
00079 
00080   /// Constructor. All locking requests will be forwarded to <lock>.
00081   ACE_Reverse_Lock (ACE_LOCKING_MECHANISM &lock,
00082                     ACE_Acquire_Method::METHOD_TYPE acquire_method = ACE_Acquire_Method::ACE_REGULAR);
00083 
00084   /// Destructor. If <lock_> was not passed in by the user, it will be
00085   /// deleted.
00086   virtual ~ACE_Reverse_Lock (void);
00087 
00088   // = Lock accessors.
00089   /// Release the lock.
00090   virtual int acquire (void);
00091 
00092   /// Release the lock.
00093   virtual int tryacquire (void);
00094 
00095   /// Acquire the lock.
00096   virtual int release (void);
00097 
00098   /// Release the lock.
00099   virtual int acquire_read (void);
00100 
00101   /// Release the lock.
00102   virtual int acquire_write (void);
00103 
00104   /// Release the lock.
00105   virtual int tryacquire_read (void);
00106 
00107   /// Release the lock.
00108   virtual int tryacquire_write (void);
00109 
00110   /// Release the lock.
00111   virtual int tryacquire_write_upgrade (void);
00112 
00113   /// Explicitly destroy the lock.
00114   virtual int remove (void);
00115 
00116 private:
00117   /// The concrete locking mechanism that all the methods delegate to.
00118   ACE_LOCKING_MECHANISM &lock_;
00119 
00120   /// This indicates what kind of acquire method will be called.
00121   ACE_Acquire_Method::METHOD_TYPE acquire_method_;
00122 };
00123 
00124 ACE_END_VERSIONED_NAMESPACE_DECL
00125 
00126 #if defined (__ACE_INLINE__)
00127 #include "ace/Reverse_Lock_T.inl"
00128 #endif /* __ACE_INLINE__ */
00129 
00130 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00131 #include "ace/Reverse_Lock_T.cpp"
00132 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
00133 
00134 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00135 #pragma implementation ("Reverse_Lock_T.cpp")
00136 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00137 
00138 #include /**/ "ace/post.h"
00139 #endif /* ACE_REVERSE_LOCK_T_H */

Generated on Tue Feb 2 17:18:42 2010 for ACE by  doxygen 1.4.7