Skip to content

Commit

Permalink
Allocate VM stack outside of ThreadLocalState
Browse files Browse the repository at this point in the history
Having a large array inside the VirtualMachine (and thus the
ThreadLocalState) seems to crash the linker on Mac. It's also just
sort of wonky.

Probably ideally we'd have a more sophisticated setup here with
mmap and guard pages and growth and yada yada, but this works ok.
There is a definite suboptimality in allocating the entire max
stack size all at once, but it's not _too_ big so maybe it's ok.
It also means that the GC will walk the entire stack for pointers,
including the inactive part, but Boehm is probably not capable
of doing something more complex anyway. Or if it is it's wizardry.
  • Loading branch information
Bike committed Jul 27, 2023
1 parent 3a84fc0 commit 760aa5b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
7 changes: 5 additions & 2 deletions include/clasp/gctools/threadlocal.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct VirtualMachine {
// grow and etc.
static constexpr size_t MaxStackWords = 65536;
bool _Running;
core::T_O* _stackBottom[MaxStackWords];
core::T_O** _stackBottom;
size_t _stackBytes;
core::T_O** _stackTop;
core::T_O** _stackGuard;
Expand All @@ -112,7 +112,8 @@ struct VirtualMachine {

void enable_guards();
void disable_guards();


void startup();
inline void shutdown() {
this->_Running = false;
}
Expand Down Expand Up @@ -375,6 +376,8 @@ struct VirtualMachine {
void pushObjectFile(llvmo::ObjectFile_sp of);
void popObjectFile();
inline DynamicBindingStack& bindings() { return this->_Bindings; };

void startUpVM();

~ThreadLocalState();
};
Expand Down
1 change: 1 addition & 0 deletions src/core/mpPackage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ void start_thread_inner(uintptr_t uniqueId, void* cold_end_of_stack) {
#endif
gctools::ThreadLocalStateLowLevel thread_local_state_low_level(cold_end_of_stack);
core::ThreadLocalState thread_local_state;
thread_local_state.startUpVM();
my_thread_low_level = &thread_local_state_low_level;
my_thread = &thread_local_state;
// printf("%s:%d entering start_thread &my_thread -> %p \n", __FILE__, __LINE__, (void*)&my_thread);
Expand Down
1 change: 1 addition & 0 deletions src/gctools/boehmGarbageCollection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ void startupBoehm(gctools::ClaspInfo* claspInfo ) {
GC_get_stack_base(&gc_stack_base);
GC_register_my_thread(&gc_stack_base);
#endif
thread_local_stateP->startUpVM();
core::global_options = new core::CommandLineOptions(claspInfo->_argc, claspInfo->_argv);

#ifndef SCRAPING
Expand Down
10 changes: 9 additions & 1 deletion src/gctools/threadlocal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ VirtualMachine::VirtualMachine() :
,_unwind_counter(0)
,_throw_counter(0)
#endif
{
{}

void VirtualMachine::startup() {
size_t stackSpace = VirtualMachine::MaxStackWords*sizeof(T_O*);
this->_stackBottom = (T_O**)GC_MALLOC_UNCOLLECTABLE(stackSpace);
this->_stackTop = this->_stackBottom+VirtualMachine::MaxStackWords-1;
// printf("%s:%d:%s vm._stackTop = %p\n", __FILE__, __LINE__, __FUNCTION__, this->_stackTop );
size_t pageSize = getpagesize();
Expand Down Expand Up @@ -203,6 +206,7 @@ VirtualMachine::~VirtualMachine() {
#if 1
this->disable_guards();
#endif
GC_FREE(this->_stackBottom);
}


Expand Down Expand Up @@ -427,6 +431,10 @@ void ThreadLocalState::popObjectFile() {
SIMPLE_ERROR("There were no more object files");
}

void ThreadLocalState::startUpVM() {
this->_VM.startup();
}

ThreadLocalState::~ThreadLocalState() {
}

Expand Down

0 comments on commit 760aa5b

Please sign in to comment.