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
15 namespace Lucene {
16
18 class LPPAPI Synchronize {
19 public:
20 Synchronize();
21 virtual ~Synchronize();
22
23 protected:
24 boost::recursive_timed_mutex mutexSynchronize;
25 int64_t lockThread;
26 int32_t recursionCount;
27
28 public:
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
46 class LPPAPI SyncLock {
47 public:
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
58 protected:
59 SynchronizePtr sync;
60
61 protected:
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
Lucene::SyncLock
Utility class to support scope locking.
Definition Synchronize.h:46
Lucene::SyncLock::SyncLock
SyncLock(OBJECT object, int32_t timeout=0)
Definition Synchronize.h:51
Lucene::SyncLock::~SyncLock
virtual ~SyncLock()
Lucene::SyncLock::SyncLock
SyncLock(const SynchronizePtr &sync, int32_t timeout=0)
Lucene::SyncLock::sync
SynchronizePtr sync
Definition Synchronize.h:59
Lucene::SyncLock::lock
void lock(int32_t timeout)
Lucene::Synchronize
Utility class to support locking via a mutex.
Definition Synchronize.h:18
Lucene::Synchronize::unlock
void unlock()
Unlock mutex.
Lucene::Synchronize::lockThread
int64_t lockThread
Definition Synchronize.h:25
Lucene::Synchronize::createSync
static void createSync(SynchronizePtr &sync)
create a new Synchronize instance atomically.
Lucene::Synchronize::mutexSynchronize
boost::recursive_timed_mutex mutexSynchronize
Definition Synchronize.h:24
Lucene::Synchronize::recursionCount
int32_t recursionCount
Definition Synchronize.h:26
Lucene::Synchronize::holdsLock
bool holdsLock()
Returns true if mutex is currently locked by current thread.
Lucene::Synchronize::Synchronize
Synchronize()
Lucene::Synchronize::lock
void lock(int32_t timeout=0)
Lock mutex using an optional timeout.
Lucene::Synchronize::~Synchronize
virtual ~Synchronize()
Lucene::Synchronize::unlockAll
int32_t unlockAll()
Unlock all recursive mutex.
Lucene
Definition AbstractAllTermDocs.h:12
Lucene::SynchronizePtr
boost::shared_ptr< Synchronize > SynchronizePtr
Definition LuceneTypes.h:552

clucene.sourceforge.net