@@ -261,41 +261,51 @@ class V8_EXPORT_PRIVATE WasmCode final {
261261 ~WasmCode ();
262262
263263 void IncRef () {
264- [[maybe_unused]] int old_val =
265- ref_count_.fetch_add (1 , std::memory_order_acq_rel);
266- DCHECK_LE (1 , old_val);
267- DCHECK_GT (kMaxInt , old_val);
264+ [[maybe_unused]] uint32_t old_field =
265+ ref_count_bitfield_.fetch_add (1 , std::memory_order_acq_rel);
266+ DCHECK_LE (1 , refcount (old_field));
267+ DCHECK_GT (kMaxInt , refcount (old_field));
268+ }
269+
270+ // Returns true if the refcount was incremented, false if {this->is_dying()}.
271+ bool IncRefIfNotDying () {
272+ uint32_t old_field = ref_count_bitfield_.load (std::memory_order_acquire);
273+ while (true ) {
274+ if (is_dying (old_field)) return false ;
275+ if (ref_count_bitfield_.compare_exchange_weak (
276+ old_field, old_field + 1 , std::memory_order_acq_rel)) {
277+ return true ;
278+ }
279+ }
268280 }
269281
270282 // Decrement the ref count. Returns whether this code becomes dead and needs
271283 // to be freed.
272284 V8_WARN_UNUSED_RESULT bool DecRef () {
273- int old_count = ref_count_ .load (std::memory_order_acquire);
285+ uint32_t old_field = ref_count_bitfield_ .load (std::memory_order_acquire);
274286 while (true ) {
275- DCHECK_LE (1 , old_count );
276- if (V8_UNLIKELY (old_count == 1 )) {
277- if (is_dying ()) {
287+ DCHECK_LE (1 , refcount (old_field) );
288+ if (V8_UNLIKELY (refcount (old_field) == 1 )) {
289+ if (is_dying (old_field )) {
278290 // The code was already on the path to deletion, only temporary
279291 // C++ references to it are left. Decrement the refcount, and
280292 // return true if it drops to zero.
281293 return DecRefOnDeadCode ();
282294 }
283295 // Otherwise, the code enters the path to destruction now.
284- mark_as_dying ();
285- old_count = ref_count_. load (std::memory_order_acquire);
286- if ( V8_LIKELY (old_count == 1 )) {
296+ if (ref_count_bitfield_. compare_exchange_weak (
297+ old_field, old_field | kIsDyingMask ,
298+ std::memory_order_acq_rel )) {
287299 // No other thread got in the way. Commit to the decision.
288300 DecRefOnPotentiallyDeadCode ();
289301 return false ;
290302 }
291- // Another thread managed to increment the refcount again, just
292- // before we set the "dying" bit. So undo that, and resume the
293- // loop to evaluate again what needs to be done.
294- undo_mark_as_dying ();
303+ // Another thread interfered. Re-evaluate what to do.
304+ continue ;
295305 }
296- DCHECK_LT (1 , old_count );
297- if (ref_count_ .compare_exchange_weak (old_count, old_count - 1 ,
298- std::memory_order_acq_rel)) {
306+ DCHECK_LT (1 , refcount (old_field) );
307+ if (ref_count_bitfield_ .compare_exchange_weak (
308+ old_field, old_field - 1 , std::memory_order_acq_rel)) {
299309 return false ;
300310 }
301311 }
@@ -304,16 +314,18 @@ class V8_EXPORT_PRIVATE WasmCode final {
304314 // Decrement the ref count on code that is known to be in use (i.e. the ref
305315 // count cannot drop to zero here).
306316 void DecRefOnLiveCode () {
307- [[maybe_unused]] int old_count =
308- ref_count_ .fetch_sub (1 , std::memory_order_acq_rel);
309- DCHECK_LE (2 , old_count );
317+ [[maybe_unused]] uint32_t old_bitfield_value =
318+ ref_count_bitfield_ .fetch_sub (1 , std::memory_order_acq_rel);
319+ DCHECK_LE (2 , refcount (old_bitfield_value) );
310320 }
311321
312322 // Decrement the ref count on code that is known to be dead, even though there
313323 // might still be C++ references. Returns whether this drops the last
314324 // reference and the code needs to be freed.
315325 V8_WARN_UNUSED_RESULT bool DecRefOnDeadCode () {
316- return ref_count_.fetch_sub (1 , std::memory_order_acq_rel) == 1 ;
326+ uint32_t old_bitfield_value =
327+ ref_count_bitfield_.fetch_sub (1 , std::memory_order_acq_rel);
328+ return refcount (old_bitfield_value) == 1 ;
317329 }
318330
319331 // Decrement the ref count on a set of {WasmCode} objects, potentially
@@ -322,9 +334,9 @@ class V8_EXPORT_PRIVATE WasmCode final {
322334
323335 // Called by the WasmEngine when it shuts down for code it thinks is
324336 // probably dead (i.e. is in the "potentially_dead_code_" set). Wrapped
325- // in a method only because {ref_count_ } is private.
337+ // in a method only because {ref_count_bitfield_ } is private.
326338 void DcheckRefCountIsOne () {
327- DCHECK_EQ (1 , ref_count_ .load (std::memory_order_acquire));
339+ DCHECK_EQ (1 , refcount (ref_count_bitfield_ .load (std::memory_order_acquire) ));
328340 }
329341
330342 // Returns the last source position before {offset}.
@@ -341,7 +353,15 @@ class V8_EXPORT_PRIVATE WasmCode final {
341353 return ForDebuggingField::decode (flags_);
342354 }
343355
344- bool is_dying () const { return dying_.load (std::memory_order_acquire); }
356+ bool is_dying () const {
357+ return is_dying (ref_count_bitfield_.load (std::memory_order_acquire));
358+ }
359+ static bool is_dying (uint32_t bit_field_value) {
360+ return (bit_field_value & kIsDyingMask ) != 0 ;
361+ }
362+ static uint32_t refcount (uint32_t bit_field_value) {
363+ return bit_field_value & ~kIsDyingMask ;
364+ }
345365
346366 // Returns {true} for Liftoff code that sets up a feedback vector slot in its
347367 // stack frame.
@@ -428,11 +448,6 @@ class V8_EXPORT_PRIVATE WasmCode final {
428448 // for consideration in the next Code GC cycle.
429449 V8_NOINLINE void DecRefOnPotentiallyDeadCode ();
430450
431- void mark_as_dying () { dying_.store (true , std::memory_order_release); }
432- // This is rarely necessary to mitigate a race condition. See the comment
433- // at its (only) call site.
434- void undo_mark_as_dying () { dying_.store (false , std::memory_order_release); }
435-
436451 NativeModule* const native_module_ = nullptr ;
437452 uint8_t * const instructions_;
438453 const uint64_t signature_hash_;
@@ -477,10 +492,6 @@ class V8_EXPORT_PRIVATE WasmCode final {
477492 using ForDebuggingField = ExecutionTierField::Next<ForDebugging, 2 >;
478493 using FrameHasFeedbackSlotField = ForDebuggingField::Next<bool , 1 >;
479494
480- // Will be set to {true} the first time this code object is considered
481- // "potentially dead" (to be confirmed by the next Wasm Code GC cycle).
482- std::atomic<bool > dying_{false };
483-
484495 // WasmCode is ref counted. Counters are held by:
485496 // 1) The jump table / code table.
486497 // 2) {WasmCodeRefScope}s.
@@ -491,7 +502,11 @@ class V8_EXPORT_PRIVATE WasmCode final {
491502 // it's being used. Once the ref count drops to zero (i.e. after being removed
492503 // from (3) and all (2)), the code object is deleted and the memory for the
493504 // machine code is freed.
494- std::atomic<int > ref_count_{1 };
505+ // The topmost bit is used to indicate that the code is in (3). It is stored
506+ // in this same field to avoid race conditions between atomic updates to
507+ // that state and the refcount.
508+ static constexpr uint32_t kIsDyingMask = 0x8000'0000u ;
509+ std::atomic<uint32_t > ref_count_bitfield_{1 };
495510};
496511
497512WasmCode::Kind GetCodeKind (const WasmCompilationResult& result);
@@ -1238,6 +1253,10 @@ class V8_EXPORT_PRIVATE V8_NODISCARD WasmCodeRefScope {
12381253 // Register a {WasmCode} reference in the current {WasmCodeRefScope}. Fails if
12391254 // there is no current scope.
12401255 static void AddRef (WasmCode*);
1256+ // Same, but conditional:
1257+ // - if the {code} is marked as dying, do nothing, return nullptr.
1258+ // - otherwise add a ref and return {code}.
1259+ static WasmCode* AddRefIfNotDying (WasmCode* code);
12411260
12421261 private:
12431262 WasmCodeRefScope* const previous_scope_;
0 commit comments