Skip to content

Commit 6798366

Browse files
committed
Make set cache as atomic operation
1 parent e32f2f0 commit 6798366

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

Python/specialize.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,21 @@ set_opcode(_Py_CODEUNIT *instr, uint8_t opcode)
689689
#endif
690690
}
691691

692+
static inline int
693+
set_cache_verison(uint16_t *old_version, uint32_t new_version)
694+
{
695+
#ifdef Py_GIL_DISABLED
696+
uint32_t old_version_val = read_u32(old_version);
697+
if (!_Py_atomic_compare_exchange_uint32((uint32_t *)old_version, &old_version_val, new_version)) {
698+
return 0;
699+
}
700+
return 1;
701+
#else
702+
write_u32(cache, new_version);
703+
return 1;
704+
#endif
705+
}
706+
692707
static inline void
693708
set_counter(_Py_BackoffCounter *counter, _Py_BackoffCounter value)
694709
{
@@ -2742,8 +2757,17 @@ _Py_Specialize_ToBool(_PyStackRef value_o, _Py_CODEUNIT *instr)
27422757
unspecialize(instr, SPEC_FAIL_OUT_OF_VERSIONS);
27432758
return;
27442759
}
2760+
#ifdef Py_GIL_DISABLED
2761+
if (read_u32(cache->version) > version) {
2762+
unspecialize(instr, SPEC_FAIL_OUT_OF_VERSIONS);
2763+
return;
2764+
}
2765+
#endif
2766+
if (!set_cache_verison(cache->version, version)) {
2767+
unspecialize(instr, SPEC_FAIL_OUT_OF_VERSIONS);
2768+
return;
2769+
}
27452770
specialize(instr, TO_BOOL_ALWAYS_TRUE);
2746-
write_u32(cache->version, version);
27472771
assert(version);
27482772
return;
27492773
}

0 commit comments

Comments
 (0)