4040
4141#ifdef Py_GIL_DISABLED
4242
43- #define LOCK_CODE (code ) \
44- assert(!_PyInterpreterState_GET()->stoptheworld.world_stopped); \
45- Py_BEGIN_CRITICAL_SECTION(code)
46-
47- #define UNLOCK_CODE () Py_END_CRITICAL_SECTION()
48-
4943#define MODIFY_BYTECODE (code , func , ...) \
5044 do { \
45+ ASSERT_WORLD_STOPPED(); \
5146 PyCodeObject *co = (code); \
5247 for (Py_ssize_t i = 0; i < code->co_tlbc->size; i++) { \
5348 char *bc = co->co_tlbc->entries[i]; \
6055
6156#else
6257
63- #define LOCK_CODE (code )
64- #define UNLOCK_CODE ()
6558#define MODIFY_BYTECODE (code , func , ...) \
6659 (func)(code, _PyCode_CODE(code), __VA_ARGS__)
6760
@@ -535,7 +528,7 @@ valid_opcode(int opcode)
535528static void
536529sanity_check_instrumentation (PyCodeObject * code )
537530{
538- ASSERT_WORLD_STOPPED_OR_LOCKED ( code );
531+ ASSERT_WORLD_STOPPED ( );
539532
540533 _PyCoMonitoringData * data = code -> _co_monitoring ;
541534 if (data == NULL ) {
@@ -945,7 +938,7 @@ add_per_instruction_tools(PyCodeObject * code, int offset, int tools)
945938static void
946939remove_per_instruction_tools (PyCodeObject * code , int offset , int tools )
947940{
948- ASSERT_WORLD_STOPPED_OR_LOCKED ( code );
941+ ASSERT_WORLD_STOPPED ( );
949942
950943 _PyCoMonitoringData * monitoring = code -> _co_monitoring ;
951944 assert (code -> _co_monitoring );
@@ -1060,15 +1053,15 @@ set_global_version(PyThreadState *tstate, uint32_t version)
10601053static bool
10611054is_version_up_to_date (PyCodeObject * code , PyInterpreterState * interp )
10621055{
1063- ASSERT_WORLD_STOPPED_OR_LOCKED ( code );
1056+ ASSERT_WORLD_STOPPED ( );
10641057 return global_version (interp ) == code -> _co_instrumentation_version ;
10651058}
10661059
10671060#ifndef NDEBUG
10681061static bool
10691062instrumentation_cross_checks (PyInterpreterState * interp , PyCodeObject * code )
10701063{
1071- ASSERT_WORLD_STOPPED_OR_LOCKED ( code );
1064+ ASSERT_WORLD_STOPPED ( );
10721065 _Py_LocalMonitors expected = local_union (
10731066 interp -> monitors ,
10741067 code -> _co_monitoring -> local_monitors );
@@ -1079,10 +1072,10 @@ static int
10791072debug_check_sanity (PyInterpreterState * interp , PyCodeObject * code )
10801073{
10811074 int res ;
1082- LOCK_CODE ( code );
1075+ _PyEval_StopTheWorld ( interp );
10831076 res = is_version_up_to_date (code , interp ) &&
10841077 instrumentation_cross_checks (interp , code );
1085- UNLOCK_CODE ( );
1078+ _PyEval_StartTheWorld ( interp );
10861079 return res ;
10871080}
10881081
@@ -1381,9 +1374,9 @@ _Py_call_instrumentation_line(PyThreadState *tstate, _PyInterpreterFrame* frame,
13811374 }
13821375 else {
13831376 /* DISABLE */
1384- LOCK_CODE ( code );
1377+ _PyEval_StopTheWorld ( tstate -> interp );
13851378 remove_line_tools (code , i , 1 << tool );
1386- UNLOCK_CODE ( );
1379+ _PyEval_StartTheWorld ( tstate -> interp );
13871380 }
13881381 } while (tools );
13891382 Py_DECREF (line_obj );
@@ -1438,9 +1431,9 @@ _Py_call_instrumentation_instruction(PyThreadState *tstate, _PyInterpreterFrame*
14381431 }
14391432 else {
14401433 /* DISABLE */
1441- LOCK_CODE ( code );
1434+ _PyEval_StopTheWorld ( tstate -> interp );
14421435 remove_per_instruction_tools (code , offset , 1 << tool );
1443- UNLOCK_CODE ( );
1436+ _PyEval_StartTheWorld ( tstate -> interp );
14441437 }
14451438 }
14461439 Py_DECREF (offset_obj );
@@ -1451,7 +1444,7 @@ _Py_call_instrumentation_instruction(PyThreadState *tstate, _PyInterpreterFrame*
14511444static void
14521445initialize_tools (PyCodeObject * code )
14531446{
1454- ASSERT_WORLD_STOPPED_OR_LOCKED ( code );
1447+ ASSERT_WORLD_STOPPED ( );
14551448 uint8_t * tools = code -> _co_monitoring -> tools ;
14561449
14571450 assert (tools != NULL );
@@ -1507,7 +1500,7 @@ initialize_tools(PyCodeObject *code)
15071500static void
15081501initialize_lines (PyCodeObject * code , int bytes_per_entry )
15091502{
1510- ASSERT_WORLD_STOPPED_OR_LOCKED ( code );
1503+ ASSERT_WORLD_STOPPED ( );
15111504 _PyCoLineInstrumentationData * line_data = code -> _co_monitoring -> lines ;
15121505
15131506 assert (line_data != NULL );
@@ -1634,7 +1627,7 @@ initialize_lines(PyCodeObject *code, int bytes_per_entry)
16341627static void
16351628initialize_line_tools (PyCodeObject * code , _Py_LocalMonitors * all_events )
16361629{
1637- ASSERT_WORLD_STOPPED_OR_LOCKED ( code );
1630+ ASSERT_WORLD_STOPPED ( );
16381631 uint8_t * line_tools = code -> _co_monitoring -> line_tools ;
16391632
16401633 assert (line_tools != NULL );
@@ -1647,7 +1640,7 @@ initialize_line_tools(PyCodeObject *code, _Py_LocalMonitors *all_events)
16471640static int
16481641allocate_instrumentation_data (PyCodeObject * code )
16491642{
1650- ASSERT_WORLD_STOPPED_OR_LOCKED ( code );
1643+ ASSERT_WORLD_STOPPED ( );
16511644
16521645 if (code -> _co_monitoring == NULL ) {
16531646 code -> _co_monitoring = PyMem_Malloc (sizeof (_PyCoMonitoringData ));
@@ -1669,7 +1662,7 @@ allocate_instrumentation_data(PyCodeObject *code)
16691662static int
16701663update_instrumentation_data (PyCodeObject * code , PyInterpreterState * interp )
16711664{
1672- ASSERT_WORLD_STOPPED_OR_LOCKED ( code );
1665+ ASSERT_WORLD_STOPPED ( );
16731666
16741667 int code_len = (int )Py_SIZE (code );
16751668 if (allocate_instrumentation_data (code )) {
@@ -2995,9 +2988,10 @@ branch_handler_vectorcall(
29952988 // Orphaned NOT_TAKEN -- Jump removed by the compiler
29962989 return res ;
29972990 }
2998- LOCK_CODE (code );
2991+ PyInterpreterState * interp = _PyInterpreterState_GET ();
2992+ _PyEval_StopTheWorld (interp );
29992993 remove_tools (code , offset , other_event , 1 << self -> tool_id );
3000- UNLOCK_CODE ( );
2994+ _PyEval_StartTheWorld ( interp );
30012995 }
30022996 return res ;
30032997}
0 commit comments