Message303441
Right now, the main loop in semlock_acquire looks like this:
do {
Py_BEGIN_ALLOW_THREADS
if (blocking && timeout_obj == Py_None)
res = sem_wait(self->handle);
else if (!blocking)
res = sem_trywait(self->handle);
else
res = sem_timedwait(self->handle, &deadline);
Py_END_ALLOW_THREADS
err = errno;
if (res == MP_EXCEPTION_HAS_BEEN_SET)
break;
} while (res < 0 && errno == EINTR && !PyErr_CheckSignals());
Here, we unconditionally release the GIL even we could acquire the mutex without blocking! As a result, we could end up switching to another thread in the process and greatly increasing the latency of operations that lock and release multiple shared data structures.
Instead, we should unconditionally try sem_trywait, and only then, if we want to block, release the GIL and try sem_wait or sem_timedwait. This way, we'll churn only when we need to.
Note that threading.Lock works the way I propose. |
|
Date |
User |
Action |
Args |
2017-10-01 01:43:02 | Daniel Colascione | set | recipients:
+ Daniel Colascione |
2017-10-01 01:43:02 | Daniel Colascione | set | messageid: <1506822182.01.0.213398074469.issue31653@psf.upfronthosting.co.za> |
2017-10-01 01:43:01 | Daniel Colascione | link | issue31653 messages |
2017-10-01 01:43:00 | Daniel Colascione | create | |
|