aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@oath.com>2022-03-09 09:58:47 +0000
committerHåvard Pettersen <havardpe@oath.com>2022-03-09 09:58:47 +0000
commitf6232099047f24eb952494d555379072144c8aeb (patch)
tree164761f93b7b63dd8804fd6f8b17a9a7220c6a4a
parentb1015bd850fb94157d8c21077578e45628384e02 (diff)
make some variables atomic to make tsan happy
-rw-r--r--fastos/src/tests/job.h2
-rw-r--r--fastos/src/tests/thread_test_base.hpp4
-rw-r--r--fastos/src/vespa/fastos/thread.h12
3 files changed, 12 insertions, 6 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..12e06395e24 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;
@@ -101,7 +101,7 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg)
int sleepOn = (INCREASE_NUMBER_AMOUNT/2) * 321/10000;
for (int i=0; i<(INCREASE_NUMBER_AMOUNT/2); i++) {
- number = number + 2;
+ number += 2;
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.