summaryrefslogtreecommitdiff
path: root/src/support/threads.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/support/threads.cpp')
-rw-r--r--src/support/threads.cpp43
1 files changed, 26 insertions, 17 deletions
diff --git a/src/support/threads.cpp b/src/support/threads.cpp
index 7ae304c4f..785994b4e 100644
--- a/src/support/threads.cpp
+++ b/src/support/threads.cpp
@@ -20,23 +20,29 @@
#include <iostream>
#include <string>
-#include "threads.h"
#include "compiler-support.h"
+#include "threads.h"
#include "utilities.h"
-
// debugging tools
#ifdef BINARYEN_THREAD_DEBUG
static std::mutex debug;
-#define DEBUG_THREAD(x) { std::lock_guard<std::mutex> lock(debug); std::cerr << "[THREAD " << std::this_thread::get_id() << "] " << x; }
-#define DEBUG_POOL(x) { std::lock_guard<std::mutex> lock(debug); std::cerr << "[POOL " << std::this_thread::get_id() << "] " << x; }
+#define DEBUG_THREAD(x) \
+ { \
+ std::lock_guard<std::mutex> lock(debug); \
+ std::cerr << "[THREAD " << std::this_thread::get_id() << "] " << x; \
+ }
+#define DEBUG_POOL(x) \
+ { \
+ std::lock_guard<std::mutex> lock(debug); \
+ std::cerr << "[POOL " << std::this_thread::get_id() << "] " << x; \
+ }
#else
#define DEBUG_THREAD(x)
#define DEBUG_POOL(x)
#endif
-
namespace wasm {
// Thread
@@ -56,7 +62,7 @@ Thread::~Thread() {
thread->join();
}
-void Thread::work(std::function<ThreadWorkState ()> doWork_) {
+void Thread::work(std::function<ThreadWorkState()> doWork_) {
// TODO: fancy work stealing
DEBUG_THREAD("send work to thread\n");
{
@@ -68,7 +74,7 @@ void Thread::work(std::function<ThreadWorkState ()> doWork_) {
}
}
-void Thread::mainLoop(void *self_) {
+void Thread::mainLoop(void* self_) {
auto* self = static_cast<Thread*>(self_);
while (1) {
DEBUG_THREAD("checking for work\n");
@@ -77,7 +83,8 @@ void Thread::mainLoop(void *self_) {
if (self->doWork) {
DEBUG_THREAD("doing work\n");
// run tasks until they are all done
- while (self->doWork() == ThreadWorkState::More) {}
+ while (self->doWork() == ThreadWorkState::More) {
+ }
self->doWork = nullptr;
} else if (self->done) {
DEBUG_THREAD("done\n");
@@ -107,16 +114,19 @@ std::mutex ThreadPool::workMutex;
std::mutex ThreadPool::threadMutex;
void ThreadPool::initialize(size_t num) {
- if (num == 1) return; // no multiple cores, don't create threads
+ if (num == 1)
+ return; // no multiple cores, don't create threads
DEBUG_POOL("initialize()\n");
std::unique_lock<std::mutex> lock(threadMutex);
- ready.store(threads.size()); // initial state before first resetThreadsAreReady()
+ // initial state before first resetThreadsAreReady()
+ ready.store(threads.size());
resetThreadsAreReady();
for (size_t i = 0; i < num; i++) {
try {
threads.emplace_back(make_unique<Thread>(this));
} catch (std::system_error&) {
- // failed to create a thread - don't use multithreading, as if num cores == 1
+ // failed to create a thread - don't use multithreading, as if num cores
+ // == 1
DEBUG_POOL("could not create thread\n");
threads.clear();
return;
@@ -154,14 +164,16 @@ ThreadPool* ThreadPool::get() {
return pool.get();
}
-void ThreadPool::work(std::vector<std::function<ThreadWorkState ()>>& doWorkers) {
+void ThreadPool::work(
+ std::vector<std::function<ThreadWorkState()>>& doWorkers) {
size_t num = threads.size();
// If no multiple cores, or on a side thread, do not use worker threads
if (num == 0) {
// just run sequentially
DEBUG_POOL("work() sequentially\n");
assert(doWorkers.size() > 0);
- while (doWorkers[0]() == ThreadWorkState::More) {}
+ while (doWorkers[0]() == ThreadWorkState::More) {
+ }
return;
}
// run in parallel on threads
@@ -187,9 +199,7 @@ void ThreadPool::work(std::vector<std::function<ThreadWorkState ()>>& doWorkers)
DEBUG_POOL("work() is done\n");
}
-size_t ThreadPool::size() {
- return std::max(size_t(1), threads.size());
-}
+size_t ThreadPool::size() { return std::max(size_t(1), threads.size()); }
bool ThreadPool::isRunning() {
DEBUG_POOL("check if running\n");
@@ -216,4 +226,3 @@ bool ThreadPool::areThreadsReady() {
}
} // namespace wasm
-