You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.1 KiB
C++

//
// Created by anton on 6/24/22.
//
#ifndef PMP_GLOBAL_THREAD_POOL_H
#define PMP_GLOBAL_THREAD_POOL_H
#include <iostream>
#include <cassert>
#include <functional>
#include <unordered_map>
#include <stack>
#include <queue>
#include <memory>
#include <vector>
#include <thread>
#include <mutex>
#include <chrono>
#include <atomic>
#include <random>
#include <sodium.h>
#include <unordered_set>
#include <map>
#include <optional>
#include <boost/lockfree/stack.hpp>
#include "call_frame.h"
#include "helpers.h"
namespace pmp::core {
class global_thread_pool {
private:
call_frame* m_root_frame;
// frame creation / lookup stuff
std::atomic<frame_id_t> m_last_id;
std::unordered_map<int, call_frame*> m_frames;
std::mutex m_frames_lock;
// thread stuff
std::atomic<bool> m_active;
const size_t m_thread_count;
std::vector<std::thread> m_threads;
boost::lockfree::stack<std::shared_ptr<call_frame>> stack;
void worker();
public:
explicit global_thread_pool(call_frame* root_frame) : m_root_frame(root_frame), m_last_id(0), m_frames(),
m_frames_lock(), m_active(true), m_thread_count(std::thread::hardware_concurrency()),
stack(100)
{
std::cout << "Init GlobalThreadPool" << std::endl;
m_threads = std::vector<std::thread>(m_thread_count);
for (size_t i = 0; i < m_thread_count; i++) {
m_threads[i] = std::thread([&] {
worker();
});
}
}
~global_thread_pool() {
if (m_active) {
std::cout << "GlobalThreadPool destructor called before join!" << std::endl;
join();
}
assert(!m_active);
}
frame_id_t insert_frame(void *argument, void *scope, call_frame_target *target_function, frame_id_t parent);
void stop() { m_active = false; }
void join();
size_t thread_count() const { return m_thread_count; }
size_t thread_count() { return m_thread_count; }
};
}
#endif //PMP_GLOBAL_THREAD_POOL_H