1//
2// ThreadLocal.cpp
3//
4// Library: Foundation
5// Package: Threading
6// Module: Thread
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/ThreadLocal.h"
16#include "Poco/SingletonHolder.h"
17#include "Poco/Thread.h"
18
19
20namespace Poco {
21
22
23TLSAbstractSlot::TLSAbstractSlot()
24{
25}
26
27
28TLSAbstractSlot::~TLSAbstractSlot()
29{
30}
31
32
33ThreadLocalStorage::ThreadLocalStorage()
34{
35}
36
37
38ThreadLocalStorage::~ThreadLocalStorage()
39{
40 for (TLSMap::iterator it = _map.begin(); it != _map.end(); ++it)
41 {
42 delete it->second;
43 }
44}
45
46
47TLSAbstractSlot*& ThreadLocalStorage::get(const void* key)
48{
49 TLSMap::iterator it = _map.find(key);
50 if (it == _map.end())
51 return _map.insert(TLSMap::value_type(key, reinterpret_cast<Poco::TLSAbstractSlot*>(0))).first->second;
52 else
53 return it->second;
54}
55
56
57namespace
58{
59 static SingletonHolder<ThreadLocalStorage> sh;
60}
61
62
63ThreadLocalStorage& ThreadLocalStorage::current()
64{
65 Thread* pThread = Thread::current();
66 if (pThread)
67 {
68 return pThread->tls();
69 }
70 else
71 {
72 return *sh.get();
73 }
74}
75
76
77void ThreadLocalStorage::clear()
78{
79 Thread* pThread = Thread::current();
80 if (pThread)
81 pThread->clearTLS();
82}
83
84
85} // namespace Poco
86