]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
sched: Use integer value for select timeout.
authorAndre Noll <maan@tuebingen.mpg.de>
Fri, 24 Sep 2021 16:11:06 +0000 (18:11 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Thu, 25 Aug 2022 13:37:26 +0000 (15:37 +0200)
This modifies the public struct sched so that users pass in the
default timeout as an integer value in milliseconds rather than
a struct timeval. This simplifies the code a little and eases the
transition from select(2) to poll(2) because poll(2) also takes a
plain integer for the timeout.

Since para_select() of fd.c now calls ms2tv() to convert the timeout
back to a struct timeval, all executables which link with fd.o must
also link with time.o. This was not the case for para_mixer and
para_audioc, so configure.ac needs to be adjusted accordingly.

17 files changed:
afs.c
audioc.c
audiod.c
client.c
configure.ac
fd.c
fd.h
filter.c
gui.c
interactive.c
interactive.h
play.c
recv.c
sched.c
sched.h
server.c
write.c

diff --git a/afs.c b/afs.c
index 710670255b2ec1cf67b9ab4e74823bfe19dd3a02..c216b35423bd954a018a64ab091162ef05c658bc 100644 (file)
--- a/afs.c
+++ b/afs.c
@@ -1003,8 +1003,7 @@ __noreturn void afs_init(int socket_fd)
        PARA_INFO_LOG("server_socket: %d\n", server_socket);
        init_admissible_files(OPT_STRING_VAL(AFS_INITIAL_MODE));
        register_command_task(&s);
-       s.default_timeout.tv_sec = 0;
-       s.default_timeout.tv_usec = 999 * 1000;
+       s.default_timeout = 1000;
        ret = write(socket_fd, "\0", 1);
        if (ret != 1) {
                if (ret == 0)
index af67063367044b675877173a1ed351bdd28f2dd1..76b3d6dba0b3c67742e78903e6352949da4285c4 100644 (file)
--- a/audioc.c
+++ b/audioc.c
@@ -252,7 +252,7 @@ __noreturn static void interactive_session(void)
        sigaction(SIGINT, &act, NULL);
        sched.select_function = i9e_select;
 
-       sched.default_timeout.tv_sec = 1;
+       sched.default_timeout = 1000;
        ret = i9e_open(&ici, &sched);
        if (ret < 0)
                goto out;
index 119adbc0be2ddbf24a7d06de4b77a5a3d88bd230..8e1da6e80e4c8c23e382cf367412274fd63fce69 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -1510,8 +1510,7 @@ int main(int argc, char *argv[])
                .context = signal_task,
        }, &sched);
 
-       sched.default_timeout.tv_sec = 2;
-       sched.default_timeout.tv_usec = 999 * 1000;
+       sched.default_timeout = 2999;
        ret = schedule(&sched);
        audiod_cleanup();
        sched_shutdown(&sched);
