00001 // -*- C++ -*- 00002 00003 //========================================================================== 00004 /** 00005 * @file Reverse_Lock_T.h 00006 * 00007 * Reverse_Lock_T.h,v 4.4 2006/03/15 16:46:21 olli Exp 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 #if defined (ACE_LYNXOS_MAJOR) && (ACE_LYNXOS_MAJOR < 4) 00054 using namespace ACE_Acquire_Method; 00055 #endif 00056 00057 /** 00058 * @class ACE_Reverse_Lock 00059 * 00060 * @brief A reverse (or anti) lock. 00061 * 00062 * This is an interesting adapter class that changes a lock into 00063 * a reverse lock, i.e., <acquire> on this class calls <release> 00064 * on the lock, and <release> on this class calls <acquire> on 00065 * the lock. 00066 * One motivation for this class is when we temporarily want to 00067 * release a lock (which we have already acquired) but then 00068 * reacquire it soon after. An alternative design would be to 00069 * add a Anti_Guard or Reverse_Guard class which would <release> 00070 * on construction and <acquire> destruction. However, there 00071 * are *many* varieties of the Guard class and this design 00072 * choice would lead to at least 6 new classes. One new 00073 * ACE_Reverse_Lock class seemed more reasonable. 00074 */ 00075 template <class ACE_LOCKING_MECHANISM> 00076 class ACE_Reverse_Lock : public ACE_Lock 00077 { 00078 public: 00079 00080 typedef ACE_LOCKING_MECHANISM ACE_LOCK; 00081 00082 // = Initialization/Finalization methods. 00083 00084 /// Constructor. All locking requests will be forwarded to <lock>. 00085 #if defined (ACE_LYNXOS_MAJOR) && (ACE_LYNXOS_MAJOR < 4) 00086 // Make LynxOS 3.x buggy compiler happy 00087 ACE_Reverse_Lock (ACE_LOCKING_MECHANISM &lock, 00088 METHOD_TYPE acquire_method = ACE_REGULAR); 00089 #else 00090 ACE_Reverse_Lock (ACE_LOCKING_MECHANISM &lock, 00091 ACE_Acquire_Method::METHOD_TYPE acquire_method = ACE_Acquire_Method::ACE_REGULAR); 00092 #endif 00093 00094 /// Destructor. If <lock_> was not passed in by the user, it will be 00095 /// deleted. 00096 virtual ~ACE_Reverse_Lock (void); 00097 00098 // = Lock accessors. 00099 /// Release the lock. 00100 virtual int acquire (void); 00101 00102 /// Release the lock. 00103 virtual int tryacquire (void); 00104 00105 /// Acquire the lock. 00106 virtual int release (void); 00107 00108 /// Release the lock. 00109 virtual int acquire_read (void); 00110 00111 /// Release the lock. 00112 virtual int acquire_write (void); 00113 00114 /// Release the lock. 00115 virtual int tryacquire_read (void); 00116 00117 /// Release the lock. 00118 virtual int tryacquire_write (void); 00119 00120 /// Release the lock. 00121 virtual int tryacquire_write_upgrade (void); 00122 00123 /// Explicitly destroy the lock. 00124 virtual int remove (void); 00125 00126 private: 00127 /// The concrete locking mechanism that all the methods delegate to. 00128 ACE_LOCKING_MECHANISM &lock_; 00129 00130 /// This indicates what kind of acquire method will be called. 00131 ACE_Acquire_Method::METHOD_TYPE acquire_method_; 00132 }; 00133 00134 ACE_END_VERSIONED_NAMESPACE_DECL 00135 00136 #if defined (__ACE_INLINE__) 00137 #include "ace/Reverse_Lock_T.inl" 00138 #endif /* __ACE_INLINE__ */ 00139 00140 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) 00141 #include "ace/Reverse_Lock_T.cpp" 00142 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ 00143 00144 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00145 #pragma implementation ("Reverse_Lock_T.cpp") 00146 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00147 00148 #include /**/ "ace/post.h" 00149 #endif /* ACE_REVERSE_LOCK_T_H */