20 #include <winpr/config.h>
22 #include <winpr/platform.h>
23 #include <winpr/windows.h>
25 #include <winpr/synch.h>
28 #include "../thread/apc.h"
29 #include "../thread/thread.h"
30 #include "../synch/pollset.h"
32 #define TAG WINPR_TAG("synch.sleep")
38 WINPR_PRAGMA_DIAG_PUSH
39 WINPR_PRAGMA_DIAG_IGNORED_RESERVED_ID_MACRO
40 WINPR_PRAGMA_DIAG_IGNORED_UNUSED_MACRO
42 #ifdef WINPR_HAVE_UNISTD_H
44 #define _XOPEN_SOURCE 500
51 VOID Sleep(DWORD dwMilliseconds)
53 usleep(dwMilliseconds * 1000);
56 DWORD SleepEx(DWORD dwMilliseconds, BOOL bAlertable)
58 WINPR_THREAD* thread = winpr_GetCurrentThread();
59 WINPR_POLL_SET pollset;
61 DWORD ret = WAIT_FAILED;
62 BOOL autoSignalled = 0;
67 if (thread->apc.treatingCompletions)
76 if (!bAlertable || !thread->apc.length)
78 usleep(dwMilliseconds * 1000);
82 if (!pollset_init(&pollset, thread->apc.length))
84 WLog_ERR(TAG,
"unable to initialize pollset");
88 if (!apc_collectFds(thread, &pollset, &autoSignalled))
90 WLog_ERR(TAG,
"unable to APC file descriptors");
97 status = pollset_poll(&pollset, dwMilliseconds);
100 WLog_ERR(TAG,
"polling of apc fds failed");
105 if (apc_executeCompletions(thread, &pollset, 0))
107 ret = WAIT_IO_COMPLETION;
116 pollset_uninit(&pollset);
122 VOID USleep(DWORD dwMicroseconds)
125 usleep(dwMicroseconds);
127 static LARGE_INTEGER freq = { 0 };
128 LARGE_INTEGER t1 = { 0 };
129 LARGE_INTEGER t2 = { 0 };
131 QueryPerformanceCounter(&t1);
133 if (freq.QuadPart == 0)
135 QueryPerformanceFrequency(&freq);
139 if (dwMicroseconds >= 1000)
141 Sleep(dwMicroseconds / 1000);
146 QueryPerformanceCounter(&t2);
147 }
while (((t2.QuadPart - t1.QuadPart) * 1000000) / freq.QuadPart < dwMicroseconds);