Skip to content

Commit 5ed284a

Browse files
committed
windows: Add modtime implementation
1 parent d25cba4 commit 5ed284a

10 files changed

Lines changed: 165 additions & 4 deletions

File tree

py/runtime.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ void mp_deinit(void) {
9191
//mp_obj_dict_free(&dict_main);
9292
mp_module_deinit();
9393
mp_emit_glue_deinit();
94+
95+
// call port specific deinitialization if any
96+
#ifdef MICROPY_PORT_INIT_FUNC
97+
MICROPY_PORT_DEINIT_FUNC;
98+
#endif
9499
}
95100

96101
mp_obj_t mp_load_const_dec(qstr qstr) {

unix/modtime.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@
3636
#include "obj.h"
3737
#include "runtime.h"
3838

39+
#ifdef _WIN32
40+
void msec_sleep_tv(struct timeval *tv) {
41+
msec_sleep(tv->tv_sec * 1000.0 + tv->tv_usec / 1000.0);
42+
}
43+
#define sleep_select(a,b,c,d,e) msec_sleep_tv((e))
44+
#else
45+
#define sleep_select select
46+
#endif
47+
48+
#if CLOCKS_PER_SEC == 1000000 // POSIX
49+
#define CLOCK_DIV 1000.0
50+
#elif CLOCKS_PER_SEC == 1000 // WIN32
51+
#define CLOCK_DIV 1.0
52+
#else
53+
#error Unsupported clock() implementation
54+
#endif
55+
3956
STATIC mp_obj_t mod_time_time() {
4057
#if MICROPY_ENABLE_FLOAT
4158
struct timeval tv;
@@ -51,11 +68,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_time_obj, mod_time_time);
5168
// Note: this is deprecated since CPy3.3, but pystone still uses it.
5269
STATIC mp_obj_t mod_time_clock() {
5370
#if MICROPY_ENABLE_FLOAT
54-
// POSIX requires CLOCKS_PER_SEC equals 1000000, so that's what we assume.
5571
// float cannot represent full range of int32 precisely, so we pre-divide
5672
// int to reduce resolution, and then actually do float division hoping
5773
// to preserve integer part resolution.
58-
return mp_obj_new_float((float)(clock() / 1000) / 1000.0);
74+
return mp_obj_new_float((float)(clock() / 1000) / CLOCK_DIV);
5975
#else
6076
return mp_obj_new_int((machine_int_t)clock());
6177
#endif
@@ -69,7 +85,7 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) {
6985
double ipart;
7086
tv.tv_usec = round(modf(val, &ipart) * 1000000);
7187
tv.tv_sec = ipart;
72-
select(0, NULL, NULL, NULL, &tv);
88+
sleep_select(0, NULL, NULL, NULL, &tv);
7389
#else
7490
sleep(mp_obj_get_int(arg));
7591
#endif

windows/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ SRC_C = \
3131
unix/main.c \
3232
unix/file.c \
3333
unix/input.c \
34+
unix/modtime.c \
3435
realpath.c \
3536
init.c \
37+
sleep.c \
3638

3739
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
3840

windows/init.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,21 @@
2626

2727
#include <stdlib.h>
2828
#include <stdio.h>
29+
#include <Windows.h>
30+
31+
HANDLE hSleepEvent = NULL;
2932

3033
void init() {
34+
hSleepEvent = CreateEvent(NULL, TRUE, FALSE, FALSE);
3135
#ifdef __MINGW32__
3236
putenv("PRINTF_EXPONENT_DIGITS=2");
3337
#else
3438
_set_output_format(_TWO_DIGIT_EXPONENT);
3539
#endif
3640
}
41+
42+
void deinit() {
43+
if (hSleepEvent != NULL) {
44+
CloseHandle(hSleepEvent);
45+
}
46+
}

windows/init.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
*/
2626

2727
void init(void);
28+
void deinit(void);

windows/mpconfigport.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
4545
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
4646
#define MICROPY_PORT_INIT_FUNC init()
47+
#define MICROPY_PORT_DEINIT_FUNC deinit()
4748

4849
// type definitions for the specific machine
4950

@@ -69,9 +70,15 @@ extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
6970
#define MICROPY_EXTRA_BUILTINS \
7071
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },
7172

73+
extern const struct _mp_obj_module_t mp_module_time;
74+
#define MICROPY_EXTRA_BUILTIN_MODULES \
75+
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mp_module_time }, \
76+
7277
#include "realpath.h"
7378
#include "init.h"
7479

80+
// sleep for given number of milliseconds
81+
void msec_sleep(double msec);
7582

7683
// MSVC specifics
7784
#ifdef _MSC_VER

windows/msvc/gettimeofday.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 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+
#include <Winsock2.h>
28+
29+
typedef union {
30+
unsigned __int64 tm; // time in 100 nanoseconds interval
31+
FILETIME ft;
32+
} FT;
33+
34+
int gettimeofday(struct timeval *tp, struct timezone *tz) {
35+
if (tp == NULL) {
36+
return 0;
37+
}
38+
39+
// UTC time
40+
FT ft;
41+
ZeroMemory(&ft, sizeof(ft));
42+
GetSystemTimeAsFileTime(&ft.ft);
43+
44+
// to microseconds
45+
ft.tm /= 10;
46+
47+
// convert to unix format
48+
// number of microseconds intervals between the 1st january 1601 and the 1st january 1970 (369 years + 89 leap days)
49+
const unsigned __int64 deltaEpoch = 11644473600000000ull;
50+
const unsigned __int64 microSecondsToSeconds = 1000000ull;
51+
tp->tv_usec = ft.tm % microSecondsToSeconds;
52+
tp->tv_sec = (ft.tm - deltaEpoch) / microSecondsToSeconds;
53+
54+
// see man gettimeofday: timezone is deprecated and expected to be NULL
55+
(void)tz;
56+
57+
return 0;
58+
}

windows/msvc/sources.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66
<ItemGroup>
77
<ClCompile Include="$(PyBaseDir)py\*.c" />
8-
<ClCompile Include="$(PyBaseDir)unix\*.c" Exclude="$(PyBaseDir)unix\mod*.c" />
8+
<ClCompile Include="$(PyBaseDir)unix\*.c" Exclude="$(PyBaseDir)unix\modffi.c;$(PyBaseDir)unix\modsocket.c" />
99
<ClCompile Include="$(PyBaseDir)windows\*.c" />
1010
<ClCompile Include="$(PyBaseDir)windows\msvc\*.c" />
1111
</ItemGroup>

windows/msvc/sys/time.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 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+
// Get the definitions for timeval etc
28+
#include <Winsock2.h>

windows/sleep.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 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+
#include <Windows.h>
28+
29+
extern HANDLE hSleepEvent;
30+
31+
void msec_sleep(double msec) {
32+
ResetEvent(hSleepEvent);
33+
WaitForSingleObjectEx(hSleepEvent, msec, FALSE);
34+
}

0 commit comments

Comments
 (0)