00001 // distlock.h 00002 00003 /* Copyright 2009 10gen Inc. 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #pragma once 00019 00020 #include "../pch.h" 00021 #include "dbclient.h" 00022 #include "connpool.h" 00023 #include "redef_macros.h" 00024 #include "syncclusterconnection.h" 00025 00026 namespace mongo { 00027 00036 class DistributedLock { 00037 public: 00038 00047 DistributedLock( const ConnectionString& conn , const string& name , unsigned takeoverMinutes = 15 ); 00048 00057 bool lock_try( string why , BSONObj * other = 0 ); 00058 00062 void unlock(); 00063 00064 private: 00065 ConnectionString _conn; 00066 string _name; 00067 unsigned _takeoverMinutes; 00068 00069 string _ns; 00070 BSONObj _id; 00071 }; 00072 00073 class dist_lock_try { 00074 public: 00075 dist_lock_try( DistributedLock * lock , string why ) 00076 : _lock(lock) { 00077 _got = _lock->lock_try( why , &_other ); 00078 } 00079 00080 ~dist_lock_try() { 00081 if ( _got ) { 00082 _lock->unlock(); 00083 } 00084 } 00085 00086 bool got() const { return _got; } 00087 BSONObj other() const { return _other; } 00088 00089 private: 00090 DistributedLock * _lock; 00091 bool _got; 00092 BSONObj _other; 00093 }; 00094 00095 } 00096