Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[WIP] bpo-39465: Mark _Py_Identifier.object as atomic
This PR is basically a test to check if C compilers used by CPython
support C11 _Atomic specifier.
  • Loading branch information
vstinner committed May 25, 2020
commit 5cfdbab1175aec596e16eeafe418eba21e29c768
5 changes: 4 additions & 1 deletion Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
typedef struct _Py_Identifier {
struct _Py_Identifier *next;
const char* string;
PyObject *object;
// bpo-39465: _PyUnicode_FromId() can be called by multiple threads
// running in different interpreters. Use an atomic variable rather
// than a lock for better parallelism.
_Atomic PyObject *object;
} _Py_Identifier;

#define _Py_static_string_init(value) { .next = NULL, .string = value, .object = NULL }
Expand Down
28 changes: 17 additions & 11 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2275,18 +2275,24 @@ PyUnicode_FromString(const char *u)
PyObject *
_PyUnicode_FromId(_Py_Identifier *id)
{
if (!id->object) {
id->object = PyUnicode_DecodeUTF8Stateful(id->string,
strlen(id->string),
NULL, NULL);
if (!id->object)
return NULL;
PyUnicode_InternInPlace(&id->object);
assert(!id->next);
id->next = static_strings;
static_strings = id;
if (id->object) {
return (PyObject *)id->object;
}
return id->object;

PyObject *object;
object = PyUnicode_DecodeUTF8Stateful(id->string,
strlen(id->string),
NULL, NULL);
if (!object) {
return NULL;
}

PyUnicode_InternInPlace(&object);
id->object = (_Atomic PyObject *)object;
assert(!id->next);
id->next = static_strings;
static_strings = id;
return object;
}

static void
Expand Down