diff options
Diffstat (limited to 'odb/details/posix/tls.txx')
-rw-r--r-- | odb/details/posix/tls.txx | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/odb/details/posix/tls.txx b/odb/details/posix/tls.txx new file mode 100644 index 0000000..8cc78a4 --- /dev/null +++ b/odb/details/posix/tls.txx @@ -0,0 +1,108 @@ +// file : odb/details/posix/tls.txx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <memory> // std::auto_ptr + +#include <odb/details/posix/exceptions.hxx> + +namespace odb +{ + namespace details + { + // tls<T> + // + + template <typename T> + int tls<T>::error_ = 0; + + template <typename T> + pthread_once_t tls<T>::once_ = PTHREAD_ONCE_INIT; + + template <typename T> + pthread_key_t tls<T>::key_; + + template <typename T> + T& tls<T>:: + get () const + { + int e (pthread_once (&once_, key_init)); + + if (e != 0 || error_ != 0) + throw posix_exception (e ? e : error_); + + void* v (pthread_getspecific (key_)); + + if (v != 0) + return *static_cast<T*> (v); + + std::auto_ptr<T> p (new T); + + if (e = pthread_setspecific (key_, p.get ())) + throw posix_exception (e); + + T& r (*p); + p.release (); + return r; + } + + template <typename T> + void tls<T>:: + key_init () + { + error_ = pthread_key_create (&key_, destructor); + } + + template <typename T> + void tls<T>:: + destructor (void* v) + { + delete static_cast<T*> (v); + } + + // tls<T*> + // + + template <typename T> + int tls<T*>::error_ = 0; + + template <typename T> + pthread_once_t tls<T*>::once_ = PTHREAD_ONCE_INIT; + + template <typename T> + pthread_key_t tls<T*>::key_; + + template <typename T> + T* tls<T*>:: + get () const + { + int e (pthread_once (&once_, key_init)); + + if (e != 0 || error_ != 0) + throw posix_exception (e ? e : error_); + + return static_cast<T*> (pthread_getspecific (key_)); + } + + template <typename T> + void tls<T*>:: + 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 <typename T> + void tls<T*>:: + key_init () + { + error_ = pthread_key_create (&key_, 0); + } + } +} |