summaryrefslogtreecommitdiffstats
path: root/fastos
diff options
context:
space:
mode:
authorHÃ¥vard Pettersen <3535158+havardpe@users.noreply.github.com>2022-03-09 11:40:43 +0100
committerGitHub <noreply@github.com>2022-03-09 11:40:43 +0100
commit44607b151b97c0483354ff83630db6829df0a5ba (patch)
tree351e51132162f4cc0bba80c8bb2f4992e09026d4 /fastos
parent7743992d1dd55b635a297ad97546d4fbfd5ce23f (diff)
parent72905e49bd93ed344de54022f884dc545a115b7f (diff)
Merge pull request #21605 from vespa-engine/havardpe/remove-tsan-warnings-from-fastos
make some variables atomic to make tsan happy
Diffstat (limited to 'fastos')
-rw-r--r--fastos/src/tests/job.h2
-rw-r--r--fastos/src/tests/thread_test_base.hpp6
-rw-r--r--fastos/src/vespa/fastos/thread.h12
3 files changed, 13 insertions, 7 deletions
diff --git a/fastos/src/tests/job.h b/fastos/src/tests/job.h
index 15356270972..4546cfe1daa 100644
--- a/fastos/src/tests/job.h
+++ b/fastos/src/tests/job.h
@@ -28,7 +28,7 @@ public:
std::mutex *mutex;
std::condition_variable *condition;
FastOS_ThreadInterface *otherThread, *ownThread;
- int result;
+ std::atomic<int> result;
FastOS_ThreadId _threadId;
Job()
diff --git a/fastos/src/tests/thread_test_base.hpp b/fastos/src/tests/thread_test_base.hpp
index 49d37209b6a..eb994537f6e 100644
--- a/fastos/src/tests/thread_test_base.hpp
+++ b/fastos/src/tests/thread_test_base.hpp
@@ -5,7 +5,7 @@
#include <chrono>
#include <thread>
-static volatile int64_t number;
+static std::atomic<int64_t> number;
#define INCREASE_NUMBER_AMOUNT 10000
using namespace std::chrono_literals;
@@ -97,11 +97,11 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg)
guard = std::unique_lock<std::mutex>(*job->mutex);
}
- result = static_cast<int>(number);
+ result = static_cast<int>(number.load(std::memory_order_relaxed));
int sleepOn = (INCREASE_NUMBER_AMOUNT/2) * 321/10000;
for (int i=0; i<(INCREASE_NUMBER_AMOUNT/2); i++) {
- number = number + 2;
+ number.fetch_add(2, std::memory_order_relaxed);
if (i == sleepOn)
std::this_thread::sleep_for(1ms);
diff --git a/fastos/src/vespa/fastos/thread.h b/fastos/src/vespa/fastos/thread.h
index 0f4cc4c09f7..f881095c29a 100644
--- a/fastos/src/vespa/fastos/thread.h
+++ b/fastos/src/vespa/fastos/thread.h
@@ -50,7 +50,7 @@ private:
bool _closeCalledFlag;
// Always lock in this order
- std::mutex _freeMutex;
+ mutable std::mutex _freeMutex;
std::mutex _liveMutex;
std::condition_variable _liveCond;
/**
@@ -191,7 +191,10 @@ public:
* @ref GetNumActiveThreads() and @ref GetNumInactiveThreads().
* @return Number of currently active threads
*/
- int GetNumActiveThreads () const { return _numActive; }
+ int GetNumActiveThreads () const {
+ std::lock_guard<std::mutex> guard(_freeMutex);
+ return _numActive;
+ }
/**
* Get the number of currently inactive threads.
@@ -199,7 +202,10 @@ public:
* @ref GetNumActiveThreads() and @ref GetNumInactiveThreads().
* @return Number of currently inactive threads
*/
- int GetNumInactiveThreads () const { return _numFree; }
+ int GetNumInactiveThreads () const {
+ std::lock_guard<std::mutex> guard(_freeMutex);
+ return _numFree;
+ }
/**
* Get the number of started threads since instantiation of the thread pool.