summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/clucene/src/CLucene/store/Lock.h
blob: b3b790494ba58b7d22a84f90d11b79893569e32f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
 *
 * Distributable under the terms of either the Apache License (Version 2.0) or 
 * the GNU Lesser General Public License, as specified in the COPYING file.
 *
 * Changes are Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies), all rights reserved.
*/
#ifndef _lucene_store_Lock_
#define _lucene_store_Lock_

#if defined(_LUCENE_PRAGMA_ONCE)
# pragma once
#endif

CL_NS_DEF(store)

class LuceneLock : LUCENE_BASE
{
public:
    LUCENE_STATIC_CONSTANT(int64_t, LOCK_POLL_INTERVAL = 1000);

    virtual ~LuceneLock() {}

    // Attempts to obtain exclusive access and immediately return upon success
    // or failure. Return true if exclusive access is obtained.
    virtual bool obtain() = 0;

    // Attempts to obtain an exclusive lock within amount of time given.
    // Currently polls once per second until lockWaitTimeout is passed.
    // @param lockWaitTimeout length of time to wait in ms
    // @return true if lock was obtained
    // @throws IOException if lock wait times out or obtain() throws an IOException
    bool obtain(int64_t lockWaitTimeout);

    // Release exclusive access.
    virtual void release() = 0;

    // Returns true if the resource is currently locked.  Note that one must
    // still call {@link #obtain()} before using the resource.
    virtual bool isLocked() = 0;

    virtual QString toString() const = 0;
};


// Utility class for executing code with exclusive access.
template<typename T>
class LuceneLockWith
{
public:   
    // Constructs an executor that will grab the named lock. Defaults
    // lockWaitTimeout to LUCENE_COMMIT_LOCK_TIMEOUT.
    // @deprecated Kept only to avoid breaking existing code.
    LuceneLockWith(LuceneLock* lock, int64_t lockWaitTimeout)
    {
        this->lock = lock;
        this->lockWaitTimeout = lockWaitTimeout;
    }

    virtual ~LuceneLockWith() {} 

    // Calls {@link #doBody} while <i>lock</i> is obtained. Blocks if lock
    // cannot be obtained immediately.  Retries to obtain lock once per second
    // until it is obtained, or until it has tried ten times. Lock is released
    // when {@link #doBody} exits.
    T runAndReturn()
    {
        bool locked = false;
        T ret = NULL;
        try {
            locked = lock->obtain(lockWaitTimeout);
            ret = doBody();
        } _CLFINALLY (
            if (locked) 
                lock->release();
        );
        return ret;
    }

    // @see runAndReturn
    // Same as runAndReturn, except doesn't return any value. The only
    // difference is that no void values are used
    void run()
    {
        bool locked = false;
        try {
            locked = lock->obtain(lockWaitTimeout);
            doBody();
        } _CLFINALLY (
            if (locked) 
                lock->release();
        );
    }

protected:
    virtual T doBody() = 0;

private:
    LuceneLock* lock;
    int64_t lockWaitTimeout;
};

CL_NS_END

#endif