Skip to content

Commit ca9eb81

Browse files
stinospfalcon
authored andcommitted
windows: Add usleep() implementation for msvc port
Also make sleep.c self-contained by moving initialization code, instead of having part of the code in init.c, and add a header file to accomodate this. msec_sleep() now uses the usleep() implementation as well.
1 parent 1c55310 commit ca9eb81

4 files changed

Lines changed: 81 additions & 13 deletions

File tree

windows/init.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@
2626

2727
#include <stdlib.h>
2828
#include <stdio.h>
29-
#include <windows.h>
30-
31-
HANDLE hSleepEvent = NULL;
29+
#include "sleep.h"
3230

3331
void init() {
34-
hSleepEvent = CreateEvent(NULL, TRUE, FALSE, FALSE);
32+
init_sleep();
3533
#ifdef __MINGW32__
3634
putenv("PRINTF_EXPONENT_DIGITS=2");
3735
#else
@@ -40,7 +38,5 @@ void init() {
4038
}
4139

4240
void deinit() {
43-
if (hSleepEvent != NULL) {
44-
CloseHandle(hSleepEvent);
45-
}
41+
deinit_sleep();
4642
}

windows/mpconfigport.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ extern const struct _mp_obj_module_t mp_module_time;
162162

163163
#include "realpath.h"
164164
#include "init.h"
165-
166-
// sleep for given number of milliseconds
167-
void msec_sleep(double msec);
165+
#include "sleep.h"
168166

169167
// MSVC specifics
170168
#ifdef _MSC_VER

windows/sleep.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,52 @@
2525
*/
2626

2727
#include <windows.h>
28+
#include <errno.h>
29+
#include <limits.h>
2830

29-
extern HANDLE hSleepEvent;
31+
HANDLE waitTimer = NULL;
32+
33+
void init_sleep(void) {
34+
waitTimer = CreateWaitableTimer(NULL, TRUE, NULL);
35+
}
36+
37+
void deinit_sleep(void) {
38+
if (waitTimer != NULL) {
39+
CloseHandle(waitTimer);
40+
waitTimer = NULL;
41+
}
42+
}
43+
44+
int usleep_impl(__int64 usec) {
45+
if (waitTimer == NULL) {
46+
errno = EAGAIN;
47+
return -1;
48+
}
49+
if (usec < 0 || usec > LLONG_MAX / 10) {
50+
errno = EINVAL;
51+
return -1;
52+
}
53+
54+
LARGE_INTEGER ft;
55+
ft.QuadPart = -10 * usec; // 100 nanosecond interval, negative value = relative time
56+
if (SetWaitableTimer(waitTimer, &ft, 0, NULL, NULL, 0) == 0) {
57+
errno = EINVAL;
58+
return -1;
59+
}
60+
if (WaitForSingleObject(waitTimer, INFINITE) != WAIT_OBJECT_0) {
61+
errno = EAGAIN;
62+
return -1;
63+
}
64+
return 0;
65+
}
66+
67+
#ifdef _MSC_VER // mingw and the likes provide their own usleep()
68+
int usleep(__int64 usec) {
69+
return usleep_impl(usec);
70+
}
71+
#endif
3072

3173
void msec_sleep(double msec) {
32-
ResetEvent(hSleepEvent);
33-
WaitForSingleObjectEx(hSleepEvent, msec, FALSE);
74+
const double usec = msec * 1000.0;
75+
usleep_impl(usec > (double)LLONG_MAX ? LLONG_MAX : (__int64)usec);
3476
}

windows/sleep.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
void init_sleep(void);
28+
void deinit_sleep(void);
29+
void msec_sleep(double msec);
30+
#ifdef _MSC_VER
31+
int usleep(__int64 usec);
32+
#endif

0 commit comments

Comments
 (0)