index 8caf44839919fb33c1368715c528f507ebc7a5c9..24f9c61c1c505e304b590b4ad7d7bce7041e13b0 100644 (file)
--- a/client.c
+++ b/client.c
@@ -123,7 +123,7 @@ fail:
 static int execute_client_command(const char *cmd, char **result)
 {
        int ret;
-       struct sched command_sched = {.default_timeout = {.tv_sec = 1}};
+       struct sched command_sched = {.default_timeout = 1000};
        struct exec_task exec_task = {
                .result_buf = para_strdup(""),
                .result_size = 1,
@@ -624,7 +624,7 @@ int main(int argc, char *argv[])
        int ret;
 
        crypt_init();
-       sched.default_timeout.tv_sec = 1;
+       sched.default_timeout = 1000;
 
        ret = client_parse_config(argc, argv, &ct, &client_loglevel);
        if (ret < 0)
index b817979f39ec79c141cd67eaaef6f49f559b5d21..0e37e39ef412521b611fe38353865c21f6e315d8 100644 (file)
@@ -586,7 +586,7 @@ fi
 if test $HAVE_OSS = yes -o $HAVE_ALSA = yes; then
        build_mixer="yes"
        executables="$executables mixer"
-       mixer_errlist_objs="mixer exec string fd lsu version"
+       mixer_errlist_objs="mixer exec string fd time lsu version"
        if test $HAVE_OSS = yes; then
                mixer_errlist_objs="$mixer_errlist_objs oss_mix"
        fi
@@ -842,6 +842,7 @@ audioc_errlist_objs="
        lsu
        net
        fd
+       time
        version
 "
 if test $HAVE_READLINE = yes; then
@@ -849,7 +850,6 @@ if test $HAVE_READLINE = yes; then
                buffer_tree
                interactive
                sched
-               time
        "
 fi
 audioc_objs="$audioc_errlist_objs"
diff --git a/fd.c b/fd.c
index 33891d2e6c9f1c3568428b1df93b4b92e295ef61..d72096e1e9674fb0d5307dc4005f8a3d3aa7e6dc 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -334,8 +334,7 @@ bool file_exists(const char *fn)
  * \param n The highest-numbered descriptor in any of the two sets, plus 1.
  * \param readfds fds that should be checked for readability.
  * \param writefds fds that should be checked for writablility.
- * \param timeout_tv upper bound on the amount of time elapsed before select()
- * returns.
+ * \param timeout Upper bound in milliseconds.
  *
  * \return The return value of the underlying select() call on success, the
  * negative system error code on errors.
@@ -343,12 +342,14 @@ bool file_exists(const char *fn)
  * All arguments are passed verbatim to select(2).
  * \sa select(2) select_tut(2).
  */
-int para_select(int n, fd_set *readfds, fd_set *writefds,
-               struct timeval *timeout_tv)
+int para_select(int n, fd_set *readfds, fd_set *writefds, int timeout)
 {
        int ret;
+       struct timeval tv;
+
+       ms2tv(timeout, &tv);
        do
-               ret = select(n, readfds, writefds, NULL, timeout_tv);
+               ret = select(n, readfds, writefds, NULL, &tv);
        while (ret < 0 && errno == EINTR);
        if (ret < 0)
                return -ERRNO_TO_PARA_ERROR(errno);
@@ -653,14 +654,11 @@ int para_munmap(void *start, size_t length)
 
 int write_ok(int fd)
 {
-       struct timeval tv;
        fd_set wfds;
 
        FD_ZERO(&wfds);
        FD_SET(fd, &wfds);
-       tv.tv_sec = 0;
-       tv.tv_usec = 0;
-       return para_select(fd + 1, NULL, &wfds, &tv);
+       return para_select(fd + 1, NULL, &wfds, 0);
 }
 
 /**
diff --git a/fd.h b/fd.h
index c9e79426f5c27ebf621367ce24b1c073c4e97e23..820e1cc97452d5f4c577f86f789d504391e96b59 100644 (file)
--- a/fd.h
+++ b/fd.h
@@ -6,8 +6,7 @@ int xrename(const char *oldpath, const char *newpath);
 int write_all(int fd, const char *buf, size_t len);
 __printf_2_3 int write_va_buffer(int fd, const char *fmt, ...);
 bool file_exists(const char *);
-int para_select(int n, fd_set *readfds, fd_set *writefds,
-               struct timeval *timeout_tv);
+int para_select(int n, fd_set *readfds, fd_set *writefds, int timeout);
 __must_check int mark_fd_nonblocking(int fd);
 __must_check int mark_fd_blocking(int fd);
 void para_fd_set(int fd, fd_set *fds, int *max_fileno);
index d4a2423904ca96f87f1ec11600f1c67e2b101ae1..95438779e6aa0c206ec2b07225d3fae1d4a977a5 100644 (file)
--- a/filter.c
+++ b/filter.c
@@ -149,8 +149,7 @@ int main(int argc, char *argv[])
                EMBRACE(.name = "stdout", .parent = parent));
        stdout_task_register(sot, &s);
 
-       s.default_timeout.tv_sec = 1;
-       s.default_timeout.tv_usec = 0;
+       s.default_timeout = 1000;
        btr_log_tree(sit->btrn, LL_INFO);
        ret = schedule(&s);
        sched_shutdown(&s);
diff --git a/gui.c b/gui.c
index d779ff864ddebfc38a34ba1a0d166b5c04089556..7a859a0cb559fa8d858237489e793c8b89fe77e5 100644 (file)
--- a/gui.c
+++ b/gui.c
@@ -1391,7 +1391,7 @@ static int setup_tasks_and_schedule(void)
        struct status_task status_task = {.fd = -1};
        struct input_task input_task = {.task = NULL};
        struct signal_task *signal_task;
-       struct sched sched = {.default_timeout = {.tv_sec = 1}};
+       struct sched sched = {.default_timeout = 1000};
 
        exec_task.task = task_register(&(struct task_info) {
                .name = "exec",
index 041376a1057bcf2e98a4c23c2b23c42d73a68345..8e61484009cb73b5842485d9168c48aaea9e0002 100644 (file)
@@ -261,12 +261,11 @@ static void clear_bottom_line(void)
 static bool input_available(void)
 {
        fd_set rfds;
-       struct timeval tv = {0, 0};
        int ret;
 
        FD_ZERO(&rfds);
        FD_SET(i9ep->ici->fds[0], &rfds);
-       ret = para_select(1, &rfds, NULL, &tv);
+       ret = para_select(1, &rfds, NULL, 0);
        return ret > 0;
 }
 
@@ -610,17 +609,20 @@ void i9e_signal_dispatch(int sig_num)
  * \param n \sa \ref para_select().
  * \param readfds \sa \ref para_select().
  * \param writefds \sa \ref para_select().
- * \param timeout_tv \sa \ref para_select().
+ * \param timeout \sa \ref para_select().
  *
  * \return \sa \ref para_select().
  *
  * The only difference between this function and \ref para_select() is that
  * \ref i9e_select() returns zero if the select call returned \p EINTR.
  */
-int i9e_select(int n, fd_set *readfds, fd_set *writefds,
-               struct timeval *timeout_tv)
+int i9e_select(int n, fd_set *readfds, fd_set *writefds, int timeout)
 {
-       int ret = select(n, readfds, writefds, NULL, timeout_tv);
+       struct timeval tv;
+       int ret;
+
+       ms2tv(timeout, &tv);
+       ret = select(n, readfds, writefds, NULL, &tv);
 
        if (ret < 0) {
                if (errno == EINTR)
index ddf02d76d2bc44a71133844fa0dddf4d9db03778..4253f79ccf3b511b4b901a3d2495f9ef0e1e715d 100644 (file)
@@ -84,8 +84,7 @@ void i9e_print_status_bar(char *buf, unsigned len);
 void i9e_close(void);
 void i9e_signal_dispatch(int sig_num);
 __printf_2_3 void i9e_log(int ll, const char* fmt,...);
-int i9e_select(int n, fd_set *readfds, fd_set *writefds,
-               struct timeval *timeout_tv);
+int i9e_select(int n, fd_set *readfds, fd_set *writefds, int timeout);
 int i9e_extract_completions(const char *word, char **string_list,
                char ***result);
 char **i9e_complete_commands(const char *word, struct i9e_completer *completers);
diff --git a/play.c b/play.c
index ba9fff70190c7e150b136e15eb5375015897b2d0..0c78b960f1161f9ecad7c1a4f1f06c3e75e9974f 100644 (file)
--- a/play.c
+++ b/play.c
@@ -1255,7 +1255,7 @@ int main(int argc, char *argv[])
        int ret;
        unsigned num_inputs;
 
-       sched.default_timeout.tv_sec = 5;
+       sched.default_timeout = 5000;
        parse_config_or_die(argc, argv);
        session_open();
        num_inputs = lls_num_inputs(play_lpr);
diff --git a/recv.c b/recv.c
index 10d55d218071ccf6a314fc87d4e5f78001012db4..a81eebb8cbe0aa6d81731959079a4183fa33ad6a 100644 (file)
--- a/recv.c
+++ b/recv.c
@@ -102,8 +102,7 @@ int main(int argc, char *argv[])
        ti.context = &rn;
        rn.task = task_register(&ti, &s);
 
-       s.default_timeout.tv_sec = 1;
-       s.default_timeout.tv_usec = 0;
+       s.default_timeout = 1000;
        ret = schedule(&s);
        sched_shutdown(&s);
        r->close(&rn);
diff --git a/sched.c b/sched.c
index aac8efed1bcf8d897e6aef4d07973f2babc8d4f4..8deb7f383596162e97444c7766f6d15bc60eb92c 100644 (file)
--- a/sched.c
+++ b/sched.c
@@ -137,12 +137,12 @@ int schedule(struct sched *s)
 again:
        FD_ZERO(&s->rfds);
        FD_ZERO(&s->wfds);
-       s->select_timeout = s->default_timeout;
+       s->timeout = s->default_timeout;
        s->max_fileno = -1;
        clock_get_realtime(&now_struct);
        sched_preselect(s);
        ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds,
-               &s->select_timeout);
+               s->timeout);
        if (ret < 0)
                return ret;
        if (ret == 0) {
@@ -370,7 +370,7 @@ void task_notify_all(struct sched *s, int err)
  */
 void sched_min_delay(struct sched *s)
 {
-       s->select_timeout.tv_sec = s->select_timeout.tv_usec = 0;
+       s->timeout = 0;
 }
 
 /**
@@ -387,8 +387,9 @@ void sched_min_delay(struct sched *s)
  */
 void sched_request_timeout(struct timeval *to, struct sched *s)
 {
-       if (tv_diff(&s->select_timeout, to, NULL) > 0)
-               s->select_timeout = *to;
+       long unsigned ms = tv2ms(to);
+       if (s->timeout > ms)
+               s->timeout = ms;
 }
 
 /**
diff --git a/sched.h b/sched.h
index 35e2503e383be3611fc302f5a732e45cf322d506..4695da05040d1934e0c23a12db9bb14c7e9f799c 100644 (file)
--- a/sched.h
+++ b/sched.h
  * called after the select call.
  */
 struct sched {
-       /** Initial value before any pre_select call. */
-       struct timeval default_timeout;
-       /** The current timeout for the upcoming select call. */
-       struct timeval select_timeout;
+       /** Initial value (in milliseconds) before any pre_select call. */
+       int default_timeout;
+       /** The timeout (also in milliseconds) for the next select call. */
+       int timeout;
        /** fds that should be watched for readability. */
        fd_set rfds;
        /** fds that should be watched for writability. */
@@ -24,7 +24,7 @@ struct sched {
        /** Highest numbered file descriptor in any of the above fd sets. */
        int max_fileno;
        /** If non-NULL, use this function instead of para_select. */
-       int (*select_function)(int, fd_set *, fd_set *, struct timeval *);
+       int (*select_function)(int, fd_set *, fd_set *, int timeout);
        /** Tasks which have been registered to the scheduler. */
        struct list_head task_list;
 };
index e0df714be975b083f4d3cc7116ad0af2501a2b46..729ca1ac69d4f1f86a09359d315dc84e60e873cf 100644 (file)
--- a/server.c
+++ b/server.c
@@ -618,13 +618,13 @@ out:
 }
 
 static int server_select(int max_fileno, fd_set *readfds, fd_set *writefds,
-               struct timeval *timeout_tv)
+               int timeout)
 {
        int ret;
 
        status_refresh();
        mutex_unlock(mmd_mutex);
-       ret = para_select(max_fileno + 1, readfds, writefds, timeout_tv);
+       ret = para_select(max_fileno + 1, readfds, writefds, timeout);
        mutex_lock(mmd_mutex);
        return ret;
 }
@@ -658,7 +658,7 @@ int main(int argc, char *argv[])
        struct server_command_task server_command_task_struct,
                *sct = &server_command_task_struct;
 
-       sched.default_timeout.tv_sec = 1;
+       sched.default_timeout = 1000;
        sched.select_function = server_select;
 
        server_init(argc, argv, sct);
diff --git a/write.c b/write.c
index acfb94605b53d259a89ea41e75507b81e2cb2ca4..177b2e66834a03d590f0eba5f1f145c63a703a59 100644 (file)
--- a/write.c
+++ b/write.c
@@ -96,8 +96,7 @@ static int setup_and_schedule(struct lls_parse_result *lpr)
                wns[i].wid = check_writer_arg_or_die(arg, &wns[i].lpr);
                register_writer_node(wns + i, cw_btrn, &s);
        }
-       s.default_timeout.tv_sec = 10;
-       s.default_timeout.tv_usec = 50000;
+       s.default_timeout = 10500;
        ret = schedule(&s);
        if (ret >= 0) {
                int j, ts;