From 6d28a8a4d8225b27a7a422cae4ff384b76aec601 Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Sun, 7 Apr 2024 23:11:40 +0200 Subject: [PATCH] No threads option --- src/support/CMakeLists.txt | 4 ---- src/vm/CMakeLists.txt | 10 +++++++--- src/vm/include/MemoryContext.h | 23 ++++++++++++++++++++++- src/vm/src/MemoryContext.cpp | 22 +++++++++++++++++++--- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/support/CMakeLists.txt b/src/support/CMakeLists.txt index c002a45..4fb07b8 100644 --- a/src/support/CMakeLists.txt +++ b/src/support/CMakeLists.txt @@ -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) diff --git a/src/vm/CMakeLists.txt b/src/vm/CMakeLists.txt index 15b076d..f38400f 100644 --- a/src/vm/CMakeLists.txt +++ b/src/vm/CMakeLists.txt @@ -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) diff --git a/src/vm/include/MemoryContext.h b/src/vm/include/MemoryContext.h index dbc8e51..dd16080 100644 --- a/src/vm/include/MemoryContext.h +++ b/src/vm/include/MemoryContext.h @@ -11,10 +11,13 @@ #include #include #include -#include #include #include + +#ifndef NO_THREADS +#include #include +#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 R run_dirty(const std::function)> &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("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)...); { +#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 _roots; std::map _new_roots; +#ifndef NO_THREADS std::recursive_mutex _new_roots_lock; std::set _gc_dirty_notif_queue; @@ -130,6 +150,7 @@ private: std::thread _gc_thread; std::atomic _gc_thread_stop = false; +#endif }; #endif//PSIL_MEMORYCONTEXT_H diff --git a/src/vm/src/MemoryContext.cpp b/src/vm/src/MemoryContext.cpp index 80429ea..c4824d6 100644 --- a/src/vm/src/MemoryContext.cpp +++ b/src/vm/src/MemoryContext.cpp @@ -10,7 +10,11 @@ #include #include -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(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(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;