From acb656e605d91971ee4014da66be1b7ba6201ac3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 18 Aug 2010 17:51:09 +0200 Subject: Add multi-threading primitives Currently only the pthread-based implementation is present. --- odb/details/posix/tls.txx | 108 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 odb/details/posix/tls.txx (limited to 'odb/details/posix/tls.txx') 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 +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include // std::auto_ptr + +#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_); + + void* v (pthread_getspecific (key_)); + + if (v != 0) + return *static_cast (v); + + std::auto_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:: + 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); + } + } +} -- cgit v1.1