Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/include/borealis/core/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extern void sync(const std::function<void()>& func);
*
* It's a shortcut for brls::Application::async(&func);
*/
extern void async(const std::function<void()>& func);
extern void async(const std::function<void()>& func, bool concurrent = true);

extern size_t delay(long milliseconds, const std::function<void()>& func);

Expand Down
48 changes: 48 additions & 0 deletions library/include/borealis/core/thread_pool.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2025 XITRIX

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include <thread>
#include <functional>
#include <mutex>
#include <condition_variable>
#include <queue>

namespace brls {

class ThreadPool {
public:
explicit ThreadPool(int threads);
~ThreadPool();

void async(std::function<void(void)> func);

static ThreadPool* global() { return _global; }
private:

void threadEntry(int i);

std::mutex lock_;
std::condition_variable condVar_;
bool shutdown_;
std::queue<std::function<void(void)>> jobs_;
std::vector<std::thread> threads_;

static ThreadPool* _global;
};

} // namespace brls
8 changes: 6 additions & 2 deletions library/lib/core/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <borealis/core/logger.hpp>
#include <borealis/core/thread.hpp>
#include <borealis/core/thread_pool.hpp>
#include <exception>

#ifdef BOREALIS_USE_STD_THREAD
Expand Down Expand Up @@ -49,9 +50,12 @@ void sync(const std::function<void()>& func)
Threading::sync(func);
}

void async(const std::function<void()>& task)
void async(const std::function<void()>& task, bool concurrent)
{
Threading::async(task);
if (concurrent)
ThreadPool::global()->async(task);
else
Threading::async(task);
}

size_t delay(long milliseconds, const std::function<void()>& func)
Expand Down
82 changes: 82 additions & 0 deletions library/lib/core/thread_pool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2025 XITRIX

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <borealis/core/thread_pool.hpp>
#include <borealis/core/logger.hpp>

namespace brls {

// Create global ThreadPool with default amount of threads (probably should be configurable)
ThreadPool* ThreadPool::_global = new ThreadPool(8);

ThreadPool::ThreadPool(int threads) : shutdown_(false) {
// Create the specified number of threads
threads_.reserve(threads);
for (int i = 0; i < threads; ++i)
threads_.emplace_back([this, i] { threadEntry(i); });
}

ThreadPool::~ThreadPool() {
{
// Unblock any threads and tell them to stop
std::unique_lock<std::mutex> l(lock_);

shutdown_ = true;
condVar_.notify_all();
}

// Wait for all threads to stop
brls::Logger::info("Joining threads");
for (auto &thread: threads_)
thread.join();
}

void ThreadPool::async(std::function<void(void)> func) {
// Place a job on the queue and unblock a thread
std::unique_lock<std::mutex> l(lock_);

jobs_.emplace(std::move(func));
condVar_.notify_one();
}

void ThreadPool::threadEntry(int i) {
std::function<void(void)> job;

while (true) {
{
std::unique_lock<std::mutex> l(lock_);

while (!shutdown_ && jobs_.empty())
condVar_.wait(l);

if (jobs_.empty()) {
// No jobs to do and we are shutting down
brls::Logger::info("Thread {} terminates", i);
return;
}

// brls::Logger::info("Thread {} does a job", i);
job = std::move(jobs_.front());
jobs_.pop();
}

// Do the job without holding any locks
job();
}

}

} // namespace brls