Skip to content

Commit

Permalink
Update emscripten config to match standard installations. Add some em…
Browse files Browse the repository at this point in the history
…scipten specific GC paths.
  • Loading branch information
hughsando committed Aug 29, 2024
1 parent 9c8cc14 commit 17ac6e8
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 69 deletions.
147 changes: 104 additions & 43 deletions src/hx/gc/Immix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
#include "GcRegCapture.h"
#include <hx/Unordered.h>

#ifdef EMSCRIPTEN
#include <emscripten/stack.h>
#ifdef HXCPP_SINGLE_THREADED_APP
// Use provided tools to measure stack extent
#define HXCPP_EXPLICIT_STACK_EXTENT
#endif
#endif

#include <string>
#include <stdlib.h>

Expand All @@ -28,7 +36,6 @@ int gSlowPath = 0;
using hx::gByteMarkID;
using hx::gRememberedByteMarkID;

// #define HXCPP_SINGLE_THREADED_APP

namespace hx
{
Expand Down Expand Up @@ -108,10 +115,6 @@ static size_t sgMaximumFreeSpace = 1024*1024*1024;
static size_t sgMaximumFreeSpace = 1024*1024*1024;
#endif

#ifdef EMSCRIPTEN
// #define HXCPP_STACK_UP
#endif


// #define HXCPP_GC_DEBUG_LEVEL 1

Expand Down Expand Up @@ -5762,8 +5765,10 @@ class LocalAllocator : public hx::StackContext

bool mMoreHoles;

#ifndef HXCPP_EXPLICIT_STACK_EXTENT
int *mTopOfStack;
int *mBottomOfStack;
#endif

hx::RegisterCaptureBuffer mRegisterBuf;
int mRegisterBufSize;
Expand Down Expand Up @@ -5801,7 +5806,9 @@ class LocalAllocator : public hx::StackContext

void AttachThread(int *inTopOfStack)
{
#ifndef HXCPP_EXPLICIT_STACK_EXTENT
mTopOfStack = mBottomOfStack = inTopOfStack;
#endif

mRegisterBufSize = 0;
mStackLocks = 0;
Expand Down Expand Up @@ -5859,7 +5866,9 @@ class LocalAllocator : public hx::StackContext
}
#endif

#ifndef HXCPP_EXPLICIT_STACK_EXTENT
mTopOfStack = mBottomOfStack = 0;
#endif

sGlobalAlloc->RemoveLocalLocked(this);

Expand Down Expand Up @@ -5891,7 +5900,7 @@ class LocalAllocator : public hx::StackContext
// Other places may call this to ensure the the current thread is registered
// indefinitely (until forcefully revoked)
//
// Normally liraries/mains will then let this dangle.
// Normally libraries/mains will then let this dangle.
//
// However after the main, on android it calls SetTopOfStack(0,true), to unregister the thread,
// because it is likely to be the ui thread, and the remaining call will be from
Expand All @@ -5916,13 +5925,28 @@ class LocalAllocator : public hx::StackContext
// SetTopOfStack(top,true) -> add stack lock
// SetTopOfStack(0,_) -> pop stack lock. If all gone, clear global stack lock
//

#ifdef HXCPP_EXPLICIT_STACK_EXTENT // {

void SetTopOfStack(int *inTop,bool inPush) { }
void PushTopOfStack(void *inTop) { }
void PopTopOfStack() { }
void SetBottomOfStack(int *inBottom) { }
void PauseForCollect() { }
void EnterGCFreeZone() { }
bool TryGCFreeZone() { return true; }
bool TryExitGCFreeZone() { return false; }
void ExitGCFreeZoneLocked() { }


#else // } !HXCPP_EXPLICIT_STACK_EXTENT {
void SetTopOfStack(int *inTop,bool inPush)
{
if (inTop)
{
if (!mTopOfStack)
mTopOfStack = inTop;
// EMSCRIPTEN the stack grows upwards
// EMSCRIPTEN the stack grows upwards - not wasm.
// It could be that the main routine was called from deep with in the stack,
// then some callback was called from a higher location on the stack
#ifdef HXCPP_STACK_UP
Expand Down Expand Up @@ -5987,39 +6011,6 @@ class LocalAllocator : public hx::StackContext
#endif
}

virtual void SetupStackAndCollect(bool inMajor, bool inForceCompact, bool inLocked=false,bool inFreeIsFragged=false)
{
#ifndef HXCPP_SINGLE_THREADED_APP
#if HXCPP_DEBUG
if (mGCFreeZone)
CriticalGCError("Collecting from a GC-free thread");
#endif
#endif

volatile int dummy = 1;
mBottomOfStack = (int *)&dummy;

CAPTURE_REGS;

if (!mTopOfStack)
mTopOfStack = mBottomOfStack;
// EMSCRIPTEN the stack grows upwards
#ifdef HXCPP_STACK_UP
if (mBottomOfStack < mTopOfStack)
mTopOfStack = mBottomOfStack;
#else
if (mBottomOfStack > mTopOfStack)
mTopOfStack = mBottomOfStack;
#endif

#ifdef VerifyStackRead
VerifyStackRead(mBottomOfStack, mTopOfStack)
#endif


sGlobalAlloc->Collect(inMajor, inForceCompact, inLocked, inFreeIsFragged);
}


void PauseForCollect()
{
Expand Down Expand Up @@ -6145,6 +6136,58 @@ class LocalAllocator : public hx::StackContext
#endif
}

#endif // } HXCPP_EXPLICIT_STACK_EXTENT


void SetupStackAndCollect(bool inMajor, bool inForceCompact, bool inLocked=false,bool inFreeIsFragged=false)
{
#ifndef HXCPP_SINGLE_THREADED_APP
#if HXCPP_DEBUG
if (mGCFreeZone)
CriticalGCError("Collecting from a GC-free thread");
#endif
#endif

#ifndef HXCPP_EXPLICIT_STACK_EXTENT
volatile int dummy = 1;
mBottomOfStack = (int *)&dummy;

CAPTURE_REGS;

if (!mTopOfStack)
mTopOfStack = mBottomOfStack;

// EMSCRIPTEN the stack grows upwards
#ifdef HXCPP_STACK_UP
if (mBottomOfStack < mTopOfStack)
mTopOfStack = mBottomOfStack;
#else
if (mBottomOfStack > mTopOfStack)
mTopOfStack = mBottomOfStack;
#endif

#ifdef VerifyStackRead
VerifyStackRead(mBottomOfStack, mTopOfStack)
#endif

#endif


sGlobalAlloc->Collect(inMajor, inForceCompact, inLocked, inFreeIsFragged);
}













void ExpandAlloc(int &ioSize)
{
#ifdef HXCPP_GC_NURSERY
Expand Down Expand Up @@ -6331,11 +6374,13 @@ class LocalAllocator : public hx::StackContext

void Mark(hx::MarkContext *__inCtx)
{
#ifndef HXCPP_SINGLE_THREADED_APP
if (!mTopOfStack)
{
Reset();
return;
}
#endif

#ifdef SHOW_MEM_EVENTS
//int here = 0;
Expand All @@ -6345,19 +6390,33 @@ class LocalAllocator : public hx::StackContext
#ifdef HXCPP_DEBUG
MarkPushClass("Stack",__inCtx);
MarkSetMember("Stack",__inCtx);

#ifdef HXCPP_EXPLICIT_STACK_EXTENT
hx::MarkConservative( (int *)emscripten_stack_get_current(),(int *)emscripten_stack_get_base(), __inCtx);
#else
if (mTopOfStack && mBottomOfStack)
hx::MarkConservative(mBottomOfStack, mTopOfStack , __inCtx);
#endif

#ifdef HXCPP_SCRIPTABLE
MarkSetMember("ScriptStack",__inCtx);
hx::MarkConservative((int *)(stack), (int *)(pointer),__inCtx);
#endif
MarkSetMember("Registers",__inCtx);
hx::MarkConservative(CAPTURE_REG_START, CAPTURE_REG_END, __inCtx);


MarkPopClass(__inCtx);
#else
if (mTopOfStack && mBottomOfStack)
hx::MarkConservative(mBottomOfStack, mTopOfStack , __inCtx);
hx::MarkConservative(CAPTURE_REG_START, CAPTURE_REG_END, __inCtx);

#ifdef HXCPP_EXPLICIT_STACK_EXTENT
hx::MarkConservative( (int *)emscripten_stack_get_current(), (int *) emscripten_stack_get_base(), __inCtx);
#else
if (mTopOfStack && mBottomOfStack)
hx::MarkConservative(mBottomOfStack, mTopOfStack , __inCtx);
hx::MarkConservative(CAPTURE_REG_START, CAPTURE_REG_END, __inCtx);
#endif

#ifdef HXCPP_SCRIPTABLE
hx::MarkConservative((int *)(stack), (int *)(pointer),__inCtx);
#endif
Expand Down Expand Up @@ -6390,6 +6449,7 @@ inline LocalAllocator *GetLocalAlloc(bool inAllowEmpty=false)
#endif
}

#ifndef HXCPP_SINGLE_THREADED_APP
void WaitForSafe(LocalAllocator *inAlloc)
{
inAlloc->WaitForSafe();
Expand All @@ -6399,6 +6459,7 @@ void ReleaseFromSafe(LocalAllocator *inAlloc)
{
inAlloc->ReleaseFromSafe();
}
#endif

void MarkLocalAlloc(LocalAllocator *inAlloc,hx::MarkContext *__inCtx)
{
Expand Down
Loading

0 comments on commit 17ac6e8

Please sign in to comment.