summaryrefslogtreecommitdiffstatshomepage
path: root/windows/sleep.c
diff options
context:
space:
mode:
authorstijn <stinos@zoho.com>2015-10-25 12:40:24 +0100
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2015-10-25 15:42:19 +0300
commitca9eb81d0b8624bfee6dac7b062747b01e8aeb4b (patch)
treeaaacbb8e8f7ee19f6c3901344965f27e04e58a13 /windows/sleep.c
parent1c55310bcced133fa4efc1546c0001f3ae4a9fc6 (diff)
downloadmicropython-ca9eb81d0b8624bfee6dac7b062747b01e8aeb4b.tar.gz
micropython-ca9eb81d0b8624bfee6dac7b062747b01e8aeb4b.zip
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.
Diffstat (limited to 'windows/sleep.c')
-rw-r--r--windows/sleep.c48
1 files changed, 45 insertions, 3 deletions
diff --git a/windows/sleep.c b/windows/sleep.c
index a7e5125e5b..214d6d622a 100644
--- a/windows/sleep.c
+++ b/windows/sleep.c
@@ -25,10 +25,52 @@
*/
#include <windows.h>
+#include <errno.h>
+#include <limits.h>
-extern HANDLE hSleepEvent;
+HANDLE waitTimer = NULL;
+
+void init_sleep(void) {
+ waitTimer = CreateWaitableTimer(NULL, TRUE, NULL);
+}
+
+void deinit_sleep(void) {
+ if (waitTimer != NULL) {
+ CloseHandle(waitTimer);
+ waitTimer = NULL;
+ }
+}
+
+int usleep_impl(__int64 usec) {
+ if (waitTimer == NULL) {
+ errno = EAGAIN;
+ return -1;
+ }
+ if (usec < 0 || usec > LLONG_MAX / 10) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ LARGE_INTEGER ft;
+ ft.QuadPart = -10 * usec; // 100 nanosecond interval, negative value = relative time
+ if (SetWaitableTimer(waitTimer, &ft, 0, NULL, NULL, 0) == 0) {
+ errno = EINVAL;
+ return -1;
+ }
+ if (WaitForSingleObject(waitTimer, INFINITE) != WAIT_OBJECT_0) {
+ errno = EAGAIN;
+ return -1;
+ }
+ return 0;
+}
+
+#ifdef _MSC_VER // mingw and the likes provide their own usleep()
+int usleep(__int64 usec) {
+ return usleep_impl(usec);
+}
+#endif
void msec_sleep(double msec) {
- ResetEvent(hSleepEvent);
- WaitForSingleObjectEx(hSleepEvent, msec, FALSE);
+ const double usec = msec * 1000.0;
+ usleep_impl(usec > (double)LLONG_MAX ? LLONG_MAX : (__int64)usec);
}