From 81ac8f1ff70adb0b7a1e4625dcf237bcec83b2a4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 10 Sep 2010 11:26:29 +0200 Subject: Add support for early destruction of TLS object --- odb/details/posix/tls.hxx | 10 ++++++++++ odb/details/posix/tls.txx | 22 +++++++++++++++++++--- odb/details/tls.hxx | 9 +++++++++ odb/details/win32/tls.cxx | 4 ++++ odb/details/win32/tls.hxx | 10 ++++++++++ odb/details/win32/tls.txx | 16 ++++++++++++++-- 6 files changed, 66 insertions(+), 5 deletions(-) (limited to 'odb/details') 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 + inline void + tls_free (tls& t) + { + t.free (); + } + + template inline T* tls_get (const tls& 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 (v); std::auto_ptr p (new T); @@ -49,6 +47,24 @@ namespace odb 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); diff --git a/odb/details/tls.hxx b/odb/details/tls.hxx index d2627be..2ed68ab 100644 --- a/odb/details/tls.hxx +++ b/odb/details/tls.hxx @@ -26,6 +26,15 @@ namespace odb return x; } + // If early destructions is possible, destroy the object and free + // any allocated resources. + // + template + inline void + tls_free (T& x) + { + } + template inline T* tls_get (T* p) diff --git a/odb/details/win32/tls.cxx b/odb/details/win32/tls.cxx index d6dc2d8..29eb5fb 100644 --- a/odb/details/win32/tls.cxx +++ b/odb/details/win32/tls.cxx @@ -18,6 +18,10 @@ #include #include +#ifdef _MSC_VER +# pragma warning (disable:4200) // zero-sized array in struct +#endif + using namespace std; namespace odb diff --git a/odb/details/win32/tls.hxx b/odb/details/win32/tls.hxx index b1dce0f..3354723 100644 --- a/odb/details/win32/tls.hxx +++ b/odb/details/win32/tls.hxx @@ -39,6 +39,9 @@ namespace odb T& get () const; + void + free (); + private: tls (const tls&); tls& operator= (const tls&); @@ -88,6 +91,13 @@ namespace odb } template + inline void + tls_free (tls& t) + { + t.free (); + } + + template inline T* tls_get (const tls& t) { diff --git a/odb/details/win32/tls.txx b/odb/details/win32/tls.txx index f20b6a9..025e7cb 100644 --- a/odb/details/win32/tls.txx +++ b/odb/details/win32/tls.txx @@ -26,9 +26,8 @@ namespace odb get () const { once_.call (key_init); - void* v (_get (key_)); - if (v != 0) + if (void* v = _get (key_)) return *static_cast (v); auto_ptr p (new T); @@ -41,6 +40,19 @@ namespace odb template void tls:: + free () + { + once_.call (key_init); + + if (void* v = _get (key_)) + { + _set (key_, 0); + delete static_cast (v); + } + } + + template + void tls:: key_init () { key_ = _allocate (destructor); -- cgit v1.1