00001 /* -*- mode:C++; c-basic-offset:4 -*- 00002 Shore-MT -- Multi-threaded port of the SHORE storage manager 00003 00004 Copyright (c) 2007-2009 00005 Data Intensive Applications and Systems Labaratory (DIAS) 00006 Ecole Polytechnique Federale de Lausanne 00007 00008 All Rights Reserved. 00009 00010 Permission to use, copy, modify and distribute this software and 00011 its documentation is hereby granted, provided that both the 00012 copyright notice and this permission notice appear in all copies of 00013 the software, derivative works or modified versions, and any 00014 portions thereof, and that both notices appear in supporting 00015 documentation. 00016 00017 This code is distributed in the hope that it will be useful, but 00018 WITHOUT ANY WARRANTY; without even the implied warranty of 00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS 00020 DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER 00021 RESULTING FROM THE USE OF THIS SOFTWARE. 00022 */ 00023 00024 /*<std-header orig-src='shore' incl-file-exclusion='AUTO_RELEASE_H'> 00025 00026 $Id: auto_release.h,v 1.4 2010/12/08 17:37:50 nhall Exp $ 00027 00028 SHORE -- Scalable Heterogeneous Object REpository 00029 00030 Copyright (c) 1994-99 Computer Sciences Department, University of 00031 Wisconsin -- Madison 00032 All Rights Reserved. 00033 00034 Permission to use, copy, modify and distribute this software and its 00035 documentation is hereby granted, provided that both the copyright 00036 notice and this permission notice appear in all copies of the 00037 software, derivative works or modified versions, and any portions 00038 thereof, and that both notices appear in supporting documentation. 00039 00040 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY 00041 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS 00042 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND 00043 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 00044 00045 This software was developed with support by the Advanced Research 00046 Project Agency, ARPA order number 018 (formerly 8230), monitored by 00047 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518. 00048 Further funding for this work was provided by DARPA through 00049 Rome Research Laboratory Contract No. F30602-97-2-0247. 00050 00051 */ 00052 00053 #ifndef AUTO_RELEASE_H 00054 #define AUTO_RELEASE_H 00055 00056 #include "w_defines.h" 00057 00058 /* -- do not edit anything above this line -- </std-header>*/ 00059 00060 /**\brief Template class that releases a held resource upon destruction. 00061 * 00062 * This template class is an analog of auto_ptr<T> from the C++ standard 00063 * template library, but rather than freeing a heap object, it releases a 00064 * resource by calling the resource's "release" method. This only works if 00065 * the resource has a method 00066 * \code 00067 * void release(); 00068 * \endcode 00069 * 00070 * For more complex releasing requirements, see the specializations 00071 * auto_release_t<queue_based_lock_t>, 00072 * auto_release_t<pthread_mutex_t>, and 00073 * the analogous templates for read-write synchronization primitives 00074 * auto_release_r_t<>, and 00075 * auto_release_w_t<> 00076 * 00077 * Used in the storage manager by buffer manager and io layer. 00078 */ 00079 template <class T> class auto_release_t { 00080 public: 00081 NORET auto_release_t(T& t) 00082 : obj(t) {}; 00083 NORET ~auto_release_t() { 00084 obj.release(); 00085 } 00086 private: 00087 T& obj; 00088 }; 00089 00090 00091 /**\brief Template class that releases a held queue_based_lock_t upon destruction. 00092 * 00093 * \sa auto_release_t<> 00094 */ 00095 template<> 00096 class auto_release_t<w_pthread_lock_t> 00097 { 00098 public: 00099 /// construct with a reference to the lock and a pointer to the Queue-node upon which this thread spins 00100 NORET auto_release_t(w_pthread_lock_t& t, w_pthread_lock_t::ext_qnode* me) 00101 : obj(t), _me(me) { } 00102 NORET ~auto_release_t() { 00103 obj.release(_me); 00104 } 00105 private: 00106 w_pthread_lock_t& obj; 00107 w_pthread_lock_t::ext_qnode* _me; 00108 }; 00109 00110 /**\brief Template class that releases a held pthread mutex upon destruction. 00111 * 00112 * \sa auto_release_t<> 00113 */ 00114 template<> 00115 class auto_release_t<pthread_mutex_t> 00116 { 00117 public: 00118 /// construct with a pointer to the mutex 00119 NORET auto_release_t(pthread_mutex_t* t) : obj(t) {} 00120 void exit() { if(obj) pthread_mutex_unlock(obj); obj=NULL; } 00121 NORET ~auto_release_t() { exit(); } 00122 private: 00123 pthread_mutex_t* obj; 00124 }; 00125 00126 /**\brief Template class that releases a held latch upon destruction. 00127 * 00128 * \sa auto_release_t<> 00129 */ 00130 template<> 00131 class auto_release_t<latch_t> 00132 { 00133 public: 00134 /// construct with a pointer to the mutex 00135 NORET auto_release_t(latch_t* t) : obj(t) {} 00136 void exit() { if(obj) obj->latch_release(); obj=NULL; } 00137 NORET ~auto_release_t() { exit(); } 00138 private: 00139 latch_t* obj; 00140 }; 00141 00142 /**\brief Template class that, upon destruction, releases a read-write lock held for read. 00143 * 00144 * This template class is an analog of auto_ptr<T> from the C++ standard 00145 * template library, but rather than freeing a heap object, it releases a 00146 * lock (thread-synchronization primitive, not a database lock) 00147 * by calling the lock's "release_read" method. This only works if 00148 * the resource has a method 00149 * \code 00150 * void release_read(); 00151 * \endcode 00152 * 00153 * \sa auto_release_w_t<> 00154 */ 00155 template<class T> 00156 class auto_release_r_t { 00157 public: 00158 NORET auto_release_r_t(T& t) 00159 : _disabled(false), _obj(t) { } 00160 NORET ~auto_release_r_t() { 00161 release_disable(); 00162 } 00163 void release_disable() { 00164 if(!_disabled) _obj.release_read(); 00165 _disabled=true; } 00166 void disable() { _disabled=true; } 00167 private: 00168 bool _disabled; 00169 T& _obj; 00170 }; 00171 00172 /**\brief Template class that, upon destruction, releases a read-write lock held for write. 00173 * 00174 * This template class is an analog of auto_ptr<T> from the C++ standard 00175 * template library, but rather than freeing a heap object, it releases a 00176 * lock (thread-synchronization primitive, not a database lock) 00177 * by calling the lock's "release_write" method. This only works if 00178 * the resource has a method 00179 * \code 00180 * void release_write(); 00181 * \endcode 00182 * 00183 * \sa auto_release_r_t<> 00184 */ 00185 template<class T> 00186 class auto_release_w_t 00187 { 00188 public: 00189 NORET auto_release_w_t(T& t) 00190 : _disabled(false), _obj(t) { } 00191 NORET ~auto_release_w_t() { 00192 release_disable(); 00193 } 00194 void release_disable() { 00195 if(!_disabled) _obj.release_write(); 00196 _disabled=true; } 00197 void disable() { _disabled=true; } 00198 private: 00199 bool _disabled; 00200 T& _obj; 00201 }; 00202 00203 00204 /*<std-footer incl-file-exclusion='AUTO_RELEASE_H'> -- do not edit anything below this line -- */ 00205 00206 #endif /*</std-footer>*/