@@ -118,14 +118,14 @@ NOTE: In the interpreter's initialization phase, some globals are currently
118
118
(assert(_PyUnicode_CHECK(op)), \
119
119
PyUnicode_IS_COMPACT_ASCII(op) ? \
120
120
((char*)(_PyASCIIObject_CAST(op) + 1)) : \
121
- _PyUnicode_UTF8(op))
121
+ FT_ATOMIC_LOAD_PTR( _PyUnicode_UTF8(op) ))
122
122
#define _PyUnicode_UTF8_LENGTH (op ) \
123
123
(_PyCompactUnicodeObject_CAST(op)->utf8_length)
124
124
#define PyUnicode_UTF8_LENGTH (op ) \
125
125
(assert(_PyUnicode_CHECK(op)), \
126
126
PyUnicode_IS_COMPACT_ASCII(op) ? \
127
127
_PyASCIIObject_CAST(op)->length : \
128
- _PyUnicode_UTF8_LENGTH(op))
128
+ FT_ATOMIC_LOAD_SSIZE_RELAXED( _PyUnicode_UTF8_LENGTH(op) ))
129
129
130
130
#define _PyUnicode_LENGTH (op ) \
131
131
(_PyASCIIObject_CAST(op)->length)
@@ -667,16 +667,16 @@ _PyUnicode_CheckConsistency(PyObject *op, int check_content)
667
667
CHECK (ascii -> state .compact == 0 );
668
668
CHECK (data != NULL );
669
669
if (ascii -> state .ascii ) {
670
- CHECK (compact -> utf8 == data );
671
- CHECK (compact -> utf8_length == ascii -> length );
670
+ CHECK (FT_ATOMIC_LOAD_PTR ( compact -> utf8 ) == data );
671
+ CHECK (FT_ATOMIC_LOAD_SSIZE_RELAXED ( compact -> utf8_length ) == ascii -> length );
672
672
}
673
673
else {
674
- CHECK (compact -> utf8 != data );
674
+ CHECK (FT_ATOMIC_LOAD_PTR ( compact -> utf8 ) != data );
675
675
}
676
676
}
677
677
678
- if (compact -> utf8 == NULL )
679
- CHECK (compact -> utf8_length == 0 );
678
+ if (FT_ATOMIC_LOAD_PTR ( compact -> utf8 ) == NULL )
679
+ CHECK (FT_ATOMIC_LOAD_SSIZE_RELAXED ( compact -> utf8_length ) == 0 );
680
680
}
681
681
682
682
/* check that the best kind is used: O(n) operation */
@@ -5823,7 +5823,11 @@ unicode_fill_utf8(PyObject *unicode)
5823
5823
{
5824
5824
/* the string cannot be ASCII, or PyUnicode_UTF8() would be set */
5825
5825
assert (!PyUnicode_IS_ASCII (unicode ));
5826
-
5826
+ int ret = 0 ;
5827
+ Py_BEGIN_CRITICAL_SECTION (unicode );
5828
+ if (FT_ATOMIC_LOAD_PTR (_PyUnicode_UTF8 (unicode ))) {
5829
+ goto exit ;
5830
+ }
5827
5831
int kind = PyUnicode_KIND (unicode );
5828
5832
const void * data = PyUnicode_DATA (unicode );
5829
5833
Py_ssize_t size = PyUnicode_GET_LENGTH (unicode );
@@ -5849,7 +5853,8 @@ unicode_fill_utf8(PyObject *unicode)
5849
5853
}
5850
5854
if (end == NULL ) {
5851
5855
_PyBytesWriter_Dealloc (& writer );
5852
- return -1 ;
5856
+ ret = -1 ;
5857
+ goto exit ;
5853
5858
}
5854
5859
5855
5860
const char * start = writer .use_small_buffer ? writer .small_buffer :
@@ -5860,14 +5865,17 @@ unicode_fill_utf8(PyObject *unicode)
5860
5865
if (cache == NULL ) {
5861
5866
_PyBytesWriter_Dealloc (& writer );
5862
5867
PyErr_NoMemory ();
5863
- return -1 ;
5868
+ ret = -1 ;
5869
+ goto exit ;
5864
5870
}
5865
- _PyUnicode_UTF8 (unicode ) = cache ;
5866
- _PyUnicode_UTF8_LENGTH (unicode ) = len ;
5867
5871
memcpy (cache , start , len );
5868
5872
cache [len ] = '\0' ;
5873
+ FT_ATOMIC_STORE_SSIZE_RELAXED (_PyUnicode_UTF8_LENGTH (unicode ), len );
5874
+ FT_ATOMIC_STORE_PTR (_PyUnicode_UTF8 (unicode ), cache );
5869
5875
_PyBytesWriter_Dealloc (& writer );
5870
- return 0 ;
5876
+ exit :;
5877
+ Py_END_CRITICAL_SECTION ();
5878
+ return ret ;
5871
5879
}
5872
5880
5873
5881
PyObject *
0 commit comments