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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/****************************************************************************
* Copyright (C) 2009-2015 EPAM Systems
*
* This file is part of Indigo toolkit.
*
* This file may be distributed and/or modified under the terms of the
* GNU General Public License version 3 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in the
* packaging of this file.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
***************************************************************************/
#ifndef __tlscont_h__
#define __tlscont_h__
#include <typeinfo>
#include "base_c/defs.h"
#include "base_cpp/array.h"
#include "base_cpp/pool.h"
#include "base_cpp/os_sync_wrapper.h"
#include "base_cpp/red_black.h"
#include "base_cpp/ptr_array.h"
#include "base_c/os_tls.h"
#include "base_cpp/auto_ptr.h"
#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable:4251)
#endif
namespace indigo {
// Session identifiers manager.
// Every thread have local session ID that corresponds to the all
// local session variables.
class DLLEXPORT _SIDManager {
public:
static _SIDManager& getInst (void);
_SIDManager (void);
~_SIDManager (void);
void setSessionId (qword id);
qword allocSessionId (void);
qword getSessionId (void);
// Add specified SID to the vacant list.
// This method should be called before thread exit if SID was
// assigned automatically (not by manual TL_SET_SESSION_ID call)
void releaseSessionId (qword id);
DECL_ERROR;
private:
qword * _getID () const;
// Thread local key for storing current session ID
TLS_IDX_TYPE _tlsIdx;
RedBlackSet<qword> _allSIDs;
qword _lastNewSID;
// Array with vacant SIDs
Array<qword> _vacantSIDs;
static _SIDManager _instance;
static OsLock _lock;
};
// Macros for managing session IDs for current thread
#define TL_GET_SESSION_ID() _SIDManager::getInst().getSessionId()
#define TL_SET_SESSION_ID(id) _SIDManager::getInst().setSessionId(id)
#define TL_ALLOC_SESSION_ID() _SIDManager::getInst().allocSessionId()
#define TL_RELEASE_SESSION_ID(id) _SIDManager::getInst().releaseSessionId(id)
// Container that keeps one instance of specifed type per session
template <typename T>
class _SessionLocalContainer {
public:
T& getLocalCopy (void)
{
return getLocalCopy(_SIDManager::getInst().getSessionId());
}
T& getLocalCopy (const qword id)
{
OsLocker locker(_lock.ref());
AutoPtr<T>& ptr = _map.findOrInsert(id);
if (ptr.get() == NULL)
ptr.reset(new T());
return ptr.ref();
}
private:
typedef RedBlackObjMap<qword, AutoPtr<T> > _Map;
_Map _map;
ThreadSafeStaticObj<OsLock> _lock;
};
// Helpful templates to deal with commas in template type names
// to be able to write like
// QS_DEF((std::unordered_map<std::string, int>), atoms_id);
// See http://stackoverflow.com/a/13842784
template<typename T> struct ArgumentType;
template<typename T, typename U> struct ArgumentType<T(U)> { typedef U Type; };
#define _GET_TYPE(t) ArgumentType<void(t)>::Type
// Macros for working with global variables per each session
// By tradition this macros start with TL_, but should start with SL_
#define TL_DECL_EXT(type, name) extern _SessionLocalContainer< _GET_TYPE(type) > TLSCONT_##name
#define TL_DECL(type, name) static _SessionLocalContainer< _GET_TYPE(type) > TLSCONT_##name
#define TL_GET(type, name) _GET_TYPE(type)& name = (TLSCONT_##name).getLocalCopy()
#define TL_DECL_GET(type, name) TL_DECL(type, name); TL_GET(type, name)
#define TL_GET2(type, name, realname) _GET_TYPE(type)& name = (TLSCONT_##realname).getLocalCopy()
#define TL_GET_BY_ID(type, name, id) _GET_TYPE(type)& name = (TLSCONT_##name).getLocalCopy(id)
#define TL_DEF(className, type, name) _SessionLocalContainer< _GET_TYPE(type) > className::TLSCONT_##name
#define TL_DEF_EXT(type, name) _SessionLocalContainer< _GET_TYPE(type) > TLSCONT_##name
// Pool for local variables, reused in consecutive function calls,
// but not required to preserve their state
template <typename T>
class _ReusableVariablesPool {
public:
_ReusableVariablesPool () { is_valid = true; }
~_ReusableVariablesPool () { is_valid = false; }
bool isValid () const { return is_valid; }
T& getVacant (int& idx)
{
OsLocker locker(_lock);
if (_vacant_indices.size() != 0)
{
idx = _vacant_indices.pop();
return *_objects[idx];
}
_objects.add(new T);
idx = _objects.size() - 1;
_vacant_indices.reserve(idx + 1);
return *_objects[idx];
}
void release (int idx)
{
OsLocker locker(_lock);
_vacant_indices.push(idx);
}
T& getByIndex (int idx)
{
return *_objects[idx];
}
private:
OsLock _lock;
bool is_valid;
PtrArray< T > _objects;
Array<int> _vacant_indices;
};
// Utility class for automatically release call
template <typename T>
class _ReusableVariablesAutoRelease {
public:
_ReusableVariablesAutoRelease () : _idx(-1), _var_pool(0) {}
void init (int idx, _ReusableVariablesPool< T > *var_pool)
{
_idx = idx;
_var_pool = var_pool;
}
~_ReusableVariablesAutoRelease (void)
{
if (_var_pool == 0)
return;
// Check if the _var_pool destructor have not been called already
// (this can happen on program exit)
if (_var_pool->isValid())
_var_pool->release(_idx);
}
protected:
int _idx;
_ReusableVariablesPool< T >* _var_pool;
};
// Abstract proxy class to call a destructor for an allocated data
class Destructor
{
public:
virtual void callDestructor (void *data) = 0;
virtual ~Destructor() {};
};
// Proxy destructor class for a type T
template <typename T>
class DestructorT : public Destructor
{
public:
virtual void callDestructor (void *data)
{
((T*)data)->~T();
}
};
// Template function to create proxy destructor
template <typename T>
Destructor *createDestructor (T *t)
{
return new DestructorT<T>();
}
// Variables pool that can reuse objects allocations that are initialized in the same order
class _LocalVariablesPool
{
public:
_LocalVariablesPool ()
{
reset();
}
~_LocalVariablesPool ()
{
for (int i = 0; i < data.size(); i++)
{
destructors[i]->callDestructor(data[i]);
free(data[i]);
}
}
template <typename T>
size_t hash ()
{
return typeid(T).hash_code();
}
template <typename T>
T& getVacant ()
{
data.expandFill(index + 1, 0);
destructors.expand(index + 1);
type_hash.expandFill(index + 1, 0);
if (data[index] == 0)
{
// Allocate data and destructor
data[index] = malloc(sizeof(T));
T *t = new (data[index]) T();
destructors[index] = createDestructor(t);
type_hash[index] = hash<T>();
}
// Class hash check to verify initialization order
if (type_hash[index] != hash<T>())
throw Exception("VariablesPool: invalid initialization order");
T *t = (T*)data[index];
index++;
return *t;
}
void reset ()
{
index = 0;
}
private:
Array<void *> data;
Array<size_t> type_hash;
PtrArray<Destructor> destructors;
int index;
};
// Auto release class that additionally calls reset method for LocalVariablesPool
class _LocalVariablesPoolAutoRelease : public _ReusableVariablesAutoRelease<_LocalVariablesPool>
{
public:
~_LocalVariablesPoolAutoRelease ()
{
if (_var_pool == 0)
return;
if (_var_pool->isValid())
{
_LocalVariablesPool &local_pool = _var_pool->getByIndex(_idx);
local_pool.reset();
}
}
};
}
// "Quasi-static" variable definition
#define QS_DEF(TYPE, name) \
static ThreadSafeStaticObj<_ReusableVariablesPool< _GET_TYPE(TYPE) > > _POOL_##name; \
int _POOL_##name##_idx; \
_GET_TYPE(TYPE) &name = _POOL_##name->getVacant(_POOL_##name##_idx); \
_ReusableVariablesAutoRelease< _GET_TYPE(TYPE) > _POOL_##name##_auto_release; \
_POOL_##name##_auto_release.init(_POOL_##name##_idx, _POOL_##name.ptr())
//
// Reusable class members definition
// By tradition this macros start with TL_, but should start with SL_
// To work with them you should first define commom pool with CP_DECL,
// then define it in the source class with CP_DEF(cls), and initialize
// in the constructor via CP_INIT before any TL_CP_ initializations
//
// Add this to class definition
#define TL_CP_DECL(TYPE, name) \
typedef _GET_TYPE(TYPE) _##name##_TYPE; \
_GET_TYPE(TYPE) &name
// Add this to constructor initialization list
#define TL_CP_GET(name) \
name(_local_pool.getVacant<_##name##_TYPE>())
#define CP_DECL \
_LocalVariablesPoolAutoRelease _local_pool_autorelease; \
static _LocalVariablesPool& _getLocalPool (_LocalVariablesPoolAutoRelease &auto_release); \
_LocalVariablesPool &_local_pool \
#define CP_INIT _local_pool(_getLocalPool(_local_pool_autorelease))
#define CP_DEF(cls) \
_LocalVariablesPool& cls::_getLocalPool (_LocalVariablesPoolAutoRelease &auto_release) \
{ \
static ThreadSafeStaticObj< _ReusableVariablesPool< _LocalVariablesPool > > _shared_pool; \
\
int idx; \
_LocalVariablesPool &var = _shared_pool->getVacant(idx); \
auto_release.init(idx, _shared_pool.ptr()); \
return var; \
} \
#ifdef _WIN32
#pragma warning(pop)
#endif
#endif // __tlscont_h__
|