@@ -1286,13 +1286,10 @@ _Py_call_instrumentation_exc2(
12861286}
12871287
12881288int
1289- _Py_Instrumentation_GetLine (PyCodeObject * code , int index )
1289+ _Py_Instrumentation_GetLine (PyCodeObject * code , _PyCoLineInstrumentationData * line_data , int index )
12901290{
1291- _PyCoMonitoringData * monitoring = code -> _co_monitoring ;
1292- assert (monitoring != NULL );
1293- assert (monitoring -> lines != NULL );
1291+ assert (line_data != NULL );
12941292 assert (index < Py_SIZE (code ));
1295- _PyCoLineInstrumentationData * line_data = monitoring -> lines ;
12961293 int line_delta = get_line_delta (line_data , index );
12971294 int line = compute_line (code , line_delta );
12981295 return line ;
@@ -1310,11 +1307,11 @@ _Py_call_instrumentation_line(PyThreadState *tstate, _PyInterpreterFrame* frame,
13101307 _PyCoMonitoringData * monitoring = code -> _co_monitoring ;
13111308 _PyCoLineInstrumentationData * line_data = monitoring -> lines ;
13121309 PyInterpreterState * interp = tstate -> interp ;
1313- int line = _Py_Instrumentation_GetLine (code , i );
1310+ int line = _Py_Instrumentation_GetLine (code , line_data , i );
13141311 assert (line >= 0 );
13151312 assert (prev != NULL );
13161313 int prev_index = (int )(prev - bytecode );
1317- int prev_line = _Py_Instrumentation_GetLine (code , prev_index );
1314+ int prev_line = _Py_Instrumentation_GetLine (code , line_data , prev_index );
13181315 if (prev_line == line ) {
13191316 int prev_opcode = bytecode [prev_index ].op .code ;
13201317 /* RESUME and INSTRUMENTED_RESUME are needed for the operation of
@@ -1511,11 +1508,9 @@ initialize_tools(PyCodeObject *code)
15111508}
15121509
15131510static void
1514- initialize_lines (PyCodeObject * code , int bytes_per_entry )
1511+ initialize_lines (_PyCoLineInstrumentationData * line_data , PyCodeObject * code , int bytes_per_entry )
15151512{
15161513 ASSERT_WORLD_STOPPED_OR_LOCKED (code );
1517- _PyCoLineInstrumentationData * line_data = code -> _co_monitoring -> lines ;
1518-
15191514 assert (line_data != NULL );
15201515 line_data -> bytes_per_entry = bytes_per_entry ;
15211516 int code_len = (int )Py_SIZE (code );
@@ -1656,18 +1651,19 @@ allocate_instrumentation_data(PyCodeObject *code)
16561651 ASSERT_WORLD_STOPPED_OR_LOCKED (code );
16571652
16581653 if (code -> _co_monitoring == NULL ) {
1659- code -> _co_monitoring = PyMem_Malloc (sizeof (_PyCoMonitoringData ));
1660- if (code -> _co_monitoring == NULL ) {
1654+ _PyCoMonitoringData * monitoring = PyMem_Malloc (sizeof (_PyCoMonitoringData ));
1655+ if (monitoring == NULL ) {
16611656 PyErr_NoMemory ();
16621657 return -1 ;
16631658 }
1664- code -> _co_monitoring -> local_monitors = (_Py_LocalMonitors ){ 0 };
1665- code -> _co_monitoring -> active_monitors = (_Py_LocalMonitors ){ 0 };
1666- code -> _co_monitoring -> tools = NULL ;
1667- code -> _co_monitoring -> lines = NULL ;
1668- code -> _co_monitoring -> line_tools = NULL ;
1669- code -> _co_monitoring -> per_instruction_opcodes = NULL ;
1670- code -> _co_monitoring -> per_instruction_tools = NULL ;
1659+ monitoring -> local_monitors = (_Py_LocalMonitors ){ 0 };
1660+ monitoring -> active_monitors = (_Py_LocalMonitors ){ 0 };
1661+ monitoring -> tools = NULL ;
1662+ monitoring -> lines = NULL ;
1663+ monitoring -> line_tools = NULL ;
1664+ monitoring -> per_instruction_opcodes = NULL ;
1665+ monitoring -> per_instruction_tools = NULL ;
1666+ _Py_atomic_store_ptr_release (& code -> _co_monitoring , monitoring );
16711667 }
16721668 return 0 ;
16731669}
@@ -1732,12 +1728,13 @@ update_instrumentation_data(PyCodeObject *code, PyInterpreterState *interp)
17321728 else {
17331729 bytes_per_entry = 5 ;
17341730 }
1735- code -> _co_monitoring -> lines = PyMem_Malloc (1 + code_len * bytes_per_entry );
1736- if (code -> _co_monitoring -> lines == NULL ) {
1731+ _PyCoLineInstrumentationData * lines = PyMem_Malloc (1 + code_len * bytes_per_entry );
1732+ if (lines == NULL ) {
17371733 PyErr_NoMemory ();
17381734 return -1 ;
17391735 }
1740- initialize_lines (code , bytes_per_entry );
1736+ initialize_lines (lines , code , bytes_per_entry );
1737+ _Py_atomic_store_ptr_release (& code -> _co_monitoring -> lines , lines );
17411738 }
17421739 if (multitools && code -> _co_monitoring -> line_tools == NULL ) {
17431740 code -> _co_monitoring -> line_tools = PyMem_Malloc (code_len );
0 commit comments