/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  |  |  | 
41  |  | #ifdef WINPR_HAVE_UNISTD_H  | 
42  |  | #ifndef _XOPEN_SOURCE  | 
43  |  | #define _XOPEN_SOURCE 500 // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)  | 
44  |  | #endif  | 
45  |  | #include <unistd.h>  | 
46  |  | #endif  | 
47  |  |  | 
48  |  | WINPR_PRAGMA_DIAG_POP  | 
49  |  |  | 
50  |  | VOID Sleep(DWORD dwMilliseconds)  | 
51  | 0  | { | 
52  | 0  |   usleep(dwMilliseconds * 1000);  | 
53  | 0  | }  | 
54  |  |  | 
55  |  | DWORD SleepEx(DWORD dwMilliseconds, BOOL bAlertable)  | 
56  | 0  | { | 
57  | 0  |   WINPR_THREAD* thread = winpr_GetCurrentThread();  | 
58  | 0  |   WINPR_POLL_SET pollset;  | 
59  | 0  |   int status = 0;  | 
60  | 0  |   DWORD ret = WAIT_FAILED;  | 
61  | 0  |   BOOL autoSignalled = 0;  | 
62  |  | 
  | 
63  | 0  |   if (thread)  | 
64  | 0  |   { | 
65  |  |     /* treat re-entrancy if a completion is calling us */  | 
66  | 0  |     if (thread->apc.treatingCompletions)  | 
67  | 0  |       bAlertable = FALSE;  | 
68  | 0  |   }  | 
69  | 0  |   else  | 
70  | 0  |   { | 
71  |  |     /* called from a non WinPR thread */  | 
72  | 0  |     bAlertable = FALSE;  | 
73  | 0  |   }  | 
74  |  | 
  | 
75  | 0  |   if (!bAlertable || !thread->apc.length)  | 
76  | 0  |   { | 
77  | 0  |     usleep(dwMilliseconds * 1000);  | 
78  | 0  |     return 0;  | 
79  | 0  |   }  | 
80  |  |  | 
81  | 0  |   if (!pollset_init(&pollset, thread->apc.length))  | 
82  | 0  |   { | 
83  | 0  |     WLog_ERR(TAG, "unable to initialize pollset");  | 
84  | 0  |     return WAIT_FAILED;  | 
85  | 0  |   }  | 
86  |  |  | 
87  | 0  |   if (!apc_collectFds(thread, &pollset, &autoSignalled))  | 
88  | 0  |   { | 
89  | 0  |     WLog_ERR(TAG, "unable to APC file descriptors");  | 
90  | 0  |     goto out;  | 
91  | 0  |   }  | 
92  |  |  | 
93  | 0  |   if (!autoSignalled)  | 
94  | 0  |   { | 
95  |  |     /* we poll and wait only if no APC member is ready */  | 
96  | 0  |     status = pollset_poll(&pollset, dwMilliseconds);  | 
97  | 0  |     if (status < 0)  | 
98  | 0  |     { | 
99  | 0  |       WLog_ERR(TAG, "polling of apc fds failed");  | 
100  | 0  |       goto out;  | 
101  | 0  |     }  | 
102  | 0  |   }  | 
103  |  |  | 
104  | 0  |   if (apc_executeCompletions(thread, &pollset, 0))  | 
105  | 0  |   { | 
106  | 0  |     ret = WAIT_IO_COMPLETION;  | 
107  | 0  |   }  | 
108  | 0  |   else  | 
109  | 0  |   { | 
110  |  |     /* according to the spec return value is 0 see  | 
111  |  |      * https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleepex*/  | 
112  | 0  |     ret = 0;  | 
113  | 0  |   }  | 
114  | 0  | out:  | 
115  | 0  |   pollset_uninit(&pollset);  | 
116  | 0  |   return ret;  | 
117  | 0  | }  | 
118  |  |  | 
119  |  | #endif  | 
120  |  |  | 
121  |  | VOID USleep(DWORD dwMicroseconds)  | 
122  | 0  | { | 
123  | 0  | #ifndef _WIN32  | 
124  | 0  |   usleep(dwMicroseconds);  | 
125  |  | #else  | 
126  |  |   static LARGE_INTEGER freq = { 0 }; | 
127  |  |   LARGE_INTEGER t1 = { 0 }; | 
128  |  |   LARGE_INTEGER t2 = { 0 }; | 
129  |  |  | 
130  |  |   QueryPerformanceCounter(&t1);  | 
131  |  |  | 
132  |  |   if (freq.QuadPart == 0)  | 
133  |  |   { | 
134  |  |     QueryPerformanceFrequency(&freq);  | 
135  |  |   }  | 
136  |  |  | 
137  |  |   // in order to save cpu cyles we use Sleep() for the large share ...  | 
138  |  |   if (dwMicroseconds >= 1000)  | 
139  |  |   { | 
140  |  |     Sleep(dwMicroseconds / 1000);  | 
141  |  |   }  | 
142  |  |   // ... and busy loop until all the requested micro seconds have passed  | 
143  |  |   do  | 
144  |  |   { | 
145  |  |     QueryPerformanceCounter(&t2);  | 
146  |  |   } while (((t2.QuadPart - t1.QuadPart) * 1000000) / freq.QuadPart < dwMicroseconds);  | 
147  |  | #endif  | 
148  | 0  | }  |