store_latch_manager.cpp

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='STID_T_H'>
00025 
00026  $Id: store_latch_manager.cpp,v 1.2 2010/05/26 01:20:12 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 
00054 #include "w_defines.h"
00055 
00056 /*  -- do not edit anything above this line --   </std-header>*/
00057 
00058 #include "store_latch_manager.h"
00059 #include <vector>
00060 
00061 // This business is to allow us to switch from one kind of
00062 // lock to another with more ease.
00063 #if defined(USE_OCC_LOCK_HERE)
00064 #define OCCWRITE .write_lock()
00065 #define OCCREAD .read_lock()
00066 #else
00067 #define OCCWRITE 
00068 #define OCCREAD 
00069 #endif
00070 
00071 void store_latch_manager::shutdown()
00072 {
00073     CRITICAL_SECTION(cs, _latch_lock OCCWRITE);
00074     for(latch_map::iterator it=_latches.begin(); it != _latches.end(); ++it) 
00075     {
00076         w_assert1(it->second->mode() == LATCH_NL);
00077         delete it->second;
00078     }
00079     _latches.clear();
00080 }
00081 
00082 latch_t &store_latch_manager::find_latch(stid_t const &store) 
00083 {
00084     latch_t *latch=NULL;
00085     {
00086         CRITICAL_SECTION(cs, _latch_lock OCCREAD);
00087         latch_map::iterator pos=_latches.find(store);
00088         if(pos != _latches.end()) {
00089             stid_t xxx = pos->first;
00090             w_assert1(store==xxx);
00091             latch=pos->second;
00092             return *latch; 
00093         }
00094     }
00095     
00096     // not there... need to add a new entry (but verify first!)
00097     CRITICAL_SECTION(cs, _latch_lock OCCWRITE);
00098     if( (latch=_latches[store]) )   {
00099         return *latch; // somebody else beat us to it
00100     }
00101 
00102     latch = new latch_t;
00103     _latches[store] = latch;
00104     return *latch;
00105 }
00106 
00107 // do the work -- assumes I already have the _latch_lock in write mode
00108 void store_latch_manager::_destroy_latches(stid_t const &store) 
00109 {
00110     latch_map::iterator pos=_latches.find(store);
00111     if(pos != _latches.end())
00112     {
00113         w_assert1(store == pos->first);
00114         latch_t *latch = pos->second;
00115         _latches.erase(pos);
00116         delete latch;
00117     }
00118     // not there... someone beat us to it.
00119 }
00120 
00121 void store_latch_manager::destroy_latches(stid_t const &store) 
00122 {
00123     CRITICAL_SECTION(cs, _latch_lock OCCWRITE);
00124     _destroy_latches(store);
00125 }
00126 
00127 // This is called when the volume is dismounted
00128 void store_latch_manager::destroy_latches(vid_t const &vol) 
00129 {
00130     unsigned int i=0;
00131     std::vector<stid_t> _tmp;
00132     _tmp.reserve(30); // probably more than sufficient most of the time
00133 
00134     CRITICAL_SECTION(cs, _latch_lock OCCWRITE);
00135     // collect all the stids for the volume
00136     {
00137         for(
00138             latch_map::iterator pos=_latches.begin();
00139             pos != _latches.end();
00140             pos++
00141         )
00142         {
00143             stid_t stid = pos->first;
00144             if(stid.vol == vol)  { 
00145                 _tmp.push_back(stid);
00146             }
00147         }
00148     }
00149     // Must free the read lock before acquiring the write lock
00150     // in the destroy_latches() below
00151     for(i=0; i < _tmp.size(); i++)
00152     {
00153         _destroy_latches(_tmp[i]);
00154     }
00155 }
00156 
00157 store_latch_manager::~store_latch_manager() 
00158 {
00159     shutdown();
00160 }

Generated on Wed Jul 7 17:22:32 2010 for Shore Storage Manager by  doxygen 1.4.7