From e55c9a6a17dfe04969955925df002b31a18832ae Mon Sep 17 00:00:00 2001 From: lyon Date: Thu, 2 Feb 2023 00:12:21 +0800 Subject: [PATCH] fix platform_thread_mutex, update time --- package/time/_time.c | 42 ++++++++++++++++++++---------------------- package/time/_time.pyi | 4 ---- package/time/time.py | 4 +++- src/PikaPlatform.h | 2 ++ 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/package/time/_time.c b/package/time/_time.c index 9aff69382..d4d0439aa 100644 --- a/package/time/_time.c +++ b/package/time/_time.c @@ -7,23 +7,23 @@ #include #endif -void _time_sleep_ms(PikaObj* self, int ms) { -#if defined(__linux) - usleep(ms * 1000); -#elif defined(_WIN32) - Sleep(ms); -#else - __platform_sleep_ms(ms); -#endif +void (*global_do_sleep_ms)(uint32_t); + +static void _do_sleep_ms_tick(uint32_t ms) { + uint32_t tick = pika_platform_get_tick(); + while (pika_platform_get_tick() - tick < ms) { + _pikaVM_yield(); + } } + +void _time_sleep_ms(PikaObj* self, int ms) { + global_do_sleep_ms(ms); +} + void _time_sleep_s(PikaObj* self, int s) { -#if defined(__linux) - sleep(s); -#elif defined(_WIN32) - Sleep(s * 1000); -#else - __platform_sleep_s(s); -#endif + for (int i = 0; i < s; i++) { + _time_sleep_ms(self, 1000); + } } void _time_platformGetTick(PikaObj* self) { @@ -701,12 +701,10 @@ void _time___init__(PikaObj* self) { obj_setInt(self, "locale", 8); time_localtime(0.0, &this_tm, 8); time_set_tm_value(self, &this_tm); + if (-1 == pika_platform_get_tick()) { + global_do_sleep_ms = pika_platform_sleep_ms; + } else { + global_do_sleep_ms = _do_sleep_ms_tick; + } #endif } - -void _time_sleep(PikaObj* self, pika_float s) { - Args* args = New_args(NULL); - args_setInt(args, "ms", s * 1000); - obj_runNativeMethod(self, "sleep_ms", args); - args_deinit(args); -} diff --git a/package/time/_time.pyi b/package/time/_time.pyi index 5a414f412..bfd9f8553 100644 --- a/package/time/_time.pyi +++ b/package/time/_time.pyi @@ -4,10 +4,6 @@ from PikaObj import * def __init__(self): ... -def sleep(self, s: float): - """Sleep for s seconds.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") def time(self) -> float: """Get the current time.""" diff --git a/package/time/time.py b/package/time/time.py index 699b75b45..8c6392a7e 100644 --- a/package/time/time.py +++ b/package/time/time.py @@ -2,7 +2,9 @@ import _time def sleep(s: float): - return _time.sleep(s) + for i in range(int(s)): + _time.sleep_s(1) + _time.sleep_ms(int((s - int(s)) * 1000)) def sleep_s(s: int): diff --git a/src/PikaPlatform.h b/src/PikaPlatform.h index ace8664b4..49226d938 100644 --- a/src/PikaPlatform.h +++ b/src/PikaPlatform.h @@ -241,11 +241,13 @@ typedef struct pika_platform_thread_mutex { typedef struct pika_platform_thread_mutex { SemaphoreHandle_t mutex; volatile int is_init; + volatile int is_first_lock; } pika_platform_thread_mutex_t; #else typedef struct pika_platform_thread_mutex { void* platform_data; volatile int is_init; + volatile int is_first_lock; } pika_platform_thread_mutex_t; #endif