aboutsummaryrefslogtreecommitdiff
path: root/libbutl/path.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2024-05-21 12:18:21 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2024-05-21 12:18:21 +0200
commit4fa6521c562ebf09343353717fb7c927df1eee86 (patch)
tree124ab05f06082905a77addcdfa39cda956b53358 /libbutl/path.cxx
parente5fe5459ffbc53504d46331c520f5bff90085a5b (diff)
Add notion of thread-specific current working directory
Diffstat (limited to 'libbutl/path.cxx')
-rw-r--r--libbutl/path.cxx27
1 files changed, 27 insertions, 0 deletions
diff --git a/libbutl/path.cxx b/libbutl/path.cxx
index bd66f13..909971b 100644
--- a/libbutl/path.cxx
+++ b/libbutl/path.cxx
@@ -26,6 +26,8 @@
#include <atomic>
#include <cstring> // strcpy()
+#include <libbutl/ft/lang.hxx> // thread_local
+
#include <libbutl/utility.hxx> // throw_*_error()
#include <libbutl/process.hxx> // process::current_id()
@@ -55,10 +57,21 @@ namespace butl
// char
//
+ static
+#ifdef __cpp_thread_local
+ thread_local
+#else
+ __thread
+#endif
+ const path_traits<char>::string_type* current_directory_ = nullptr;
+
template <>
LIBBUTL_SYMEXPORT path_traits<char>::string_type path_traits<char>::
current_directory ()
{
+ if (const auto* twd = current_directory_)
+ return *twd;
+
#ifdef _WIN32
char cwd[_MAX_PATH];
if (_getcwd (cwd, _MAX_PATH) == 0)
@@ -98,6 +111,20 @@ namespace butl
#endif
}
+ template <>
+ LIBBUTL_SYMEXPORT const path_traits<char>::string_type* path_traits<char>::
+ thread_current_directory ()
+ {
+ return current_directory_;
+ }
+
+ template <>
+ LIBBUTL_SYMEXPORT void path_traits<char>::
+ thread_current_directory (const string_type* twd)
+ {
+ current_directory_ = twd;
+ }
+
#ifndef _WIN32
static const small_vector<string, 4> tmp_vars (
{"TMPDIR", "TMP", "TEMP", "TEMPDIR"});