summaryrefslogtreecommitdiffstatshomepage
path: root/unix
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2015-11-29 13:37:17 +0200
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2015-11-29 14:21:06 +0200
commit0bb57bf5bf1c9822abca524aabd00fe9b861d10d (patch)
tree16d083fd82cc459ed81584ec5fefc92116d13512 /unix
parent9d0d6d38306e37e66938322024ea9784e7530801 (diff)
downloadmicropython-0bb57bf5bf1c9822abca524aabd00fe9b861d10d.tar.gz
micropython-0bb57bf5bf1c9822abca524aabd00fe9b861d10d.zip
unix/modtime: sleep(): Automatically restart after receiving EINTR.
THis is required to deal well with signals, signals being the closest analogue of hardware interrupts for POSIX. This is also CPython 3.5 compliant behavior (PEP 475). The main problem implementing this is to figure out how much time was spent in waiting so far/how much is remaining. It's well-known fact that Linux updates select()'s timeout value when returning with EINTR to the remaining wait time. Here's what POSIX-based standards say about this: (http://pubs.opengroup.org/onlinepubs/9699919799/functions/pselect.html): "Upon successful completion, the select() function may modify the object pointed to by the timeout argument." I.e. it allows to modify timeout value, but doesn't say how exactly it is modified. And actually, it allows such modification only "upon successful completion", which returning with EINTR error hardly is. POSIX also allows to request automatic EINTR restart for system calls using sigaction call with SA_RESTART flag, but here's what the same document says about it: "If SA_RESTART has been set for the interrupting signal, it is implementation-defined whether the function restarts or returns with [EINTR]." In other words, POSIX doesn't leave room for both portable and efficient handling of this matter, so the code just allows to manually select Linux-compatible behavior with MICROPY_SELECT_REMAINING_TIME option, or otherwise will just raise OSError. When systems with non-Linux behavior are found, they can be handled separately.
Diffstat (limited to 'unix')
-rw-r--r--unix/modtime.c19
-rw-r--r--unix/mpconfigport.h4
2 files changed, 22 insertions, 1 deletions
diff --git a/unix/modtime.c b/unix/modtime.c
index 4a15f1a392..5d59294f2e 100644
--- a/unix/modtime.c
+++ b/unix/modtime.c
@@ -25,6 +25,7 @@
*/
#include <unistd.h>
+#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
@@ -32,6 +33,7 @@
#include "py/runtime.h"
#include "py/smallint.h"
+#include "py/mphal.h"
#ifdef _WIN32
void msec_sleep_tv(struct timeval *tv) {
@@ -113,8 +115,23 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) {
double ipart;
tv.tv_usec = round(modf(val, &ipart) * 1000000);
tv.tv_sec = ipart;
- sleep_select(0, NULL, NULL, NULL, &tv);
+ int res;
+ while (1) {
+ res = sleep_select(0, NULL, NULL, NULL, &tv);
+ #if MICROPY_SELECT_REMAINING_TIME
+ // TODO: This assumes Linux behavior of modifying tv to the remaining
+ // time.
+ if (res != -1 || errno != EINTR) {
+ break;
+ }
+ //printf("select: EINTR: %ld:%ld\n", tv.tv_sec, tv.tv_usec);
+ #else
+ break;
+ #endif
+ }
+ RAISE_ERRNO(res, errno);
#else
+ // TODO: Handle EINTR
sleep(mp_obj_get_int(arg));
#endif
return mp_const_none;
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index 8ff4e096d0..740bb9c5d0 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -205,6 +205,10 @@ void mp_unix_mark_exec(void);
#define MICROPY_PLAT_DEV_MEM (1)
#endif
+// Assume that select() call, interrupted with a signal, and erroring
+// with EINTR, updates remaining timeout value.
+#define MICROPY_SELECT_REMAINING_TIME (1)
+
#ifdef __ANDROID__
#include <android/api-level.h>
#if __ANDROID_API__ < 4