CLucene - a full-featured, c++ search engine
API Documentation
00001 /*------------------------------------------------------------------------------ 00002 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team 00003 * 00004 * Distributable under the terms of either the Apache License (Version 2.0) or 00005 * the GNU Lesser General Public License, as specified in the COPYING file. 00006 ------------------------------------------------------------------------------*/ 00007 #ifndef _lucene_store_Lock_ 00008 #define _lucene_store_Lock_ 00009 00010 #if defined(_LUCENE_PRAGMA_ONCE) 00011 # pragma once 00012 #endif 00013 00014 CL_NS_DEF(store) 00015 class LuceneLock: LUCENE_BASE{ 00016 public: 00017 LUCENE_STATIC_CONSTANT(int64_t, LOCK_POLL_INTERVAL = 1000); 00018 00023 virtual bool obtain() = 0; 00024 00032 bool obtain(int64_t lockWaitTimeout); 00033 00034 // Release exclusive access. 00035 virtual void release() = 0; 00036 00039 virtual bool isLocked() = 0; 00040 00041 virtual ~LuceneLock() 00042 { 00043 } 00044 00045 virtual TCHAR* toString() = 0; 00046 }; 00047 00048 00049 // Utility class for executing code with exclusive access. 00050 template<typename T> 00051 class LuceneLockWith { 00052 private: 00053 LuceneLock* lock; 00054 int64_t lockWaitTimeout; 00055 00056 protected: 00057 // Code to execute with exclusive access. 00058 virtual T doBody() = 0; 00059 00060 // Constructs an executor that will grab the named lock. 00061 public: 00066 LuceneLockWith(LuceneLock* lock, int64_t lockWaitTimeout) { 00067 this->lock = lock; 00068 this->lockWaitTimeout = lockWaitTimeout; 00069 } 00070 virtual ~LuceneLockWith(){ 00071 } 00072 00077 T runAndReturn() { 00078 bool locked = false; 00079 T ret = NULL; 00080 try { 00081 locked = lock->obtain(lockWaitTimeout); 00082 ret = doBody(); 00083 }_CLFINALLY( 00084 if (locked) 00085 lock->release(); 00086 ); 00087 return ret; 00088 } 00089 00094 void run() { 00095 bool locked = false; 00096 try { 00097 locked = lock->obtain(lockWaitTimeout); 00098 doBody(); 00099 }_CLFINALLY( 00100 if (locked) 00101 lock->release(); 00102 ); 00103 } 00104 }; 00105 00106 CL_NS_END 00107 #endif