From 9b1007c00eaefd98f0f70ea31c9c953271686136 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Fri, 20 Jun 2014 11:22:13 -0700 Subject: Adding a TLSFactory system. --- includes/Local.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'includes/Local.h') diff --git a/includes/Local.h b/includes/Local.h index f1174c4..b9a7741 100644 --- a/includes/Local.h +++ b/includes/Local.h @@ -23,6 +23,34 @@ class PThreadsTLSManager : public TLSManager { pthread_key_t m_key; }; +template +class PThreadsTLSFactory : private PThreadsTLSManager { + public: + PThreadsTLSFactory() : m_constructor([]() -> TLS * { return new TLS(); }) { } + ~PThreadsTLSFactory() { destroyAll(); } + void setConstructor(const std::function & constructor) { m_constructor = constructor; } + TLS * get() { + TLS * tls = (TLS *) getTLS(); + if (!tls) { + tls = m_constructor(); + setTLS(tls); + m_TLSes.push(tls); + ++m_numTLSes; + } + return tls; + } + private: + void destroyAll() { + while (m_numTLSes--) { + TLS * tls = m_TLSes.pop(); + delete tls; + } + } + Queue m_TLSes; + std::atomic m_numTLSes; + std::function m_constructor; +}; + extern TLSManager * g_tlsManager; class Local : public AtStart { -- cgit v1.2.3