summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorNicolas "Pixel" Noble <pixel@nobis-crew.org>2014-06-20 11:22:13 -0700
committerNicolas "Pixel" Noble <pixel@nobis-crew.org>2014-06-20 11:22:13 -0700
commit9b1007c00eaefd98f0f70ea31c9c953271686136 (patch)
tree35a4509d8a6802361f76f803d0cb79ae43958d1a /includes
parentf2ca78ac069bb6606abe3a6544cbb13d6b55e6d4 (diff)
Adding a TLSFactory system.
Diffstat (limited to 'includes')
-rw-r--r--includes/Local.h28
1 files changed, 28 insertions, 0 deletions
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 TLS>
+class PThreadsTLSFactory : private PThreadsTLSManager {
+ public:
+ PThreadsTLSFactory() : m_constructor([]() -> TLS * { return new TLS(); }) { }
+ ~PThreadsTLSFactory() { destroyAll(); }
+ void setConstructor(const std::function<TLS *()> & 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<TLS> m_TLSes;
+ std::atomic<unsigned> m_numTLSes;
+ std::function<TLS *()> m_constructor;
+};
+
extern TLSManager * g_tlsManager;
class Local : public AtStart {