diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2021-03-30 13:29:46 +0200 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2021-03-31 16:33:01 +0300 |
commit | 7083a1dc7d395df78d8e208c26ad996c5a6394f5 (patch) | |
tree | 439e894cdd30b968efbbba852f03c131a5c846e7 /libbutl/utility.mxx | |
parent | 7190ae233819cd68f5fce2564f7221ffe23a83bf (diff) |
Add notion of thread environment
Diffstat (limited to 'libbutl/utility.mxx')
-rw-r--r-- | libbutl/utility.mxx | 63 |
1 files changed, 60 insertions, 3 deletions
diff --git a/libbutl/utility.mxx b/libbutl/utility.mxx index 8a0059a..f329a0f 100644 --- a/libbutl/utility.mxx +++ b/libbutl/utility.mxx @@ -266,17 +266,74 @@ LIBBUTL_MODEXPORT namespace butl // Environment variables. // - optional<std::string> + // Our getenv() wrapper (as well as the relevant process startup functions) + // have a notion of a "thread environment", that is, thread-specific + // environment variables. However, unlike the process environment (in the + // form of the environ array), the thread environment is specified as a set + // of overrides over the process environment (sets and unsets), the same as + // for the process startup. + // + extern +#ifdef __cpp_thread_local + thread_local +#else + __thread +#endif + const char* const* thread_env_; + + // On Windows one cannot export a thread-local variable so we have to + // use wrapper functions. + // +#ifdef _WIN32 + LIBBUTL_SYMEXPORT const char* const* + thread_env (); + + LIBBUTL_SYMEXPORT void + thread_env (const char* const*); +#else + inline const char* const* + thread_env () {return thread_env_;} + + inline void + thread_env (const char* const* v) {thread_env_ = v;} +#endif + + struct auto_thread_env + { + optional<const char* const*> prev_env; + + auto_thread_env () = default; + + explicit + auto_thread_env (const char* const*); + + // Move-to-empty-only type. + // + auto_thread_env (auto_thread_env&&); + auto_thread_env& operator= (auto_thread_env&&); + + auto_thread_env (const auto_thread_env&) = delete; + auto_thread_env& operator= (const auto_thread_env&) = delete; + + ~auto_thread_env (); + }; + + // Get the environment variables taking into account the current thread's + // overrides (thread_env). + // + LIBBUTL_SYMEXPORT optional<std::string> getenv (const std::string&); - // Throw system_error on failure. + // Set the process environment variable. Best done before starting any + // threads (see thread_env). Throw system_error on failure. // // Note that on Windows setting an empty value unsets the variable. // LIBBUTL_SYMEXPORT void setenv (const std::string& name, const std::string& value); - // Throw system_error on failure. + // Unset the process environment variable. Best done before starting any + // threads (see thread_env). Throw system_error on failure. // LIBBUTL_SYMEXPORT void unsetenv (const std::string&); |