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

CloseableThreadLocal.h
Go to the documentation of this file.
1 // 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
Lucene::CloseableThreadLocal::CloseableThreadLocal
CloseableThreadLocal()
Definition: CloseableThreadLocal.h:21
LuceneThread.h
Lucene::Map< int64_t, localDataPtr >::iterator
map_type::iterator iterator
Definition: Map.h:22
Lucene::Map::remove
void remove(ITER pos)
Definition: Map.h:97
Lucene::Map::find
iterator find(const KEY &key)
Definition: Map.h:110
Lucene::Map< int64_t, localDataPtr >
Lucene::Map< int64_t, localDataPtr >::newInstance
static this_type newInstance()
Definition: Map.h:34
Lucene::Map::end
iterator end()
Definition: Map.h:60
Lucene::Map::put
void put(const KEY &key, const VALUE &value)
Definition: Map.h:85
Lucene::CloseableThreadLocal::close
void close()
Definition: CloseableThreadLocal.h:44
Lucene::CloseableThreadLocal::localDataPtr
boost::shared_ptr< TYPE > localDataPtr
Definition: CloseableThreadLocal.h:18
Lucene::CloseableThreadLocal::localData
MapLocalData localData
Definition: CloseableThreadLocal.h:50
Lucene::SyncLock
Utility class to support scope locking.
Definition: Synchronize.h:44
Lucene::LuceneObject
Base class for all Lucene classes.
Definition: LuceneObject.h:31
Lucene::CloseableThreadLocal::MapLocalData
Map< int64_t, localDataPtr > MapLocalData
Definition: CloseableThreadLocal.h:19
Lucene
Definition: AbstractAllTermDocs.h:12
Lucene::CloseableThreadLocal::initialValue
virtual localDataPtr initialValue()
Definition: CloseableThreadLocal.h:52
Lucene::CloseableThreadLocal
General purpose thread-local map.
Definition: CloseableThreadLocal.h:16
Lucene::LuceneThread::currentId
static int64_t currentId()
Return representation of current execution thread.

clucene.sourceforge.net