37 lines
955 B
C++
37 lines
955 B
C++
/* #define DEBUGTRACE_ENABLED */
|
|
#include "lasp_thread.h"
|
|
#include "BS_thread_pool.hpp"
|
|
#include "debugtrace.hpp"
|
|
#include <memory>
|
|
|
|
/**
|
|
* @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.
|
|
*/
|
|
std::weak_ptr<BS::thread_pool> _global_weak_pool;
|
|
|
|
/**
|
|
* @brief Global mutex, used to restrict the pool creation to a single thread
|
|
* at once.
|
|
*/
|
|
std::mutex _mtx;
|
|
|
|
using Lck = std::scoped_lock<std::mutex>;
|
|
using rte = std::runtime_error;
|
|
|
|
GlobalThreadPool::GlobalThreadPool() {
|
|
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;
|
|
}
|
|
}
|