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