Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
DinoV committed Jan 16, 2024
1 parent ac880c8 commit 53967a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
3 changes: 2 additions & 1 deletion Include/internal/pycore_critical_section.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ _PyCriticalSection_SuspendAll(PyThreadState *tstate);
#ifdef Py_GIL_DISABLED

static inline void
_PyCriticalSection_AssertHeld(PyMutex *mutex) {
_PyCriticalSection_AssertHeld(PyMutex *mutex)
{
#ifdef Py_DEBUG
PyThreadState *tstate = _PyThreadState_GET();
_PyCriticalSection *cs = (_PyCriticalSection *)tstate->critical_section;
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_typeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ struct types_state {
extern PyStatus _PyTypes_InitTypes(PyInterpreterState *);
extern void _PyTypes_FiniTypes(PyInterpreterState *);
extern void _PyTypes_Fini(PyInterpreterState *);
void _PyTypes_AfterFork(void);
extern void _PyTypes_AfterFork(void);

/* other API */

Expand Down
44 changes: 21 additions & 23 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,9 +685,11 @@ type_cache_clear(struct type_cache *cache, PyObject *value)
{
for (Py_ssize_t i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
struct type_cache_entry *entry = &cache->hashtable[i];
_PySeqLock_LockWrite(&entry->sequence);
entry->version = 0;
Py_XSETREF(entry->name, _Py_XNewRef(value));
entry->value = NULL;
_PySeqLock_UnlockWrite(&entry->sequence);
}
}

Expand Down Expand Up @@ -4885,6 +4887,8 @@ update_cache(struct type_cache_entry *entry, PyObject *name, unsigned int versio
entry->value = value; /* borrowed */
assert(_PyASCIIObject_CAST(name)->hash != -1);
OBJECT_STAT_INC_COND(type_cache_collisions, entry->name != Py_None && entry->name != name);
// We're releasing this under the lock for simplicity sake because it's always a
// exact unicode object or Py_None so it's safe to do so.
Py_SETREF(entry->name, Py_NewRef(name));
}

Expand Down Expand Up @@ -4915,15 +4919,17 @@ update_cache_gil_disabled(struct type_cache_entry *entry, PyObject *name,

#endif

void _PyTypes_AfterFork() {
void
_PyTypes_AfterFork()
{
#ifdef Py_GIL_DISABLED
struct type_cache *cache = get_type_cache();
for (int i = 0; i < 1 << MCACHE_SIZE_EXP; i++) {
for (Py_ssize_t i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
struct type_cache_entry *entry = &cache->hashtable[i];
if (_PySeqLock_AfterFork(&entry->sequence)) {
// Entry was in the process of updating while forking, clear it...
entry->value = NULL;
entry->name = NULL;
Py_SETREF(entry->name, Py_None);
entry->version = 0;
}
}
Expand Down Expand Up @@ -4987,6 +4993,7 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
if (MCACHE_CACHEABLE_NAME(name)) {
has_version = assign_version_tag(interp, type);
version = type->tp_version_tag;
assert(!has_version || _PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG));
}
END_TYPE_LOCK()

Expand All @@ -5012,8 +5019,7 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
#else
update_cache(entry, name, version, res);
#endif
assert(_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG));
}
}
return res;
}

Expand Down Expand Up @@ -9486,13 +9492,13 @@ slot_bf_getbuffer(PyObject *self, Py_buffer *buffer, int flags)
return -1;
}

static int
releasebuffer_maybe_call_super_unlocked(PyObject *self, Py_buffer *buffer, releasebufferproc *base_releasebuffer)
static releasebufferproc
releasebuffer_maybe_call_super_unlocked(PyObject *self, Py_buffer *buffer)
{
PyTypeObject *self_type = Py_TYPE(self);
PyObject *mro = lookup_tp_mro(self_type);
if (mro == NULL) {
return -1;
return NULL;
}

assert(PyTuple_Check(mro));
Expand All @@ -9506,9 +9512,8 @@ releasebuffer_maybe_call_super_unlocked(PyObject *self, Py_buffer *buffer, relea
}
i++; /* skip self_type */
if (i >= n)
return -1;
return NULL;

*base_releasebuffer = NULL;
for (; i < n; i++) {
PyObject *obj = PyTuple_GET_ITEM(mro, i);
if (!PyType_Check(obj)) {
Expand All @@ -9518,28 +9523,25 @@ releasebuffer_maybe_call_super_unlocked(PyObject *self, Py_buffer *buffer, relea
if (base_type->tp_as_buffer != NULL
&& base_type->tp_as_buffer->bf_releasebuffer != NULL
&& base_type->tp_as_buffer->bf_releasebuffer != slot_bf_releasebuffer) {
*base_releasebuffer = base_type->tp_as_buffer->bf_releasebuffer;
break;
return base_type->tp_as_buffer->bf_releasebuffer;
}
}

return 0;
return NULL;
}

static int
static void
releasebuffer_maybe_call_super(PyObject *self, Py_buffer *buffer)
{
int res;
releasebufferproc base_releasebuffer;

BEGIN_TYPE_LOCK();
res = releasebuffer_maybe_call_super_unlocked(self, buffer, &base_releasebuffer);
base_releasebuffer = releasebuffer_maybe_call_super_unlocked(self, buffer);
END_TYPE_LOCK();

if (res == 0) {
if (base_releasebuffer != NULL) {
base_releasebuffer(self, buffer);
}
return res;
}

static void
Expand Down Expand Up @@ -9616,11 +9618,7 @@ static void
slot_bf_releasebuffer(PyObject *self, Py_buffer *buffer)
{
releasebuffer_call_python(self, buffer);
if (releasebuffer_maybe_call_super(self, buffer) < 0) {
if (PyErr_Occurred()) {
PyErr_WriteUnraisable(self);
}
}
releasebuffer_maybe_call_super(self, buffer);
}

static PyObject *
Expand Down

0 comments on commit 53967a5

Please sign in to comment.