Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mjshakir committed Dec 12, 2024
1 parent b30a85a commit 0c9a43a
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
81 changes: 81 additions & 0 deletions include/ThreadPoolManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pragma once

//--------------------------------------------------------------
// Standard library
//--------------------------------------------------------------
#include <iostream>
#include <memory>
#include <functional>
#include <stdexcept>
#include <optional>
#include <atomic>
#include <type_traits>
//--------------------------------------------------------------
// User Defined library
//--------------------------------------------------------------
#include "ThreadPool.hpp"
//--------------------------------------------------------------
namespace ThreadPool {
//--------------------------------------------------------------
enum class PrecedenceLevel : uint8_t {
LOW = 0U, MEDIUM, HIGH
}; // end enum class PrecedenceLevel
//--------------------------------------------------------------
class ThreadPoolManager {
//--------------------------------------------------------------
public:
//--------------------------------------------------------------
// Get the global ThreadPoolManager instance
static ThreadPoolManager& get_instance(void);
//--------------------------
// Set the mode and tick for the global ThreadPool. Must be called once before accessing the ThreadPool.
template <ThreadMode Mode, size_t Tick, PrecedenceLevel Precedence>
constexpr bool configure(const size_t& number_threads = 0UL) {
bool isAdaptive = (Tick > 0UL);
if (m_instance) {
if (should_override_configuration(Mode, isAdaptive, Precedence)) {
//--------------------------
m_instance = std::move(std::unique_ptr<ThreadPool<>>(new ThreadPool<Mode, Tick>(number_threads)));
update_configuration(Mode, isAdaptive, Precedence);
return true;
//--------------------------
} else {
//--------------------------
return false;
//--------------------------
} // end else
} // end if (m_instance)
//--------------------------
m_instance = std::move(std::unique_ptr<ThreadPool<>>(new ThreadPool<Mode, Tick>(number_threads)));
update_configuration(Mode, isAdaptive, Precedence);
//--------------------------
return true;
//--------------------------
} // end bool configure(void)
//--------------------------
// Check if the ThreadPool is already initialized
constexpr bool initialized(void) const;
//--------------------------
// Get the global ThreadPool instance
ThreadPool<>& get_thread_pool(void) const;
//--------------------------------------------------------------
protected:
//--------------------------------------------------------------
constexpr bool should_override_configuration(ThreadMode mode, bool isAdaptiveTick, PrecedenceLevel precedence) const;
constexpr void update_configuration(ThreadMode mode, bool isAdaptiveTick, PrecedenceLevel precedence);
//--------------------------------------------------------------
private:
//--------------------------------------------------------------
// Constructor using initializer list for efficiency
ThreadPoolManager(void);
~ThreadPoolManager(void) = default;
//--------------------------
std::unique_ptr<ThreadPool<>> m_instance; // Managed uniquely within ThreadPoolManager
ThreadMode m_current_mode;
bool m_adaptive_tick;
PrecedenceLevel m_current_precedence;
//--------------------------------------------------------------
}; // end class ThreadPoolManager
//--------------------------------------------------------------
} // end namespace ThreadPool
//--------------------------------------------------------------
59 changes: 59 additions & 0 deletions src/ThreadPoolManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//--------------------------------------------------------------
// Main Header
//--------------------------------------------------------------
#include "ThreadPoolManager.hpp"
//--------------------------------------------------------------
ThreadPool::ThreadPoolManager::ThreadPoolManager(void) : m_instance(nullptr),
m_current_mode(ThreadMode::STANDARD),
m_adaptive_tick(false),
m_current_precedence(PrecedenceLevel::LOW) {
//--------------------------
}// end ThreadPool::ThreadPoolManager::ThreadPoolManager(void)
//--------------------------------------------------------------
ThreadPool::ThreadPoolManager& ThreadPool::ThreadPoolManager::get_instance(void) {
static ThreadPoolManager instance;
return instance;
} // end ThreadPool::ThreadPoolManager::get_instance(void)
//--------------------------------------------------------------
constexpr bool ThreadPool::ThreadPoolManager::initialized(void) const {
return m_instance != nullptr;
} // end ThreadPool::ThreadPoolManager::initialized(void)
//--------------------------------------------------------------
ThreadPool::ThreadPool<>& ThreadPool::ThreadPoolManager::get_thread_pool(void) const {
//--------------------------
if (!m_instance) {
static ThreadPool<> default_pool;
return default_pool; // Fallback: Provide a default pool.
} // end if (!m_instance)
//--------------------------
return *m_instance;
//--------------------------
} // end ThreadPool::ThreadPoolManager::get_thread_pool(void)
//--------------------------------------------------------------
constexpr bool ThreadPool::ThreadPoolManager::should_override_configuration(ThreadMode mode, bool isAdaptiveTick, PrecedenceLevel precedence) const {
//--------------------------
// Higher precedence overrides lower precedence
if (precedence > m_current_precedence) {
return true;
} // end if (precedence > m_current_precedence)
//--------------------------
// Priority mode overrides standard mode if precedence is the same
if (precedence == m_current_precedence && mode == ThreadMode::PRIORITY && m_current_mode == ThreadMode::STANDARD) {
return true;
} // end if (precedence == m_current_precedence && mode == ThreadMode::PRIORITY && m_current_mode == ThreadMode::STANDARD)
//--------------------------
// Non-adaptive tick overrides adaptive tick if precedence and mode are the same
if (precedence == m_current_precedence && mode == m_current_mode && !isAdaptiveTick && m_adaptive_tick) {
return true;
} // end if (precedence == m_current_precedence && mode == m_current_mode && !isAdaptiveTick && m_adaptive_tick)
//--------------------------
return false;
//--------------------------
} // end ThreadPoolManager::should_override_configuration
//--------------------------------------------------------------
constexpr void ThreadPool::ThreadPoolManager::update_configuration(ThreadMode mode, bool isAdaptiveTick, PrecedenceLevel precedence) {
m_current_mode = mode;
m_adaptive_tick = isAdaptiveTick;
m_current_precedence = precedence;
} // end ThreadPoolManager::update_configuration
//--------------------------------------------------------------

0 comments on commit 0c9a43a

Please sign in to comment.