Paul Gofman (@gofman) commented about dlls/ntdll/tests/sync.c:
timeout.QuadPart = tests[i].timeout;
apc_count = 0;
if (tests[i].queue_apc)
QueueUserAPC( apc_proc, GetCurrentThread(), (ULONG_PTR)&apc_count );
/* test NtDelayExecution */
SetLastError( 0xdeadbeef );
status = pNtDelayExecution( tests[i].alertable, &timeout );
ok( GetLastError() == 0xdeadbeef, "got error %#lx.\n", GetLastError() );
todo_wine_if(status == STATUS_TIMEOUT);
ok( status == STATUS_SUCCESS ||
(!tests[i].timeout && status == STATUS_NO_YIELD_PERFORMED) ||
(tests[i].alertable && tests[i].queue_apc && status == STATUS_USER_APC),
"got status %#lx.\n", status );
In fact, APC is always processed in alertable sleep if APC is available, it is not random. Not sure, maybe in early patches versions when another thread was involved there was some race. So there is no alternative here, if apc is queued and wait is alertable, APC should be processed and reflected in the status. Also, test currently has leftover queued APC. How about incorporating the attached diff (the diff is on top but would be applied to the first patch): it solves those (in particular, pops leftover APC with SleepEx(0, TRUE)) and makes this a bit more straightforward.
[test.patch](/uploads/a38e87b1795d7259321cf46b50808e1d/test.patch)