Coverage Report

Created: 2023-09-25 06:56

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