2022-09-22 08:18:38 +00:00
|
|
|
/* #define DEBUGTRACE_ENABLED */
|
2022-08-11 12:47:44 +00:00
|
|
|
#include "lasp_thread.h"
|
|
|
|
#include "BS_thread_pool.hpp"
|
2022-08-14 19:00:22 +00:00
|
|
|
#include "debugtrace.hpp"
|
|
|
|
#include <memory>
|
2022-08-11 12:47:44 +00:00
|
|
|
|
2022-08-14 19:00:22 +00:00
|
|
|
/**
|
2023-06-10 13:47:52 +00:00
|
|
|
* @brief Store a global weak_ptr, that is used to create new shared pointers
|
|
|
|
* if any other shared pointers are still alive. If not, we create a new
|
|
|
|
* instance.
|
2022-08-14 19:00:22 +00:00
|
|
|
*/
|
2023-06-10 13:47:52 +00:00
|
|
|
std::weak_ptr<BS::thread_pool> _global_weak_pool;
|
2022-08-11 12:47:44 +00:00
|
|
|
|
2023-06-10 13:47:52 +00:00
|
|
|
/**
|
2023-06-12 07:11:08 +00:00
|
|
|
* @brief Global mutex, used to restrict the pool creation to a single thread
|
|
|
|
* at once.
|
2023-06-10 13:47:52 +00:00
|
|
|
*/
|
2023-06-12 07:11:08 +00:00
|
|
|
std::mutex _mtx;
|
2023-06-10 13:47:52 +00:00
|
|
|
|
|
|
|
using Lck = std::scoped_lock<std::mutex>;
|
|
|
|
using rte = std::runtime_error;
|
2022-08-11 12:47:44 +00:00
|
|
|
|
2023-06-12 07:11:08 +00:00
|
|
|
GlobalThreadPool::GlobalThreadPool() {
|
2023-06-10 13:47:52 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
Lck lck(_mtx);
|
|
|
|
/// See if we can get it from the global ptr. If not, time to allocate it.
|
|
|
|
_pool = _global_weak_pool.lock();
|
|
|
|
if (!_pool) {
|
|
|
|
_pool = std::make_shared<BS::thread_pool>();
|
|
|
|
if (!_pool) {
|
|
|
|
throw rte("Fatal: could not allocate thread pool!");
|
|
|
|
}
|
|
|
|
// Update global weak pointer
|
|
|
|
_global_weak_pool = _pool;
|
2022-08-14 19:00:22 +00:00
|
|
|
}
|
2022-08-11 12:47:44 +00:00
|
|
|
}
|