~ubuntu-branches/ubuntu/raring/clucene-core/raring-proposed

« back to all changes in this revision

Viewing changes to src/CLucene/store/Lock.h

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-08-11 09:33:38 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120811093338-fgrx41ftqew3qt6a
Tags: 2.3.3.4-1
* New upstream release (Closes: #661703).
* Convert package to multiarch.
* Drop obsolete patches:
  - 01_add_missing_include_bug505667.diff
  - 02_posixness_fix_bug530308.diff
* Add patches:
  - Fixing_ZLIB_configuration_in_shared_CMakeLists.patch
  - Fix-pkgconfig-file-by-adding-clucene-shared-library.patch
  - Install-contribs-lib.patch
  - multiarch.patch
* Update debian/compat: bump to 8.
* Update debian/control:
  - update build dependencies (add cmake, libboost-dev and libz-dev).
  - bump Standards-Version to 3.9.3.
  - rename packages due to ABI bump: libclucene0ldbl -> libclucene-core1.
  - add libclucene-contribs1 package.
* Update debian/rules:
  - rewrite to use CMake.
  - add multiarch support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*------------------------------------------------------------------------------
2
 
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3
 
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_
9
 
 
10
 
#if defined(_LUCENE_PRAGMA_ONCE)
11
 
# pragma once
12
 
#endif
13
 
 
14
 
CL_NS_DEF(store)
15
 
  class LuceneLock: LUCENE_BASE{
16
 
  public:
17
 
      LUCENE_STATIC_CONSTANT(int64_t, LOCK_POLL_INTERVAL = 1000);
18
 
 
19
 
      /** Attempts to obtain exclusive access and immediately return
20
 
      *  upon success or failure.
21
 
      * @return true iff exclusive access is obtained
22
 
      */
23
 
      virtual bool obtain() = 0;
24
 
 
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
31
 
      */
32
 
      bool obtain(int64_t lockWaitTimeout);
33
 
 
34
 
      // Release exclusive access.
35
 
      virtual void release() = 0;
36
 
 
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;
40
 
 
41
 
      virtual ~LuceneLock()
42
 
      {
43
 
      }
44
 
      
45
 
      virtual TCHAR* toString() = 0;
46
 
  };
47
 
 
48
 
 
49
 
  // Utility class for executing code with exclusive access.
50
 
  template<typename T>
51
 
  class LuceneLockWith {
52
 
  private:
53
 
    LuceneLock* lock;
54
 
    int64_t lockWaitTimeout;
55
 
 
56
 
  protected:
57
 
    // Code to execute with exclusive access.
58
 
    virtual T doBody() = 0;
59
 
 
60
 
  // Constructs an executor that will grab the named lock.
61
 
  public:   
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.
65
 
     */
66
 
    LuceneLockWith(LuceneLock* lock, int64_t lockWaitTimeout) {
67
 
      this->lock = lock;
68
 
      this->lockWaitTimeout = lockWaitTimeout;
69
 
    }
70
 
    virtual ~LuceneLockWith(){
71
 
        } 
72
 
 
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. */
77
 
    T runAndReturn() {
78
 
        bool locked = false;
79
 
        T ret = NULL;
80
 
        try {
81
 
            locked = lock->obtain(lockWaitTimeout);
82
 
            ret = doBody();
83
 
        }_CLFINALLY(
84
 
            if (locked) 
85
 
                lock->release();
86
 
        );
87
 
                return ret;
88
 
    }
89
 
 
90
 
        /** @see runAndReturn
91
 
     * Same as runAndReturn, except doesn't return any value.
92
 
         * The only difference is that no void values are used
93
 
         */
94
 
        void run() {
95
 
        bool locked = false;
96
 
        try {
97
 
            locked = lock->obtain(lockWaitTimeout);
98
 
            doBody();
99
 
        }_CLFINALLY(
100
 
            if (locked) 
101
 
                lock->release();
102
 
        );
103
 
    }
104
 
  };
105
 
 
106
 
CL_NS_END
107
 
#endif