This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pycore_condvar.h: remove Windows conditonal variable emulation, use Windows native conditional variable
Type: Stage:
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, paul.moore, steve.dower, tim.golden, vstinner, zach.ware
Priority: normal Keywords:

Created on 2021-09-27 14:26 by vstinner, last changed 2022-04-11 14:59 by admin.

Messages (6)
msg402720 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-09-27 14:26
I recently worked on time.sleep() enhancement (bpo-21302) and threading bugfixes (bpo-45274, bpo-1596321). I saw one more time that Python emulates conditional variables to support Windows XP and older. But Python 3.11 requires Windows 8.1 or newer. IMO it's time to remove _PY_EMULATED_WIN_CV code path from pycore_condvar.h.
msg402721 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-09-27 14:26
See also bpo-29971 "Lock.acquire() not interruptible on Windows".
msg402725 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-09-27 14:48
>  IMO it's time to remove _PY_EMULATED_WIN_CV code path from 
> pycore_condvar.h.

SleepConditionVariableSRW() can't be interrupted in PyCOND_WAIT() and PyCOND_TIMEDWAIT(). Maybe a hybrid solution could be adopted. Use native condition variables for the GIL, where performance matters and the ability to interrupt the wait doesn't matter. Otherwise use the semaphore implementation, for which the wait implementation can be modified to use WaitForMultipleObjects(), and include the Ctrl+C event.
msg402732 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2021-09-27 16:45
> SleepConditionVariableSRW() can't be interrupted in PyCOND_WAIT() and PyCOND_TIMEDWAIT()

This was my immediate reaction as well.

Unfortunately, we keep seeing that all waits need to be interruptible, so either a WaitForMultipleObjects or a slow spinlock.
msg402741 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-09-27 21:24
Eryk:
> SleepConditionVariableSRW() can't be interrupted in PyCOND_WAIT() and PyCOND_TIMEDWAIT().

Oh. A comment on StackOverlow says:     

"The WaitForXxx functions accept parameters of the generic HANDLE type, which represents a handle to a kernel object. Condition variables are user-mode objects, not kernel objects, so you cannot use them with these functions, since they work only with kernel objects."

https://stackoverflow.com/questions/37522108/can-you-really-wait-on-condition-variable-with-waitfor-objects

I only created this issue because of this comment in pycore_condvar.h:
---
/* non-emulated condition variables are provided for those that want
 * to target Windows Vista.  Modify this macro to enable them.
 */
#ifndef _PY_EMULATED_WIN_CV
#define _PY_EMULATED_WIN_CV 1  /* use emulated condition variables */
#endif
---
msg402757 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-09-28 06:30
FYI, waiting for a condition variable can cause a thread to enter a wait state that's interruptible, in theory, but the mechanism is different since condition variables and SRW locks are pointer-sized values in user space, instead of NT objects in kernel space. The current implementation is based on the system call NtWaitForAlertByThreadId(address, timeout), which enters the "WrAlertByThreadId" wait state. The address parameter is that of the SRW lock. The kernel sets this as the 'object' for the wait, but the wait is actually satisfied by alerting the thread directly via NtAlertThreadByThreadId(tid). ISTM, they could have added a function that calls the latter to cancel a wait on a given thread. That would have been useful for Ctrl+C since the handler executes on a new thread.
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89464
2021-09-28 06:30:07eryksunsetmessages: + msg402757
2021-09-27 21:24:49vstinnersetmessages: + msg402741
title: pycore_condvar.h: remove Windows conditonal variable emulation -> pycore_condvar.h: remove Windows conditonal variable emulation, use Windows native conditional variable
2021-09-27 16:45:41steve.dowersetmessages: + msg402732
2021-09-27 14:48:59eryksunsetnosy: + eryksun
messages: + msg402725
2021-09-27 14:26:22vstinnersetmessages: + msg402721
2021-09-27 14:26:03vstinnercreate