Lucene++ - a full-featured, c++ search engine
API Documentation


Loading...
Searching...
No Matches
Synchronize.h
Go to the documentation of this file.
1
2// Copyright (c) 2009-2014 Alan Wright. All rights reserved.
3// Distributable under the terms of either the Apache License (Version 2.0)
4// or the GNU Lesser General Public License.
6
7#ifndef SYNCHRONIZE_H
8#define SYNCHRONIZE_H
9
10#include <atomic>
11#include <boost/thread/recursive_mutex.hpp>
12#include <boost/thread/mutex.hpp>
13#include "Lucene.h"
14
15namespace Lucene {
16
18class LPPAPI Synchronize {
19public:
21 virtual ~Synchronize();
22
23protected:
24 boost::recursive_timed_mutex mutexSynchronize;
25 int64_t lockThread;
27
28public:
30 static void createSync(SynchronizePtr& sync);
31
33 void lock(int32_t timeout = 0);
34
36 void unlock();
37
39 int32_t unlockAll();
40
42 bool holdsLock();
43};
44
46class LPPAPI SyncLock {
47public:
48 SyncLock(const SynchronizePtr& sync, int32_t timeout = 0);
49
50 template <class OBJECT>
51 SyncLock(OBJECT object, int32_t timeout = 0) {
52 this->sync = object->getSync();
53 lock(timeout);
54 }
55
56 virtual ~SyncLock();
57
58protected:
60
61protected:
62 void lock(int32_t timeout);
63};
64
65
66#define LUCENE_RUN_ONCE(Command) \
67 do { \
68 static std::atomic<bool> RUN_ONCE_hasRun = {}; \
69 if (!RUN_ONCE_hasRun) { \
70 static boost::mutex RUN_ONCE_mutex; \
71 boost::mutex::scoped_lock RUN_ONCE_lock(RUN_ONCE_mutex); \
72 if (!RUN_ONCE_hasRun) { \
73 Command; \
74 RUN_ONCE_hasRun = true; \
75 } \
76 } \
77 } while(0)
78
79
80}
81
82#endif
Utility class to support scope locking.
Definition Synchronize.h:46
SyncLock(OBJECT object, int32_t timeout=0)
Definition Synchronize.h:51
virtual ~SyncLock()
SyncLock(const SynchronizePtr &sync, int32_t timeout=0)
SynchronizePtr sync
Definition Synchronize.h:59
void lock(int32_t timeout)
Utility class to support locking via a mutex.
Definition Synchronize.h:18
void unlock()
Unlock mutex.
int64_t lockThread
Definition Synchronize.h:25
static void createSync(SynchronizePtr &sync)
create a new Synchronize instance atomically.
boost::recursive_timed_mutex mutexSynchronize
Definition Synchronize.h:24
int32_t recursionCount
Definition Synchronize.h:26
bool holdsLock()
Returns true if mutex is currently locked by current thread.
void lock(int32_t timeout=0)
Lock mutex using an optional timeout.
virtual ~Synchronize()
int32_t unlockAll()
Unlock all recursive mutex.
Definition AbstractAllTermDocs.h:12
boost::shared_ptr< Synchronize > SynchronizePtr
Definition LuceneTypes.h:552

clucene.sourceforge.net