diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2010-09-10 11:26:29 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2010-09-10 11:26:29 +0200 |
commit | 81ac8f1ff70adb0b7a1e4625dcf237bcec83b2a4 (patch) | |
tree | f046f261ea0e81da62a1f8d723d67d8ead8bd8fc /odb/details/posix | |
parent | 33de34de425d2c34b88cdb6547b79b0ee18425ac (diff) |
Add support for early destruction of TLS object
Diffstat (limited to 'odb/details/posix')
-rw-r--r-- | odb/details/posix/tls.hxx | 10 | ||||
-rw-r--r-- | odb/details/posix/tls.txx | 22 |
2 files changed, 29 insertions, 3 deletions
diff --git a/odb/details/posix/tls.hxx b/odb/details/posix/tls.hxx index d23a09a..0db3d35 100644 --- a/odb/details/posix/tls.hxx +++ b/odb/details/posix/tls.hxx @@ -23,6 +23,9 @@ namespace odb T& get () const; + void + free (); + private: tls (const tls&); tls& operator= (const tls&); @@ -74,6 +77,13 @@ namespace odb } template <typename T> + inline void + tls_free (tls<T>& t) + { + t.free (); + } + + template <typename T> inline T* tls_get (const tls<T*>& t) { diff --git a/odb/details/posix/tls.txx b/odb/details/posix/tls.txx index 8cc78a4..189248b 100644 --- a/odb/details/posix/tls.txx +++ b/odb/details/posix/tls.txx @@ -32,9 +32,7 @@ namespace odb if (e != 0 || error_ != 0) throw posix_exception (e ? e : error_); - void* v (pthread_getspecific (key_)); - - if (v != 0) + if (void* v = pthread_getspecific (key_)) return *static_cast<T*> (v); std::auto_ptr<T> p (new T); @@ -49,6 +47,24 @@ namespace odb template <typename T> void tls<T>:: + 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<T*> (v); + } + } + + template <typename T> + void tls<T>:: key_init () { error_ = pthread_key_create (&key_, destructor); |