Skip to content

Commit

Permalink
DisableGC: replace by CoroutineContext, std::shared_ptr<void>
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickvP authored and Théophane Hufschmitt committed Apr 7, 2023
1 parent 2c53ef1 commit 00bc344
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
20 changes: 11 additions & 9 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,20 +344,22 @@ static Symbol getName(const AttrName & name, EvalState & state, Env & env)
}
}


class BoehmDisableGC : public DisableGC {
#if HAVE_BOEHMGC
/* Disable GC while this object lives. Used by CoroutineContext.
*
* Boehm keeps a count of GC_disable() and GC_enable() calls,
* and only enables GC when the count matches.
*/
class BoehmDisableGC {
public:
BoehmDisableGC() {
#if HAVE_BOEHMGC
GC_disable();
#endif
};
virtual ~BoehmDisableGC() override {
#if HAVE_BOEHMGC
~BoehmDisableGC() {
GC_enable();
#endif
};
};
#endif

static bool gcInitialised = false;

Expand All @@ -384,8 +386,8 @@ void initGC()


/* Used to disable GC when entering coroutines on macOS */
DisableGC::create = []() {
return std::dynamic_pointer_cast<DisableGC>(std::make_shared<BoehmDisableGC>());
create_disable_gc = []() -> std::shared_ptr<void> {
return std::make_shared<BoehmDisableGC>();
};

/* Set the initial heap size to something fairly big (25% of
Expand Down
38 changes: 20 additions & 18 deletions src/libutil/serialise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,21 @@ static DefaultStackAllocator defaultAllocatorSingleton;
StackAllocator *StackAllocator::defaultAllocator = &defaultAllocatorSingleton;


std::shared_ptr<DisableGC> (*DisableGC::create)() = []() {
return std::dynamic_pointer_cast<DisableGC>(std::make_shared<DisableGC>());
std::shared_ptr<void> (*create_disable_gc)() = []() -> std::shared_ptr<void> {
return {};
};

/* This class is used for entry and exit hooks on coroutines */
class CoroutineContext {
#if __APPLE__
/* Disable GC when entering the coroutine on macOS, since it doesn't find the main thread stack in this case.
* std::shared_ptr<void> performs type-erasure, so it will call the right
* deleter. */
const std::shared_ptr<void> disable_gc = create_disable_gc();
#endif
public:
CoroutineContext() {};
~CoroutineContext() {};
};

std::unique_ptr<FinishSink> sourceToSink(std::function<void(Source &)> fun)
Expand All @@ -211,10 +224,7 @@ std::unique_ptr<FinishSink> sourceToSink(std::function<void(Source &)> fun)
cur = in;

if (!coro) {
#if __APPLE__
/* Disable GC when entering the coroutine on macOS, since it doesn't find the main thread stack in this case */
auto disablegc = DisableGC::create();
#endif
CoroutineContext ctx;
coro = coro_t::push_type(VirtualStackAllocator{}, [&](coro_t::pull_type & yield) {
LambdaSource source([&](char *out, size_t out_len) {
if (cur.empty()) {
Expand All @@ -236,9 +246,7 @@ std::unique_ptr<FinishSink> sourceToSink(std::function<void(Source &)> fun)
if (!*coro) { abort(); }

if (!cur.empty()) {
#if __APPLE__
auto disablegc = DisableGC::create();
#endif
CoroutineContext ctx;
(*coro)(false);
}
}
Expand All @@ -248,9 +256,7 @@ std::unique_ptr<FinishSink> sourceToSink(std::function<void(Source &)> fun)
if (!coro) return;
if (!*coro) abort();
{
#if __APPLE__
auto disablegc = DisableGC::create();
#endif
CoroutineContext ctx;
(*coro)(true);
}
if (*coro) abort();
Expand Down Expand Up @@ -284,9 +290,7 @@ std::unique_ptr<Source> sinkToSource(
size_t read(char * data, size_t len) override
{
if (!coro) {
#if __APPLE__
auto disablegc = DisableGC::create();
#endif
CoroutineContext ctx;
coro = coro_t::pull_type(VirtualStackAllocator{}, [&](coro_t::push_type & yield) {
LambdaSink sink([&](std::string_view data) {
if (!data.empty()) yield(std::string(data));
Expand All @@ -299,9 +303,7 @@ std::unique_ptr<Source> sinkToSource(

if (pos == cur.size()) {
if (!cur.empty()) {
#if __APPLE__
auto disablegc = DisableGC::create();
#endif
CoroutineContext ctx;
(*coro)();
}
cur = coro->get();
Expand Down
10 changes: 3 additions & 7 deletions src/libutil/serialise.hh
Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,9 @@ struct StackAllocator {
};

/* Disabling GC when entering a coroutine (on macos).
::create is to avoid boehm gc dependency in libutil.
mutable to avoid boehm gc dependency in libutil.
*/
class DisableGC {
public:
DisableGC() {};
virtual ~DisableGC() {};
static std::shared_ptr<DisableGC> (*create)();
};
extern std::shared_ptr<void> (*create_disable_gc)();


}

0 comments on commit 00bc344

Please sign in to comment.