// Index in PyInterpreterState.unicode.ids.array. It is process-wide
// unique and must be initialized to -1.
Py_ssize_t index;
+ // Hidden PyMutex struct for non free-threaded build.
+ struct {
+ uint8_t v;
+ } mutex;
} _Py_Identifier;
#ifndef Py_BUILD_CORE
PyObject *
_PyUnicode_FromId(_Py_Identifier *id)
{
+ PyMutex_Lock((PyMutex *)&id->mutex);
PyInterpreterState *interp = _PyInterpreterState_GET();
struct _Py_unicode_ids *ids = &interp->unicode.ids;
obj = ids->array[index];
if (obj) {
// Return a borrowed reference
- return obj;
+ goto end;
}
}
obj = PyUnicode_DecodeUTF8Stateful(id->string, strlen(id->string),
NULL, NULL);
if (!obj) {
- return NULL;
+ goto end;
}
PyUnicode_InternInPlace(&obj);
PyObject **new_array = PyMem_Realloc(ids->array, new_size * item_size);
if (new_array == NULL) {
PyErr_NoMemory();
- return NULL;
+ obj = NULL;
+ goto end;
}
memset(&new_array[ids->size], 0, (new_size - ids->size) * item_size);
ids->array = new_array;
// The array stores a strong reference
ids->array[index] = obj;
+end:
+ PyMutex_Unlock((PyMutex *)&id->mutex);
// Return a borrowed reference
return obj;
}