ysbaddaden / gc

A garbage collector for Crystal
93 stars 5 forks source link

Thread safety #14

Closed ysbaddaden closed 2 years ago

ysbaddaden commented 2 years ago

Creates a LocalAllocator for each thread and protects GlobalAllocator for concurrent function calls with a mutex, but doesn't stop the world to start a collection (yet)!

Introduces a new Array data structure to hold the full list of LocalAllocator, so we can reset (and free) them.

Other methods seem to be thread safe because they either only touch the thread LocalAllocator, or will delegate to the GlobalAllocator, that is protected. Even GC_realloc should be safe, even when moving the finalizer, because we can safely access the object from the thread, but also because we still reference the old pointer, so even if a collect starts, neither the old nor the new object shall be collected (the old one will be in the next collect).

closes #7 closes #19

ysbaddaden commented 2 years ago

Things left to do:

  1. write tests for Array_each() and Array_delete();
  2. start some threads, call GC_malloc() concurrently, and wait for them to segfault :grin:
  3. find out how to stop-the-world to run the collector safely (we can send a signal to each thread);
  4. find out how to save the context (i.e. CPU registers) for the fibers currently running in stopped threads.