mirror of
https://github.com/usatiuk/psil.git
synced 2025-10-28 10:47:49 +01:00
No threads option
This commit is contained in:
@@ -1,10 +1,6 @@
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
add_library(support src/Logger.cpp src/Options.cpp)
|
||||
|
||||
target_link_libraries(support PRIVATE Threads::Threads)
|
||||
|
||||
target_include_directories(support PUBLIC include)
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
add_library(vm src/VM.cpp src/Cell.cpp
|
||||
src/Parser.cpp src/MemoryContext.cpp src/Handle.cpp src/Compiler.cpp)
|
||||
|
||||
if (NOT NO_THREADS)
|
||||
find_package(Threads REQUIRED)
|
||||
target_link_libraries(vm PRIVATE Threads::Threads)
|
||||
else ()
|
||||
target_compile_options(vm PUBLIC -DNO_THREADS)
|
||||
endif ()
|
||||
|
||||
target_link_libraries(vm PUBLIC support)
|
||||
target_link_libraries(vm PRIVATE Threads::Threads)
|
||||
|
||||
target_include_directories(vm PUBLIC include)
|
||||
|
||||
@@ -11,10 +11,13 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
|
||||
#ifndef NO_THREADS
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#endif
|
||||
|
||||
#include "Cell.h"
|
||||
#include "Handle.h"
|
||||
@@ -38,28 +41,38 @@ public:
|
||||
}
|
||||
|
||||
void request_gc_and_wait() {
|
||||
#ifndef NO_THREADS
|
||||
std::unique_lock l(_gc_done_m);
|
||||
_gc_done = false;
|
||||
{ request_gc(); }
|
||||
if (!_gc_done) _gc_done_cv.wait(l, [&] { return _gc_done.load(); });
|
||||
#else
|
||||
gc_thread_entry();
|
||||
#endif
|
||||
}
|
||||
|
||||
void request_gc() {
|
||||
#ifndef NO_THREADS
|
||||
std::lock_guard l(_gc_request_m);
|
||||
_gc_request = true;
|
||||
_gc_request_cv.notify_all();
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t cell_count() const { return _cells_num; }
|
||||
|
||||
template<typename R>
|
||||
R run_dirty(const std::function<R(std::function<void(Cell *)>)> &f) {
|
||||
#ifndef NO_THREADS
|
||||
std::lock_guard l(_gc_dirty_notif_queue_lock);
|
||||
return f([&](Cell *c) {
|
||||
if (c == nullptr) return;
|
||||
Logger::log(Logger::MemoryContext, [&](std::ostream &out) { out << "marked dirty: " << c; }, Logger::DEBUG);
|
||||
_gc_dirty_notif_queue.emplace(c);
|
||||
});
|
||||
#else
|
||||
return f([](Cell *c) {});
|
||||
#endif
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -68,7 +81,9 @@ private:
|
||||
|
||||
size_t tcellnum;
|
||||
{
|
||||
#ifndef NO_THREADS
|
||||
std::lock_guard tmplg(_new_roots_lock);
|
||||
#endif
|
||||
tcellnum = _temp_cells.size();
|
||||
}
|
||||
|
||||
@@ -78,7 +93,9 @@ private:
|
||||
for (int i = 0; i < 3 && (_cells_num + tcellnum) >= (Options::get<size_t>("cell_limit")); i++) {
|
||||
request_gc_and_wait();
|
||||
{
|
||||
#ifndef NO_THREADS
|
||||
std::lock_guard tmplg(_new_roots_lock);
|
||||
#endif
|
||||
tcellnum = _temp_cells.size();
|
||||
}
|
||||
}
|
||||
@@ -92,7 +109,9 @@ private:
|
||||
CT *cell = new CT(std::forward<Args>(args)...);
|
||||
|
||||
{
|
||||
#ifndef NO_THREADS
|
||||
std::lock_guard tmplg(_new_roots_lock);
|
||||
#endif
|
||||
Handle ret(cell);
|
||||
_temp_cells.emplace_back(cell);
|
||||
if ((_cells_num + _temp_cells.size()) >=
|
||||
@@ -115,6 +134,7 @@ private:
|
||||
|
||||
std::map<Cell *, int64_t> _roots;
|
||||
std::map<Cell *, int64_t> _new_roots;
|
||||
#ifndef NO_THREADS
|
||||
std::recursive_mutex _new_roots_lock;
|
||||
|
||||
std::set<Cell *> _gc_dirty_notif_queue;
|
||||
@@ -130,6 +150,7 @@ private:
|
||||
|
||||
std::thread _gc_thread;
|
||||
std::atomic<bool> _gc_thread_stop = false;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif//PSIL_MEMORYCONTEXT_H
|
||||
|
||||
@@ -10,7 +10,11 @@
|
||||
#include <sstream>
|
||||
#include <unordered_set>
|
||||
|
||||
MemoryContext::MemoryContext() { _gc_thread = std::thread(std::bind(&MemoryContext::gc_thread_entry, this)); }
|
||||
MemoryContext::MemoryContext() {
|
||||
#ifndef NO_THREADS
|
||||
_gc_thread = std::thread(std::bind(&MemoryContext::gc_thread_entry, this));
|
||||
#endif
|
||||
}
|
||||
|
||||
MemoryContext::~MemoryContext() {
|
||||
// Three times because the first one might not start it as it's been running already
|
||||
@@ -22,18 +26,22 @@ MemoryContext::~MemoryContext() {
|
||||
|
||||
assert(cell_count() == 0);
|
||||
|
||||
#ifndef NO_THREADS
|
||||
_gc_thread_stop = true;
|
||||
{
|
||||
std::lock_guard l(_gc_request_m);
|
||||
_gc_request_cv.notify_all();
|
||||
}
|
||||
_gc_thread.join();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void MemoryContext::add_root(Cell *c) {
|
||||
{
|
||||
#ifndef NO_THREADS
|
||||
std::lock_guard l(_new_roots_lock);
|
||||
#endif
|
||||
_new_roots[c]++;
|
||||
Logger::log(
|
||||
Logger::MemoryContext,
|
||||
@@ -45,7 +53,9 @@ void MemoryContext::add_root(Cell *c) {
|
||||
}
|
||||
void MemoryContext::remove_root(Cell *c) {
|
||||
{
|
||||
#ifndef NO_THREADS
|
||||
std::lock_guard l(_new_roots_lock);
|
||||
#endif
|
||||
_new_roots[c]--;
|
||||
Logger::log(
|
||||
Logger::MemoryContext,
|
||||
@@ -58,13 +68,14 @@ void MemoryContext::remove_root(Cell *c) {
|
||||
}
|
||||
|
||||
void MemoryContext::gc_thread_entry() {
|
||||
#ifndef NO_THREADS
|
||||
while (!_gc_thread_stop) {
|
||||
{
|
||||
std::unique_lock l(_gc_request_m);
|
||||
_gc_request_cv.wait_for(l, std::chrono::seconds(1), [&] { return _gc_request || _gc_thread_stop; });
|
||||
}
|
||||
if (_gc_thread_stop) return;
|
||||
|
||||
#endif
|
||||
Logger::log(Logger::MemoryContext, [&](std::ostream &out) { out << "gc start "; }, Logger::DEBUG);
|
||||
auto gcstart = std::chrono::high_resolution_clock::now();
|
||||
|
||||
@@ -97,7 +108,9 @@ void MemoryContext::gc_thread_entry() {
|
||||
{
|
||||
decltype(_temp_cells) temp_cells;
|
||||
{
|
||||
#ifndef NO_THREADS
|
||||
std::lock_guard l(_new_roots_lock);
|
||||
#endif
|
||||
std::swap(new_roots, _new_roots);
|
||||
std::swap(temp_cells, _temp_cells);
|
||||
}
|
||||
@@ -147,6 +160,7 @@ void MemoryContext::gc_thread_entry() {
|
||||
Logger::INFO);
|
||||
}
|
||||
|
||||
#ifndef NO_THREADS
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
decltype(_gc_dirty_notif_queue) dirtied;
|
||||
@@ -177,6 +191,7 @@ void MemoryContext::gc_thread_entry() {
|
||||
std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count()),
|
||||
Logger::INFO);
|
||||
}
|
||||
#endif
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
@@ -212,7 +227,7 @@ void MemoryContext::gc_thread_entry() {
|
||||
std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(gcstop - gcstart).count()),
|
||||
Logger::INFO);
|
||||
|
||||
|
||||
#ifndef NO_THREADS
|
||||
{
|
||||
std::unique_lock l(_gc_done_m);
|
||||
std::unique_lock l2(_gc_request_m);
|
||||
@@ -221,6 +236,7 @@ void MemoryContext::gc_thread_entry() {
|
||||
_gc_done_cv.notify_all();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
MemoryContext &MemoryContext::get() {
|
||||
static MemoryContext mc;
|
||||
|
||||
Reference in New Issue
Block a user