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

Loading...
Searching...
No Matches
CloseableThreadLocal.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 CLOSEABLETHREADLOCAL_H
8#define CLOSEABLETHREADLOCAL_H
9
10#include "LuceneThread.h"
11
12 namespace Lucene {
13
15 template <typename TYPE>
16 class CloseableThreadLocal : public LuceneObject {
17 public:
18 typedef boost::shared_ptr<TYPE> localDataPtr;
19 typedef Map<int64_t, localDataPtr> MapLocalData;
20
21 CloseableThreadLocal() {
22 localData = MapLocalData::newInstance();
23 }
24
25 public:
26 localDataPtr get() {
27 SyncLock syncLock(this);
28 typename MapLocalData::iterator local = localData.find(LuceneThread::currentId());
29 if (local != localData.end()) {
30 return local->second;
31 }
32 localDataPtr initial(initialValue());
33 if (initial) {
34 localData.put(LuceneThread::currentId(), initial);
35 }
36 return initial;
37 }
38
39 void set(const localDataPtr& data) {
40 SyncLock syncLock(this);
41 localData.put(LuceneThread::currentId(), data);
42 }
43
44 void close() {
45 SyncLock syncLock(this);
46 localData.remove(LuceneThread::currentId());
47 }
48
49 protected:
50 MapLocalData localData;
51
52 virtual localDataPtr initialValue() {
53 return localDataPtr(); // override
54 }
55};
56
57}
58
59#endif
LuceneThread.h
Lucene::CloseableThreadLocal::initialValue
virtual localDataPtr initialValue()
Definition CloseableThreadLocal.h:52
Lucene::CloseableThreadLocal::MapLocalData
Map< int64_t, localDataPtr > MapLocalData
Definition CloseableThreadLocal.h:19
Lucene::CloseableThreadLocal::localData
MapLocalData localData
Definition CloseableThreadLocal.h:50
Lucene::CloseableThreadLocal::localDataPtr
boost::shared_ptr< TYPE > localDataPtr
Definition CloseableThreadLocal.h:18
Lucene::CloseableThreadLocal::CloseableThreadLocal
CloseableThreadLocal()
Definition CloseableThreadLocal.h:21
Lucene::CloseableThreadLocal::close
void close()
Definition CloseableThreadLocal.h:44
Lucene::CloseableThreadLocal::get
localDataPtr get()
Definition CloseableThreadLocal.h:26
Lucene::CloseableThreadLocal::set
void set(const localDataPtr &data)
Definition CloseableThreadLocal.h:39
Lucene::LuceneObject::LuceneObject
LuceneObject()
Lucene::LuceneThread::currentId
static int64_t currentId()
Return representation of current execution thread.
Lucene::Map
Utility template class to handle maps that can be safely copied and shared.
Definition Map.h:17
Lucene::Map< int64_t, localDataPtr >::newInstance
static this_type newInstance()
Definition Map.h:34
Lucene::Map< int64_t, localDataPtr >::iterator
map_type::iterator iterator
Definition Map.h:22
Lucene::SyncLock
Utility class to support scope locking.
Definition Synchronize.h:46
Lucene
Definition AbstractAllTermDocs.h:12

clucene.sourceforge.net