RCFProto

- Main Page
- Classes
- Files

- File List

All Classes Functions Typedefs
ThreadLocalData.hpp
1
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18
19 #ifndef INCLUDE_RCF_THREADLOCALDATA_HPP
20 #define INCLUDE_RCF_THREADLOCALDATA_HPP
21
22 #include <boost/shared_ptr.hpp>
23
24 #include <RCF/ByteBuffer.hpp>
25 #include <RCF/Export.hpp>
26 #include <RCF/RecursionLimiter.hpp>
27 #include <RCF/ThreadLibrary.hpp>
28
29 #ifdef BOOST_WINDOWS
30 #include <WinSock2.h>
31 #endif
32
33 #ifndef BOOST_WINDOWS
34 struct iovec;
35 #endif
36
37 namespace RCF {
38
39 class ObjectCache;
40 class ClientStub;
41 class RcfSession;
42 class ThreadInfo;
43 class UdpSessionState;
44 class I_Future;
45 class AmiNotification;
46 class OverlappedAmi;
47 class LogBuffers;
48 class Filter;
49 class FileUpload;
50
51 typedef boost::shared_ptr<ClientStub> ClientStubPtr;
52 typedef boost::shared_ptr<RcfSession> RcfSessionPtr;
53 typedef boost::shared_ptr<ThreadInfo> ThreadInfoPtr;
54 typedef boost::shared_ptr<UdpSessionState> UdpSessionStatePtr;
55 typedef boost::shared_ptr<OverlappedAmi> OverlappedAmiPtr;
56 typedef boost::shared_ptr<LogBuffers> LogBuffersPtr;
57 typedef boost::function1<void, RcfSession&> RcfSessionCallback;
58 typedef boost::shared_ptr<Filter> FilterPtr;
59
60 #ifndef BOOST_WINDOWS
61 typedef iovec WSABUF;
62 #endif
63
64 class ThreadLocalData;
65 ThreadLocalData & getThreadLocalData();
66
67 RCF_EXPORT void clearThreadLocalDataForThisThread();
68
69 RCF_EXPORT ClientStub * getTlsClientStubPtr();
70
71 RCF_EXPORT void pushTlsClientStub(
72 ClientStub * pClientStub);
73
74 RCF_EXPORT void popTlsClientStub();
75
76 RCF_EXPORT RcfSession * getCurrentRcfSessionPtr();
77 RCF_EXPORT RcfSession * getTlsRcfSessionPtr();
78
79 RCF_EXPORT void setTlsRcfSessionPtr(
80 RcfSession * pRcfSession = NULL);
81
82 RCF_EXPORT ThreadInfoPtr getTlsThreadInfoPtr();
83
84 RCF_EXPORT void setTlsThreadInfoPtr(
85 ThreadInfoPtr threadInfoPtr);
86
87 RCF_EXPORT UdpSessionStatePtr getTlsUdpSessionStatePtr();
88
89 RCF_EXPORT void setTlsUdpSessionStatePtr(
90 UdpSessionStatePtr udpSessionStatePtr);
91
92 RCF_EXPORT RcfSession & getCurrentRcfSession();
93 RCF_EXPORT RcfSession & getTlsRcfSession();
94
95 RecursionState<int, int> & getTlsRcfSessionRecursionState();
96
97 RCF_EXPORT AmiNotification & getTlsAmiNotification();
98
99 RCF_EXPORT LogBuffers & getTlsLogBuffers();
100
101 RCF_EXPORT std::vector< std::vector<RCF::ByteBuffer> * > &
102 getTlsCache(std::vector<RCF::ByteBuffer> *);
103
104 RCF_EXPORT std::vector< std::vector<int> * > &
105 getTlsCache(std::vector<int> *);
106
107 RCF_EXPORT std::vector< std::vector<WSABUF> * > &
108 getTlsCache(std::vector<WSABUF> *);
109
110 RCF_EXPORT std::vector< std::vector<FilterPtr> * > &
111 getTlsCache(std::vector<FilterPtr> *);
112
113 RCF_EXPORT std::vector< std::vector<RcfSessionCallback> * > &
114 getTlsCache(std::vector<RcfSessionCallback> *);
115
116 RCF_EXPORT std::vector< std::vector<FileUpload> * > &
117 getTlsCache(std::vector<FileUpload> *);
118
119 template<typename T>
120 class ThreadLocalCached
121 {
122 public:
123
124 ThreadLocalCached() : mpt(NULL)
125 {
126 std::vector<T *> & tlsCache = getTlsCache( (T *) NULL);
127 if (tlsCache.empty())
128 {
129 mpt = new T();
130 }
131 else
132 {
133 mpt = tlsCache.back();
134 tlsCache.pop_back();
135 }
136 RCF_ASSERT(mpt->empty());
137 }
138
139 ~ThreadLocalCached()
140 {
141 mpt->clear();
142 std::vector<T *> & tlsCache = getTlsCache( (T *) NULL);
143 tlsCache.push_back(mpt);
144 mpt = NULL;
145 }
146
147 T & get()
148 {
149 return *mpt;
150 }
151
152 private:
153
154 T * mpt;
155 };
156
157 void RCF_EXPORT addThreadExitHandler(boost::function<void()> func);
158
159 } // namespace RCF
160
161 #endif // ! INCLUDE_RCF_THREADLOCALDATA_HPP