1
/*------------------------------------------------------------------------------
2
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
4
* Distributable under the terms of either the Apache License (Version 2.0) or
5
* the GNU Lesser General Public License, as specified in the COPYING file.
6
------------------------------------------------------------------------------*/
7
#ifndef _lucene_store_Lock_
8
#define _lucene_store_Lock_
10
#if defined(_LUCENE_PRAGMA_ONCE)
15
class LuceneLock: LUCENE_BASE{
17
LUCENE_STATIC_CONSTANT(int64_t, LOCK_POLL_INTERVAL = 1000);
19
/** Attempts to obtain exclusive access and immediately return
20
* upon success or failure.
21
* @return true iff exclusive access is obtained
23
virtual bool obtain() = 0;
25
/** Attempts to obtain an exclusive lock within amount
26
* of time given. Currently polls once per second until
27
* lockWaitTimeout is passed.
28
* @param lockWaitTimeout length of time to wait in ms
29
* @return true if lock was obtained
30
* @throws IOException if lock wait times out or obtain() throws an IOException
32
bool obtain(int64_t lockWaitTimeout);
34
// Release exclusive access.
35
virtual void release() = 0;
37
/** Returns true if the resource is currently locked. Note that one must
38
* still call {@link #obtain()} before using the resource. */
39
virtual bool isLocked() = 0;
45
virtual TCHAR* toString() = 0;
49
// Utility class for executing code with exclusive access.
51
class LuceneLockWith {
54
int64_t lockWaitTimeout;
57
// Code to execute with exclusive access.
58
virtual T doBody() = 0;
60
// Constructs an executor that will grab the named lock.
62
/** Constructs an executor that will grab the named lock.
63
* Defaults lockWaitTimeout to LUCENE_COMMIT_LOCK_TIMEOUT.
64
* @deprecated Kept only to avoid breaking existing code.
66
LuceneLockWith(LuceneLock* lock, int64_t lockWaitTimeout) {
68
this->lockWaitTimeout = lockWaitTimeout;
70
virtual ~LuceneLockWith(){
73
/** Calls {@link #doBody} while <i>lock</i> is obtained. Blocks if lock
74
* cannot be obtained immediately. Retries to obtain lock once per second
75
* until it is obtained, or until it has tried ten times. Lock is released when
76
* {@link #doBody} exits. */
81
locked = lock->obtain(lockWaitTimeout);
91
* Same as runAndReturn, except doesn't return any value.
92
* The only difference is that no void values are used
97
locked = lock->obtain(lockWaitTimeout);