// file : odb/details/posix/tls.txx // copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file #include #include namespace odb { namespace details { // tls // template int tls::error_ = 0; template pthread_once_t tls::once_ = PTHREAD_ONCE_INIT; template pthread_key_t tls::key_; template T& tls:: get () const { int e (pthread_once (&once_, key_init)); if (e != 0 || error_ != 0) throw posix_exception (e ? e : error_); if (void* v = pthread_getspecific (key_)) return *static_cast (v); unique_ptr p (new T); if ((e = pthread_setspecific (key_, p.get ()))) throw posix_exception (e); T& r (*p); p.release (); return r; } template void tls:: free () { int e (pthread_once (&once_, key_init)); if (e != 0 || error_ != 0) throw posix_exception (e ? e : error_); if (void* v = pthread_getspecific (key_)) { if ((e = pthread_setspecific (key_, 0))) throw posix_exception (e); delete static_cast (v); } } template void tls:: key_init () { error_ = pthread_key_create (&key_, destructor); } template void tls:: destructor (void* v) { delete static_cast (v); } // tls // template int tls::error_ = 0; template pthread_once_t tls::once_ = PTHREAD_ONCE_INIT; template pthread_key_t tls::key_; template T* tls:: get () const { int e (pthread_once (&once_, key_init)); if (e != 0 || error_ != 0) throw posix_exception (e ? e : error_); return static_cast (pthread_getspecific (key_)); } template void tls:: set (T* p) { int e (pthread_once (&once_, key_init)); if (e != 0 || error_ != 0) throw posix_exception (e ? e : error_); if ((e = pthread_setspecific (key_, p))) throw posix_exception (e); } template void tls:: key_init () { error_ = pthread_key_create (&key_, 0); } } }