Hi v8-users, I'm trying to understand how to correctly instrument my wrappers for multi-threaded garbage collection, and it seems I'm doing something wrong. Currently, my code seems to work only in --predictable mode (single-threaded), but I'd like to take advantage of GC in a background thread.
When in multi-threaded mode, I observe that occasionally, when V8 traces into a particular wrapper, the weak handles held by that wrapper have already been collected. The tracing is happening, but V8 seemingly prematurely collects some handles before I get a chance to mark them. Here are the things I've done: Per-object: - When I create a new wrapper object, I give it two internal fields set to the native object pointer and a tracing callback function. - The tracing callback, when run, invokes RegisterExternalReference() on each persistent handle reachable through the native object. - Once the internal pointers are set, I invoke SetWeak() (with no arguments) on all handles reachable through the native object. Globally: - I call AddGCPrologueCallback() to register a callback for v8::kGCTypeScavenge. - In the scavenge callback, I call VisitWeakHandles() with a visitor that calls MarkActive() on every handle. - I call SetEmbedderHeapTracer() to register my own heap tracer implementation. - In the heap tracer's RegisterV8References() method, I invoke each tracing callback using the pair of internal pointers (which calls RegisterExternalReference() on reachable weak handles, as described above). - Currently, I don't do anything in AdvanceTracing() -- I complete all tracing before RegisterV8References() returns. (It appears this is what Blink does in its unified_heap_controller.cc, so I followed the example.) However, if I instead only save a copy of the pointer list in RegisterV8References() and then do the tracing later in AdvanceTracing(), then I observe problems with prematurely-collected handles more often. I'd guess this is because the underlying problem is a race condition, in which another thread is collecting those handles before I get a chance to trace them. - I don't do anything in any of the other EmbedderHeapTracer callbacks. Anything I'm missing here? I haven't been able to find any documentation on how to use these interfaces. Please let me know if there are docs I missed. Thanks, -Kenton -- -- v8-users mailing list v8-users@googlegroups.com http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.