Add Yield() function to python Threading

I’ve seen the term “yield” used variously in the context of a thread yielding the processor to another thread. For example, Python supports the POSIX function sched_yield() as os.sched_yield(). Its function is to “force the running thread to relinquish the processor until it again becomes the head of its thread list”. It’s intended for use with deterministic, realtime scheduling policies such as SCHED_FIFO and SCHED_RR (round robin).

On Windows, SwitchToThread() yields the processor for up to one time slice to another thread that’s ready to run. The Windows API implements it by calling NTAPI NtYieldExecution(), which is implemented in the kernel by KeYieldExecution(). Also, WinAPI Sleep[Ex]() is implemented by calling NTAPI NtDelayExecution(), which is implemented in the kernel by KeDelayExecutionThread(). Given that the delay is 0; it’s not an alertable wait; and the thread has no pending asynchronous procedure calls (APCs), then KeDelayExecutionThread() calls KeYieldExecution(). Thus Python’s time.sleep(0) is implemented by NTAPI NtYieldExecution().