Skip to content
Open
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
26 changes: 9 additions & 17 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ typedef struct {
isn't ready for writing. */
Py_off_t write_end;

PyThread_type_lock lock;
PyMutex mutex;
volatile unsigned long owner;

Py_ssize_t buffer_size;
Expand Down Expand Up @@ -304,16 +304,18 @@ _enter_buffered_busy(buffered *self)
PyInterpreterState *interp = _PyInterpreterState_GET();
relax_locking = _Py_IsInterpreterFinalizing(interp);
Py_BEGIN_ALLOW_THREADS
if (!relax_locking)
st = PyThread_acquire_lock(self->lock, 1);
if (!relax_locking) {
PyMutex_Lock(&self->mutex);
st = PY_LOCK_ACQUIRED;
}
else {
/* When finalizing, we don't want a deadlock to happen with daemon
* threads abruptly shut down while they owned the lock.
* Therefore, only wait for a grace period (1 s.).
* Note that non-daemon threads have already exited here, so this
* shouldn't affect carefully written threaded I/O code.
*/
st = PyThread_acquire_lock_timed(self->lock, (PY_TIMEOUT_T)1e6, 0);
st = _PyMutex_LockTimed(&self->mutex, (PyTime_t)1e9, 0);
}
Py_END_ALLOW_THREADS
if (relax_locking && st != PY_LOCK_ACQUIRED) {
Expand All @@ -327,14 +329,14 @@ _enter_buffered_busy(buffered *self)
}

#define ENTER_BUFFERED(self) \
( (PyThread_acquire_lock(self->lock, 0) ? \
( (_PyMutex_LockTimed(&self->mutex, (PyTime_t)0, 0) ? \
1 : _enter_buffered_busy(self)) \
&& (self->owner = PyThread_get_thread_ident(), 1) )

#define LEAVE_BUFFERED(self) \
do { \
self->owner = 0; \
PyThread_release_lock(self->lock); \
PyMutex_Unlock(&self->mutex); \
} while(0);

#define CHECK_INITIALIZED(self) \
Expand Down Expand Up @@ -436,10 +438,6 @@ buffered_dealloc(PyObject *op)
PyMem_Free(self->buffer);
self->buffer = NULL;
}
if (self->lock) {
PyThread_free_lock(self->lock);
self->lock = NULL;
}
(void)buffered_clear(op);
tp->tp_free(self);
Py_DECREF(tp);
Expand Down Expand Up @@ -850,13 +848,7 @@ _buffered_init(buffered *self)
PyErr_NoMemory();
return -1;
}
if (self->lock)
PyThread_free_lock(self->lock);
self->lock = PyThread_allocate_lock();
if (self->lock == NULL) {
PyErr_SetString(PyExc_RuntimeError, "can't allocate read lock");
return -1;
}
self->mutex = (PyMutex){0};
self->owner = 0;
/* Find out whether buffer_size is a power of 2 */
/* XXX is this optimization useful? */
Expand Down
Loading