jshn: add functionality to read big JSON
[project/libubox.git] / uloop.c
diff --git a/uloop.c b/uloop.c
index 0b75d4b..8517366 100644 (file)
--- a/uloop.c
+++ b/uloop.c
@@ -58,11 +58,13 @@ static struct list_head processes = LIST_HEAD_INIT(processes);
 
 static int poll_fd = -1;
 bool uloop_cancelled = false;
+bool uloop_handle_sigchld = true;
 static int uloop_status = 0;
 static bool do_sigchld = false;
 
 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
 static int cur_fd, cur_nfds;
+static int uloop_run_depth = 0;
 
 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags);
 
@@ -115,6 +117,8 @@ static int waker_init(void)
        return 0;
 }
 
+static void uloop_setup_signals(bool add);
+
 int uloop_init(void)
 {
        if (uloop_init_pollfd() < 0)
@@ -125,6 +129,8 @@ int uloop_init(void)
                return -1;
        }
 
+       uloop_setup_signals(true);
+
        return 0;
 }
 
@@ -387,7 +393,13 @@ static void uloop_handle_processes(void)
 
 static void uloop_signal_wake(void)
 {
-       write(waker_pipe, "w", 1);
+       do {
+               if (write(waker_pipe, "w", 1) < 0) {
+                       if (errno == EINTR)
+                               continue;
+               }
+               break;
+       } while (1);
 }
 
 static void uloop_handle_sigint(int signo)
@@ -455,7 +467,9 @@ static void uloop_setup_signals(bool add)
 
        uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
        uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
-       uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
+
+       if (uloop_handle_sigchld)
+               uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
 
        uloop_ignore_signal(SIGPIPE, add);
 }
@@ -508,17 +522,17 @@ static void uloop_clear_processes(void)
                uloop_process_delete(p);
 }
 
-int uloop_run(void)
+bool uloop_cancelling(void)
+{
+       return uloop_run_depth > 0 && uloop_cancelled;
+}
+
+int uloop_run_timeout(int timeout)
 {
-       static int recursive_calls = 0;
+       int next_time = 0;
        struct timeval tv;
 
-       /*
-        * Handlers are only updated for the first call to uloop_run() (and restored
-        * when this call is done).
-        */
-       if (!recursive_calls++)
-               uloop_setup_signals(true);
+       uloop_run_depth++;
 
        uloop_status = 0;
        uloop_cancelled = false;
@@ -534,17 +548,22 @@ int uloop_run(void)
                        break;
 
                uloop_gettime(&tv);
-               uloop_run_events(uloop_get_next_timeout(&tv));
+
+               next_time = uloop_get_next_timeout(&tv);
+               if (timeout >= 0 && timeout < next_time)
+                       next_time = timeout;
+               uloop_run_events(next_time);
        }
 
-       if (!--recursive_calls)
-               uloop_setup_signals(false);
+       --uloop_run_depth;
 
        return uloop_status;
 }
 
 void uloop_done(void)
 {
+       uloop_setup_signals(false);
+
        if (poll_fd >= 0) {
                close(poll_fd);
                poll_fd = -1;