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.3 2010/06/15 17:26:00 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, upon destruction, releases a read-write lock held for read. 00127 * 00128 * This template class is an analog of auto_ptr<T> from the C++ standard 00129 * template library, but rather than freeing a heap object, it releases a 00130 * lock (thread-synchronization primitive, not a database lock) 00131 * by calling the lock's "release_read" method. This only works if 00132 * the resource has a method 00133 * \code 00134 * void release_read(); 00135 * \endcode 00136 * 00137 * \sa auto_release_w_t<> 00138 */ 00139 template<class T> 00140 class auto_release_r_t { 00141 public: 00142 NORET auto_release_r_t(T& t) 00143 : obj(t) { } 00144 NORET ~auto_release_r_t() { 00145 obj.release_read(); 00146 } 00147 private: 00148 T& obj; 00149 }; 00150 00151 /**\brief Template class that, upon destruction, releases a read-write lock held for write. 00152 * 00153 * This template class is an analog of auto_ptr<T> from the C++ standard 00154 * template library, but rather than freeing a heap object, it releases a 00155 * lock (thread-synchronization primitive, not a database lock) 00156 * by calling the lock's "release_write" method. This only works if 00157 * the resource has a method 00158 * \code 00159 * void release_write(); 00160 * \endcode 00161 * 00162 * \sa auto_release_r_t<> 00163 */ 00164 template<class T> 00165 class auto_release_w_t 00166 { 00167 public: 00168 NORET auto_release_w_t(T& t) 00169 : obj(t) { } 00170 NORET ~auto_release_w_t() { 00171 obj.release_write(); 00172 } 00173 private: 00174 T& obj; 00175 }; 00176 00177 00178 /*<std-footer incl-file-exclusion='AUTO_RELEASE_H'> -- do not edit anything below this line -- */ 00179 00180 #endif /*</std-footer>*/