Multithreaded GC

Stop the world solution

How to implement a portable stop the world solution?

In the Eiffel code, each Eiffel C generated routine is equipped with the following code:

if (gc_collecting) {
	current_thread.status = thread_suspended
	gc_collecting_mutex.lock
	current_thread.status = thread_running
	gc_collecting_mutex.unlock
}

For all routines from the thread library that might block we have:

current_thread.status = blocked;
CALL TO BLOCKING STUFF
if (gc_collecting) {
	current_thread.status = thread_suspended
	gc_collecting_mutex.lock
	current_thread.status = thread_running
	gc_collecting_mutex.unlock
} else {
	current_thread.status = thread_running
}

In the code which performs the GC we have the following:

current_thread.status = thread_gc_starting

gc_collecting_mutex.lock
current_thread.status = thread_gc_started
gc_collecting = true

	// Wait until all threads are in a GC safe point.
Loop until all threads but current one are marked `suspended' or `starting_gc' or `blocked'.

Perform GC operation

gc_collecting = false

current_thread.status = thread_running
gc_collecting_mutex.unlock

Of course the members are:

volatile int gc_collecting = 0;