From 7b54f43fb1b123f451460edb0add218a0428fe95 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 7 Aug 2017 14:54:55 -0700 Subject: Remove uses of global variables There were two main places where global variables were used in the Slang implementation: 1. The "standard library" code was generated as a string at run-time, and stored in a global variable so that it could be amortized across compiles. 2. The representation of types uses some globals (well, class `static` members) to store common types (e.g., `void`) and to deal with memory lifetime for things like canonicalized types. In each case the "simple" fix is to move the relevant state into the `Session` type that controlled their lifetime already (the `Session` destructor was already cleaning up these globals to avoid leaks). For the standard library stuff this really was easy, but for the types it required threading through the `Session` a bit carefully. One more case that I found: there was a function-`static` variable used to generate a unique ID for files output when dumping of intermediates is enabled (this is almost strictly a debugging option). Rather than make this counter per-session (which would lead to different sessions on different threads clobbering the same few files), I went ahead and used an atomic in this case. Note that the remaining case I had been worried about was any function-`static` counter that might be used in generating unique names. It turns out that right now the parser doesn't use such a counter (even in cases where it probably should), and the lowering pass already uses a counter local to the pass (again, whether or not this is a good idea). This change should be a major step toward allowing an application to use Slang in multiple threads, so long as each thread uses a distinct `SlangSession`. The case of using a single session across multiple threads is harder to support, and will require more careful implementation work. --- source/slang/syntax-base-defs.h | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) (limited to 'source/slang/syntax-base-defs.h') diff --git a/source/slang/syntax-base-defs.h b/source/slang/syntax-base-defs.h index 03d4b749a..eda554988 100644 --- a/source/slang/syntax-base-defs.h +++ b/source/slang/syntax-base-defs.h @@ -62,29 +62,9 @@ ABSTRACT_SYNTAX_CLASS(ExpressionType, Val) RAW( public: - static RefPtr Error; - static RefPtr initializerListType; - static RefPtr Overloaded; + Session* getSession() { return this->session; } + void setSession(Session* s) { this->session = s; } - static Dictionary> sBuiltinTypes; - static Dictionary sMagicDecls; - - // Note: just exists to make sure we can clean up - // canonical types we create along the way - static List> sCanonicalTypes; - - - - static ExpressionType* GetBool(); - static ExpressionType* GetFloat(); - static ExpressionType* getDoubleType(); - static ExpressionType* GetInt(); - static ExpressionType* GetUInt(); - static ExpressionType* GetVoid(); - static ExpressionType* getInitializerListType(); - static ExpressionType* GetError(); - -public: virtual String ToString() = 0; bool Equals(ExpressionType * type); @@ -115,8 +95,6 @@ public: bool IsSampler() { return As() != nullptr; } bool IsStruct(); bool IsClass(); - static void Init(); - static void Finalize(); ExpressionType* GetCanonicalType(); virtual RefPtr SubstituteImpl(Substitutions* subst, int* ioDiff) override; @@ -127,6 +105,8 @@ protected: virtual ExpressionType* CreateCanonicalType() = 0; ExpressionType* canonicalType = nullptr; + + Session* session = nullptr; ) END_SYNTAX_CLASS() -- cgit v1.2.3