Skip to content

Commit 8aa79c9

Browse files
stinosdpgeorge
authored andcommitted
windows: Consolidate all sleep-related functions into windows_mphal.c.
Replace the timer-based sleep with the standard win32 call since the former has no benefits: even though it allows specifying the time in 100uSec chunks, the actual resolution is still limited by the OS and is never better than 1mSec. For clarity move all of this next to the mp_hal_delay_ms definition so all related functions are in one place.
1 parent c90f097 commit 8aa79c9

File tree

5 files changed

+20
-83
lines changed

5 files changed

+20
-83
lines changed

ports/windows/Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ SRC_C = \
5555
windows_mphal.c \
5656
realpath.c \
5757
init.c \
58-
sleep.c \
5958
fmode.c \
6059
$(SRC_MOD) \
6160
$(wildcard $(VARIANT_DIR)/*.c)

ports/windows/init.c

-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#ifdef _MSC_VER
3131
#include <crtdbg.h>
3232
#endif
33-
#include "sleep.h"
3433
#include "fmode.h"
3534

3635
extern BOOL WINAPI console_sighandler(DWORD evt);
@@ -54,7 +53,6 @@ void init() {
5453
_set_invalid_parameter_handler(invalid_param_handler);
5554
#endif
5655
SetConsoleCtrlHandler(console_sighandler, TRUE);
57-
init_sleep();
5856
#ifdef __MINGW32__
5957
putenv("PRINTF_EXPONENT_DIGITS=2");
6058
#elif _MSC_VER < 1900
@@ -67,5 +65,4 @@ void init() {
6765

6866
void deinit() {
6967
SetConsoleCtrlHandler(console_sighandler, FALSE);
70-
deinit_sleep();
7168
}

ports/windows/sleep.c

-76
This file was deleted.

ports/windows/sleep.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
#ifndef MICROPY_INCLUDED_WINDOWS_SLEEP_H
2727
#define MICROPY_INCLUDED_WINDOWS_SLEEP_H
2828

29-
void init_sleep(void);
30-
void deinit_sleep(void);
29+
// The main sleep implementation for the Windows port.
3130
void msec_sleep(double msec);
31+
32+
// Define usleep() because some of the unix port's code uses that.
33+
// Mingw and the likes provide a definition of usleep(), note however
34+
// that it's also just Sleep(usec/1000).
3235
#ifdef _MSC_VER
3336
int usleep(__int64 usec);
3437
#endif

ports/windows/windows_mphal.c

+15-1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,20 @@ uint64_t mp_hal_time_ns(void) {
262262
return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL;
263263
}
264264

265+
void msec_sleep(double msec) {
266+
if (msec < 0.0) {
267+
msec = 0.0;
268+
}
269+
SleepEx((DWORD)msec, TRUE);
270+
}
271+
272+
#ifdef _MSC_VER
273+
int usleep(__int64 usec) {
274+
msec_sleep((double)usec / 1000.0);
275+
return 0;
276+
}
277+
#endif
278+
265279
void mp_hal_delay_ms(mp_uint_t ms) {
266280
#ifdef MICROPY_EVENT_POLL_HOOK
267281
mp_uint_t start = mp_hal_ticks_ms();
@@ -270,6 +284,6 @@ void mp_hal_delay_ms(mp_uint_t ms) {
270284
MICROPY_EVENT_POLL_HOOK
271285
}
272286
#else
273-
SleepEx(ms, TRUE);
287+
msec_sleep((double)ms);
274288
#endif
275289
}

0 commit comments

Comments
 (0)