/src/FreeRDP/winpr/libwinpr/synch/sleep.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Synchronization Functions |
4 | | * |
5 | | * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <winpr/config.h> |
21 | | |
22 | | #include <winpr/platform.h> |
23 | | #include <winpr/windows.h> |
24 | | |
25 | | #include <winpr/synch.h> |
26 | | |
27 | | #include "../log.h" |
28 | | #include "../thread/apc.h" |
29 | | #include "../thread/thread.h" |
30 | | #include "../synch/pollset.h" |
31 | | |
32 | | #define TAG WINPR_TAG("synch.sleep") |
33 | | |
34 | | #ifndef _WIN32 |
35 | | |
36 | | #include <time.h> |
37 | | |
38 | | WINPR_PRAGMA_DIAG_PUSH |
39 | | WINPR_PRAGMA_DIAG_IGNORED_RESERVED_ID_MACRO |
40 | | WINPR_PRAGMA_DIAG_IGNORED_UNUSED_MACRO |
41 | | |
42 | | #ifdef WINPR_HAVE_UNISTD_H |
43 | | #ifndef _XOPEN_SOURCE |
44 | | #define _XOPEN_SOURCE 500 // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
45 | | #endif |
46 | | #include <unistd.h> |
47 | | #endif |
48 | | |
49 | | WINPR_PRAGMA_DIAG_POP |
50 | | |
51 | | VOID Sleep(DWORD dwMilliseconds) |
52 | 0 | { |
53 | 0 | usleep(dwMilliseconds * 1000); |
54 | 0 | } |
55 | | |
56 | | DWORD SleepEx(DWORD dwMilliseconds, BOOL bAlertable) |
57 | 0 | { |
58 | 0 | WINPR_THREAD* thread = winpr_GetCurrentThread(); |
59 | 0 | WINPR_POLL_SET pollset; |
60 | 0 | int status = 0; |
61 | 0 | DWORD ret = WAIT_FAILED; |
62 | 0 | BOOL autoSignalled = 0; |
63 | |
|
64 | 0 | if (thread) |
65 | 0 | { |
66 | | /* treat re-entrancy if a completion is calling us */ |
67 | 0 | if (thread->apc.treatingCompletions) |
68 | 0 | bAlertable = FALSE; |
69 | 0 | } |
70 | 0 | else |
71 | 0 | { |
72 | | /* called from a non WinPR thread */ |
73 | 0 | bAlertable = FALSE; |
74 | 0 | } |
75 | |
|
76 | 0 | if (!bAlertable || !thread->apc.length) |
77 | 0 | { |
78 | 0 | usleep(dwMilliseconds * 1000); |
79 | 0 | return 0; |
80 | 0 | } |
81 | | |
82 | 0 | if (!pollset_init(&pollset, thread->apc.length)) |
83 | 0 | { |
84 | 0 | WLog_ERR(TAG, "unable to initialize pollset"); |
85 | 0 | return WAIT_FAILED; |
86 | 0 | } |
87 | | |
88 | 0 | if (!apc_collectFds(thread, &pollset, &autoSignalled)) |
89 | 0 | { |
90 | 0 | WLog_ERR(TAG, "unable to APC file descriptors"); |
91 | 0 | goto out; |
92 | 0 | } |
93 | | |
94 | 0 | if (!autoSignalled) |
95 | 0 | { |
96 | | /* we poll and wait only if no APC member is ready */ |
97 | 0 | status = pollset_poll(&pollset, dwMilliseconds); |
98 | 0 | if (status < 0) |
99 | 0 | { |
100 | 0 | WLog_ERR(TAG, "polling of apc fds failed"); |
101 | 0 | goto out; |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | 0 | if (apc_executeCompletions(thread, &pollset, 0)) |
106 | 0 | { |
107 | 0 | ret = WAIT_IO_COMPLETION; |
108 | 0 | } |
109 | 0 | else |
110 | 0 | { |
111 | | /* according to the spec return value is 0 see |
112 | | * https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleepex*/ |
113 | 0 | ret = 0; |
114 | 0 | } |
115 | 0 | out: |
116 | 0 | pollset_uninit(&pollset); |
117 | 0 | return ret; |
118 | 0 | } |
119 | | |
120 | | #endif |
121 | | |
122 | | VOID USleep(DWORD dwMicroseconds) |
123 | 0 | { |
124 | 0 | #ifndef _WIN32 |
125 | 0 | usleep(dwMicroseconds); |
126 | | #else |
127 | | static LARGE_INTEGER freq = { 0 }; |
128 | | LARGE_INTEGER t1 = { 0 }; |
129 | | LARGE_INTEGER t2 = { 0 }; |
130 | | |
131 | | QueryPerformanceCounter(&t1); |
132 | | |
133 | | if (freq.QuadPart == 0) |
134 | | { |
135 | | QueryPerformanceFrequency(&freq); |
136 | | } |
137 | | |
138 | | // in order to save cpu cycles we use Sleep() for the large share ... |
139 | | if (dwMicroseconds >= 1000) |
140 | | { |
141 | | Sleep(dwMicroseconds / 1000); |
142 | | } |
143 | | // ... and busy loop until all the requested micro seconds have passed |
144 | | do |
145 | | { |
146 | | QueryPerformanceCounter(&t2); |
147 | | } while (((t2.QuadPart - t1.QuadPart) * 1000000) / freq.QuadPart < dwMicroseconds); |
148 | | #endif |
149 | 0 | } |