summaryrefslogtreecommitdiffstats
path: root/3rdparty/clucene/src/CLucene/util/ThreadLocal.cpp
blob: a54c8691656d3c53b8afee66c81aa1bb9e8b1693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*------------------------------------------------------------------------------
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
* 
* Distributable under the terms of either the Apache License (Version 2.0) or 
* the GNU Lesser General Public License, as specified in the COPYING file.
------------------------------------------------------------------------------*/
#include "CLucene/StdHeader.h"
#include "CLucene/LuceneThreads.h"
#include "ThreadLocal.h"

CL_NS_DEF(util)

DEFINE_MUTEX(ThreadLocalBase::ThreadLocalBase_THIS_LOCK)

ThreadLocalBase::ShutdownHooksType ThreadLocalBase::shutdownHooks(false);
ThreadLocalBase::ThreadLocalsType ThreadLocalBase::threadLocals(false,false);

ThreadLocalBase::ThreadLocalBase(){
}
ThreadLocalBase::~ThreadLocalBase(){
}

void ThreadLocalBase::UnregisterCurrentThread(){
	_LUCENE_THREADID_TYPE id = _LUCENE_CURRTHREADID;
	SCOPED_LOCK_MUTEX(ThreadLocalBase_THIS_LOCK)
	
	ThreadLocalsType::iterator itr = threadLocals.lower_bound(id);
	ThreadLocalsType::iterator end = threadLocals.upper_bound(id);
	while ( itr != end ){
		itr->second->setNull();
		++itr;
	}
}
void ThreadLocalBase::shutdown(){
	SCOPED_LOCK_MUTEX(ThreadLocalBase_THIS_LOCK)
	
	ThreadLocalsType::iterator itr = threadLocals.begin();
	while ( itr != threadLocals.end() ){
		itr->second->setNull();
		++itr;
	}

	ShutdownHooksType::iterator itr2 = shutdownHooks.begin();
	while ( itr2 != shutdownHooks.end() ){
		ShutdownHook* hook = *itr2;
		hook(false);
	}
}
void ThreadLocalBase::registerShutdownHook(ShutdownHook* hook){
	SCOPED_LOCK_MUTEX(ThreadLocalBase_THIS_LOCK)
	shutdownHooks.insert(hook);
}


CL_NS_END