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.
This commit is contained in:
parent
9d0d6d3830
commit
0bb57bf5bf
@ -25,6 +25,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
@ -32,6 +33,7 @@
|
|||||||
|
|
||||||
#include "py/runtime.h"
|
#include "py/runtime.h"
|
||||||
#include "py/smallint.h"
|
#include "py/smallint.h"
|
||||||
|
#include "py/mphal.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
void msec_sleep_tv(struct timeval *tv) {
|
void msec_sleep_tv(struct timeval *tv) {
|
||||||
@ -113,8 +115,23 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) {
|
|||||||
double ipart;
|
double ipart;
|
||||||
tv.tv_usec = round(modf(val, &ipart) * 1000000);
|
tv.tv_usec = round(modf(val, &ipart) * 1000000);
|
||||||
tv.tv_sec = ipart;
|
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
|
#else
|
||||||
|
// TODO: Handle EINTR
|
||||||
sleep(mp_obj_get_int(arg));
|
sleep(mp_obj_get_int(arg));
|
||||||
#endif
|
#endif
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
|
@ -205,6 +205,10 @@ void mp_unix_mark_exec(void);
|
|||||||
#define MICROPY_PLAT_DEV_MEM (1)
|
#define MICROPY_PLAT_DEV_MEM (1)
|
||||||
#endif
|
#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__
|
#ifdef __ANDROID__
|
||||||
#include <android/api-level.h>
|
#include <android/api-level.h>
|
||||||
#if __ANDROID_API__ < 4
|
#if __ANDROID_API__ < 4
|
||||||
|
Loading…
Reference in New Issue
Block a user