diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2022-11-18 09:37:11 +0100 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2022-11-18 09:37:11 +0100 |
commit | f1ab082ac4f589a36a9eb0cd98d1cc235eedc419 (patch) | |
tree | 80c79c7250208a55a558d53a47a24dae2faa071d | |
parent | f423d4425f6573206045e9626812002906d9493d (diff) |
createTempDir(): Use std::atomic
-rw-r--r-- | src/libutil/filesystem.cc | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/src/libutil/filesystem.cc b/src/libutil/filesystem.cc index 403389e60..3a732cff8 100644 --- a/src/libutil/filesystem.cc +++ b/src/libutil/filesystem.cc @@ -1,5 +1,6 @@ #include <sys/time.h> #include <filesystem> +#include <atomic> #include "finally.hh" #include "util.hh" @@ -10,7 +11,7 @@ namespace fs = std::filesystem; namespace nix { static Path tempName(Path tmpRoot, const Path & prefix, bool includePid, - int & counter) + std::atomic<unsigned int> & counter) { tmpRoot = canonPath(tmpRoot.empty() ? getEnv("TMPDIR").value_or("/tmp") : tmpRoot, true); if (includePid) @@ -22,9 +23,9 @@ static Path tempName(Path tmpRoot, const Path & prefix, bool includePid, Path createTempDir(const Path & tmpRoot, const Path & prefix, bool includePid, bool useGlobalCounter, mode_t mode) { - static int globalCounter = 0; - int localCounter = 0; - int & counter(useGlobalCounter ? globalCounter : localCounter); + static std::atomic<unsigned int> globalCounter = 0; + std::atomic<unsigned int> localCounter = 0; + auto & counter(useGlobalCounter ? globalCounter : localCounter); while (1) { checkInterrupt(); |