Many programming languages native C APIs with tracing garbage collectors have a root and unroot functions to protect temporary values from being collected.
In a multithreaded environment, if a native function unroot all of its objects and then return one of said objects, what happens if between unrooting and returning, another thread allocates causing a collection? Will the value that I'm trying to return be collected as well?
I've looked at Ruby, Julia, Janet and V8 for solutions, but I couldn't find nothing obvious regarding this. Is it safe to just to just use pairs of root and unroot functions in a multithreaded environment?
I'm trying to build a simple stop the world mark and sweep garbage collector for my scripting language, and I'm trying to make everything that needs to be thread safe, safe. Currently my garbage collector has two singly linked lists, one for allocated objects and the other for roots, protected by a global mutex.