From: Andre Noll Date: Sat, 18 May 2013 12:06:22 +0000 (+0200) Subject: Merge branch 't/sched_improvements' X-Git-Tag: v0.4.13~39 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=2f07d34b5d4c37606be5849b6ee51e0443707898;hp=624d59c6b2fb500f65d3e30e44da5e8c8d40f63a Merge branch 't/sched_improvements' This topic has been cooking for a month. During this period, one problem on BSD has been found and fixed. 3ad5b0 Fix build on FreeBSD 484e75 sched: Rename new_post_select back to post_select. 74c880 sched: Kill old ->post_select variant. c77e19 grab client: Switch to the alternative post select method. 38aeac client supervisor: Switch to the alternative post select method. e009fa client exec: Switch to the alternative post select method. 742718 i9e: Switch to the alternative post select method. 2e6b8f vss: Switch to the alternative post select method. 6e83b4 stdout: Switch to the alternative post select method. 1995ce stdin: Switch to the alternative post select method. 88b0ec audioc: Switch to the alternative post select method. b210e8 afs command task: Switch to the alternative post select method. 12f683 afs signal task: Switch to the alternative post select method. ccef24 server signal task: Switch to the alternative post select method. a6dabd server command task: Switch to the alternative post select method. 855c53 write: Switch to the alternative post select method. 58b74b play: Switch to the alternative post select method. cf4982 afh_recv: Switch to the alternative post select method. 4dc05b client: Switch to the alternative post select method. 043fd6 audiod: Switch command_task to the alternative post select method. ce462a audiod: Switch the status task to the alternative post select method. a7f2d1 audiod: Switch signal task to the alternative post select method. ba2f65 osx writer: Switch to the alternative post select method. 3642d2 oss writer: Switch to the alternative post select method. c29db3 file writer: Switch to the alternative post select method. 36875c ao: Switch to the alternative post select method. 3b3049 alsa: Switch to the alternative post select method. 60b853 wmadec: Switch to the alternative post select method. 6c8719 wav filter: Switch to the alternative post select method. 03e915 spxdec: Switch to the alternative post select method. 24bbc5 resample: Switch to the alternative post select method. 4ca80f prebuffer: Switch to the alternative post select method. a55083 oggdec: Switch to the alternative post select method. 806d26 mp3dec: Switch to the alternative post select method. 7dcaf5 flacdec: Switch to the alternative post select method. 4dc9b9 fecdec: Switch to the alternative post select method. f6e2cb compress: Switch to the alternative post select method. ac3371 amp: Switch to the alternative post select method. 7c2c68 aacdec: Switch to the alternative post select method. b333e0 dccp_recv: Switch to the alternative post select method. 00e793 udp_recv: Switch to the alternative post select method. 9c00a7 sched: Provide alternative post_select variant. 24758c Replace gettimeofday() by clock_gettime(). 01f802 sched: Get rid of (pre)select shortcuts. 5bb44a audiod: Avoid delay when closing slot. Conflicts: string.c --- diff --git a/Makefile.in b/Makefile.in index 173b81a3..fa2e468f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -79,6 +79,8 @@ CPPFLAGS += -I/usr/local/include CPPFLAGS += -I$(cmdline_dir) CPPFLAGS += @osl_cppflags@ +LDFLAGS += @clock_gettime_ldflags@ + man_pages := $(patsubst %, $(man_dir)/%.1, @executables@) autocrap := config.h.in configure diff --git a/NEWS b/NEWS index 591d935d..56f84753 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,9 @@ ------------------------------------------ - UTF8 support for para_gui and the mp3 audio format handler. + - Scheduler improvements and fixes. + - The obsolete gettimeofday() function has been replaced + by clock_gettime() on systems which support it. ----------------------------------------- 0.4.12 (2012-12-20) "volatile relativity" diff --git a/aacdec_filter.c b/aacdec_filter.c index 79e66620..3ff90834 100644 --- a/aacdec_filter.c +++ b/aacdec_filter.c @@ -80,7 +80,7 @@ static void aacdec_close(struct filter_node *fn) fn->private_data = NULL; } -static void aacdec_post_select(__a_unused struct sched *s, struct task *t) +static int aacdec_post_select(__a_unused struct sched *s, struct task *t) { struct filter_node *fn = container_of(t, struct filter_node, task); struct btr_node *btrn = fn->btrn; @@ -91,12 +91,11 @@ static void aacdec_post_select(__a_unused struct sched *s, struct task *t) size_t len, skip, consumed, loaded; next_buffer: - t->error = 0; ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL); if (ret < 0) goto err; if (ret == 0) - return; + return 0; btr_merge(btrn, fn->min_iqs); len = btr_next_buffer(btrn, (char **)&inbuf); len = PARA_MIN(len, (size_t)8192); @@ -203,8 +202,8 @@ out: } err: assert(ret < 0); - t->error = ret; btr_remove_node(&fn->btrn); + return ret; } /** diff --git a/afh.c b/afh.c index aab664c2..4c65d7c1 100644 --- a/afh.c +++ b/afh.c @@ -7,7 +7,6 @@ /** \file afh.c Paraslash's standalone audio format handler tool. */ #include -#include #include "para.h" #include "string.h" diff --git a/afh_common.c b/afh_common.c index 5c866c1f..6c161a7c 100644 --- a/afh_common.c +++ b/afh_common.c @@ -7,7 +7,6 @@ /** \file afh_common.c Common audio format handler functions. */ #include /* mmap */ -#include /* gettimeofday */ #include #include diff --git a/afh_recv.c b/afh_recv.c index febda8a9..774ae88c 100644 --- a/afh_recv.c +++ b/afh_recv.c @@ -173,7 +173,7 @@ static void afh_recv_pre_select(struct sched *s, struct task *t) sched_request_barrier_or_min_delay(&chunk_time, s); } -static void afh_recv_post_select(__a_unused struct sched *s, struct task *t) +static int afh_recv_post_select(__a_unused struct sched *s, struct task *t) { struct receiver_node *rn = container_of(t, struct receiver_node, task); struct afh_recv_args_info *conf = rn->conf; @@ -233,7 +233,7 @@ out: btr_remove_node(&rn->btrn); pard->current_chunk = pard->first_chunk; } - t->error = ret; + return ret; } /** diff --git a/afs.c b/afs.c index eb18708c..2f521291 100644 --- a/afs.c +++ b/afs.c @@ -723,9 +723,9 @@ static void signal_pre_select(struct sched *s, struct task *t) para_fd_set(st->fd, &s->rfds, &s->max_fileno); } -static void afs_signal_post_select(struct sched *s, struct task *t) +static int afs_signal_post_select(struct sched *s, __a_unused struct task *t) { - int signum; + int signum, ret; if (getppid() == 1) { PARA_EMERG_LOG("para_server died\n"); @@ -733,20 +733,20 @@ static void afs_signal_post_select(struct sched *s, struct task *t) } signum = para_next_signal(&s->rfds); if (signum == 0) - return; + return 0; if (signum == SIGHUP) { close_afs_tables(); parse_config_or_die(1); - t->error = open_afs_tables(); - if (t->error < 0) - return; + ret = open_afs_tables(); + if (ret < 0) + return ret; init_admissible_files(current_mop); - return; + return 0; } PARA_EMERG_LOG("terminating on signal %d\n", signum); shutdown: task_notify_all(s, E_AFS_SIGNAL); - t->error = -E_AFS_SIGNAL; + return -E_AFS_SIGNAL; } static void register_signal_task(struct sched *s) @@ -915,7 +915,7 @@ err: /** Shutdown connection if query has not arrived until this many seconds. */ #define AFS_CLIENT_TIMEOUT 3 -static void command_post_select(struct sched *s, struct task *t) +static int command_post_select(struct sched *s, struct task *t) { struct command_task *ct = container_of(t, struct command_task, task); struct sockaddr_un unix_addr; @@ -923,16 +923,13 @@ static void command_post_select(struct sched *s, struct task *t) int fd, ret; ret = task_get_notification(t); - if (ret < 0) { - t->error = ret; - return; - } + if (ret < 0) + return ret; ret = execute_server_command(&s->rfds); if (ret < 0) { PARA_EMERG_LOG("%s\n", para_strerror(-ret)); task_notify_all(s, -ret); - t->error = ret; - return; + return ret; } /* Check the list of connected clients. */ list_for_each_entry_safe(client, tmp, &afs_client_list, node) { @@ -953,17 +950,18 @@ static void command_post_select(struct sched *s, struct task *t) if (ret < 0) PARA_NOTICE_LOG("%s\n", para_strerror(-ret)); if (ret <= 0) - return; + return 0; ret = mark_fd_nonblocking(fd); if (ret < 0) { PARA_NOTICE_LOG("%s\n", para_strerror(-ret)); close(fd); - return; + return 0; } client = para_malloc(sizeof(*client)); client->fd = fd; client->connect_time = *now; para_list_add(&client->node, &afs_client_list); + return 0; } static void register_command_task(uint32_t cookie, struct sched *s) diff --git a/alsa_write.c b/alsa_write.c index ba844db2..0563ba73 100644 --- a/alsa_write.c +++ b/alsa_write.c @@ -15,7 +15,6 @@ #include #include #include -#include #include "para.h" #include "fd.h" @@ -236,7 +235,7 @@ static void alsa_close(struct writer_node *wn) free(pad); } -static void alsa_write_post_select(__a_unused struct sched *s, +static int alsa_write_post_select(__a_unused struct sched *s, struct task *t) { struct writer_node *wn = container_of(t, struct writer_node, task); @@ -253,7 +252,7 @@ static void alsa_write_post_select(__a_unused struct sched *s, again: ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF); if (ret == 0) - return; + return 0; btr_merge(btrn, wn->min_iqs); bytes = btr_next_buffer(btrn, &data); if (ret < 0 || bytes < wn->min_iqs) { /* eof */ @@ -266,17 +265,17 @@ again: PARA_DEBUG_LOG("waiting for device to drain\n"); tv_add(now, &(struct timeval)EMBRACE(0, 200 * 1000), &pad->drain_barrier); - return; + return 0; } if (tv_diff(now, &pad->drain_barrier, NULL) > 0) goto err; - return; + return 0; } if (!pad) { int32_t val; if (bytes == 0) /* no data available */ - return; + return 0; pad = para_calloc(sizeof(*pad)); get_btr_sample_rate(btrn, &val); pad->sample_rate = val; @@ -297,7 +296,7 @@ again: goto again; } if (pad->poll_fd < 0 || !FD_ISSET(pad->poll_fd, &s->rfds)) - return; + return 0; frames = bytes / pad->bytes_per_frame; frames = snd_pcm_writei(pad->handle, data, frames); if (frames == 0 || frames == -EAGAIN) { @@ -311,7 +310,7 @@ again: char buf[100]; if (read(pad->poll_fd, buf, 100)) do_nothing; - return; + return 0; } if (frames > 0) { btr_consume(btrn, frames * pad->bytes_per_frame); @@ -320,14 +319,14 @@ again: if (frames == -EPIPE) { PARA_WARNING_LOG("underrun (tried to write %zu bytes)\n", bytes); snd_pcm_prepare(pad->handle); - return; + return 0; } PARA_ERROR_LOG("alsa write error: %s\n", snd_strerror(-frames)); ret = -E_ALSA; err: assert(ret < 0); btr_remove_node(&wn->btrn); - t->error = ret; + return ret; } __malloc static void *alsa_parse_config_or_die(int argc, char **argv) diff --git a/amp_filter.c b/amp_filter.c index 90b5660d..a6bfca25 100644 --- a/amp_filter.c +++ b/amp_filter.c @@ -62,7 +62,7 @@ static void amp_open(struct filter_node *fn) pad->amp, pad->amp / 64.0 + 1.0); } -static void amp_post_select(__a_unused struct sched *s, struct task *t) +static int amp_post_select(__a_unused struct sched *s, struct task *t) { struct filter_node *fn = container_of(t, struct filter_node, task); struct private_amp_data *pad = fn->private_data; @@ -73,16 +73,15 @@ static void amp_post_select(__a_unused struct sched *s, struct task *t) bool inplace = btr_inplace_ok(btrn); if (pad->amp == 0) { /* no amplification */ - t->error = -E_AMP_ZERO_AMP; btr_splice_out_node(btrn); - return; + return -E_AMP_ZERO_AMP; } next_buffer: ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL); if (ret < 0) goto err; if (ret == 0) - return; + return 0; btr_merge(btrn, fn->min_iqs); in_bytes = btr_next_buffer(btrn, (char **)&in); len = in_bytes / 2; @@ -110,12 +109,11 @@ next_buffer: btr_consume(btrn, len * 2); btr_add_output((char *)out, len * 2, btrn); } - t->error = 0; goto next_buffer; err: assert(ret < 0); - t->error = ret; btr_remove_node(&fn->btrn); + return ret; } static void amp_free_config(void *conf) diff --git a/ao_write.c b/ao_write.c index ffe86699..9d204ff3 100644 --- a/ao_write.c +++ b/ao_write.c @@ -276,7 +276,7 @@ fail: return -E_AO_PTHREAD; } -static void aow_post_select(__a_unused struct sched *s, +static int aow_post_select(__a_unused struct sched *s, struct task *t) { struct writer_node *wn = container_of(t, struct writer_node, task); @@ -291,7 +291,7 @@ static void aow_post_select(__a_unused struct sched *s, if (ret < 0) goto remove_btrn; if (ret == 0) - return; + return 0; get_btr_sample_rate(wn->btrn, &rate); get_btr_channels(wn->btrn, &ch); get_btr_sample_format(wn->btrn, &format); @@ -312,7 +312,7 @@ static void aow_post_select(__a_unused struct sched *s, ret = aow_create_thread(wn); if (ret < 0) goto remove_thread_btrn; - return; + return 0; } pthread_mutex_lock(&pawd->mutex); ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF); @@ -334,7 +334,7 @@ remove_thread_btrn: remove_btrn: btr_remove_node(&wn->btrn); out: - t->error = ret; + return ret; } __malloc static void *aow_parse_config_or_die(int argc, char **argv) diff --git a/audioc.c b/audioc.c index abd76359..74fb11cb 100644 --- a/audioc.c +++ b/audioc.c @@ -106,7 +106,7 @@ static void audioc_pre_select(struct sched *s, struct task *t) para_fd_set(at->fd, &s->rfds, &s->max_fileno); } -static void audioc_post_select(struct sched *s, struct task *t) +static int audioc_post_select(struct sched *s, struct task *t) { char *buf = NULL; struct audioc_task *at = container_of(t, struct audioc_task, task); @@ -115,7 +115,7 @@ static void audioc_post_select(struct sched *s, struct task *t) if (ret < 0) goto out; if (!FD_ISSET(at->fd, &s->rfds)) - return; + return 0; buf = para_malloc(conf.bufsize_arg); ret = recv_bin_buffer(at->fd, buf, conf.bufsize_arg); PARA_DEBUG_LOG("recv: %d\n", ret); @@ -124,14 +124,14 @@ static void audioc_post_select(struct sched *s, struct task *t) if (ret < 0) goto out; btr_add_output(buf, ret, at->btrn); - return; + return 0; out: if (ret < 0) { free(buf); btr_remove_node(&at->btrn); close(at->fd); } - t->error = ret; + return ret; } static struct audioc_task audioc_task = { diff --git a/audiod.c b/audiod.c index 4f2d4151..d2c66f2b 100644 --- a/audiod.c +++ b/audiod.c @@ -496,7 +496,6 @@ static void open_filters(struct slot_info *s) fn->conf = a->filter_conf[i]; fn->task.pre_select = f->pre_select; fn->task.post_select = f->post_select; - fn->btrn = btr_new_node(&(struct btr_node_description) EMBRACE(.name = f->name, .parent = parent, .handler = f->execute, .context = fn)); @@ -992,7 +991,7 @@ static void signal_pre_select(struct sched *s, struct task *t) para_fd_set(st->fd, &s->rfds, &s->max_fileno); } -static void signal_post_select(struct sched *s, __a_unused struct task *t) +static int signal_post_select(struct sched *s, __a_unused struct task *t) { int signum; @@ -1004,6 +1003,7 @@ static void signal_post_select(struct sched *s, __a_unused struct task *t) PARA_EMERG_LOG("terminating on signal %d\n", signum); clean_exit(EXIT_FAILURE, "caught deadly signal"); } + return 0; } static void signal_setup_default(struct signal_task *st) @@ -1019,7 +1019,7 @@ static void command_pre_select(struct sched *s, struct task *t) para_fd_set(ct->fd, &s->rfds, &s->max_fileno); } -static void command_post_select(struct sched *s, struct task *t) +static int command_post_select(struct sched *s, struct task *t) { int ret; struct command_task *ct = container_of(t, struct command_task, task); @@ -1036,6 +1036,7 @@ static void command_post_select(struct sched *s, struct task *t) if (ret < 0) PARA_ERROR_LOG("%s\n", para_strerror(-ret)); audiod_status_dump(); + return 0; } static void init_command_task(struct command_task *ct) @@ -1089,32 +1090,44 @@ static void set_stat_task_restart_barrier(unsigned seconds) tv_add(now, &delay, &stat_task->restart_barrier); } -static void try_to_close_slot(int slot_num) +static bool must_close_slot(int slot_num) { struct slot_info *s = &slot[slot_num]; struct audio_format_info *a = afi + s->format; int i; if (s->format < 0) - return; + return false; if (s->receiver_node && s->receiver_node->task.error >= 0) - return; + return false; for (i = 0; i < a->num_filters; i++) if (s->fns && s->fns[i].task.error >= 0) - return; + return false; if (a->num_writers > 0) { for (i = 0; i < a->num_writers; i++) if (s->wns && s->wns[i].task.error >= 0) - return; + return false; } else { if (s->wns && s->wns[0].task.error >= 0) - return; + return false; + } + return true; +} + +static void close_unused_slots(void) +{ + int i; + + FOR_EACH_SLOT(i) { + struct slot_info *s = slot + i; + if (!must_close_slot(i)) + continue; + PARA_INFO_LOG("closing slot %d\n", i); + close_writers(s); + close_filters(s); + close_receiver(i); + clear_slot(i); } - PARA_INFO_LOG("closing slot %d\n", slot_num); - close_writers(s); - close_filters(s); - close_receiver(slot_num); - clear_slot(slot_num); } /* @@ -1123,12 +1136,11 @@ static void try_to_close_slot(int slot_num) */ static void start_stop_decoders(void) { - int i, ret; + int ret; struct slot_info *sl; struct audio_format_info *a; - FOR_EACH_SLOT(i) - try_to_close_slot(i); + close_unused_slots(); if (audiod_status != AUDIOD_ON || !(stat_task->vss_status & VSS_STATUS_FLAG_PLAYING)) return notify_receivers(E_NOT_PLAYING); @@ -1151,10 +1163,13 @@ static void start_stop_decoders(void) static void status_pre_select(struct sched *s, struct task *t) { struct status_task *st = container_of(t, struct status_task, task); - int ret, cafn = stat_task->current_audio_format_num; + int i, ret, cafn = stat_task->current_audio_format_num; if (must_start_decoder()) goto min_delay; + FOR_EACH_SLOT(i) + if (must_close_slot(i)) + goto min_delay; ret = btr_node_status(st->btrn, st->min_iqs, BTR_NT_LEAF); if (ret > 0) goto min_delay; @@ -1177,7 +1192,7 @@ min_delay: } /* restart the client task if necessary */ -static void status_post_select(struct sched *s, struct task *t) +static int status_post_select(struct sched *s, struct task *t) { struct status_task *st = container_of(t, struct status_task, task); @@ -1250,6 +1265,7 @@ static void status_post_select(struct sched *s, struct task *t) st->last_status_read = *now; out: start_stop_decoders(); + return 0; } static void init_status_task(struct status_task *st) diff --git a/client.c b/client.c index 715df9c6..90dc432b 100644 --- a/client.c +++ b/client.c @@ -54,7 +54,7 @@ static void exec_pre_select(struct sched *s, struct task *t) sched_min_delay(s); } -static void exec_post_select(__a_unused struct sched *s, struct task *t) +static int exec_post_select(__a_unused struct sched *s, struct task *t) { struct exec_task *et = container_of(t, struct exec_task, task); struct btr_node *btrn = et->btrn; @@ -63,10 +63,8 @@ static void exec_post_select(__a_unused struct sched *s, struct task *t) int ret; ret = btr_node_status(btrn, 0, BTR_NT_LEAF); - if (ret <= 0) { - t->error = ret; - return; - } + if (ret <= 0) + return ret; sz = btr_next_buffer(btrn, &buf); if (sz <= 1) goto out; @@ -76,6 +74,7 @@ static void exec_post_select(__a_unused struct sched *s, struct task *t) et->result_buf[et->result_size - 1] = '\0'; out: btr_consume(btrn, sz); + return 0; } static int make_client_argv(const char *line) @@ -531,23 +530,21 @@ __noreturn static void print_completions(void) #endif /* HAVE_READLINE */ -static void supervisor_post_select(struct sched *s, struct task *t) +static int supervisor_post_select(struct sched *s, __a_unused struct task *t) { - if (ct->task.error < 0) { - t->error = ct->task.error; - return; - } + if (ct->task.error < 0) + return ct->task.error; if (ct->status == CL_SENDING) { stdin_set_defaults(&sit); register_task(s, &sit.task); - t->error = -E_TASK_STARTED; - return; + return -E_TASK_STARTED; } if (ct->status == CL_RECEIVING) { stdout_set_defaults(&sot); register_task(s, &sot.task); - t->error = -E_TASK_STARTED; return; + return -E_TASK_STARTED; } + return 0; } static struct task svt = { diff --git a/client_common.c b/client_common.c index 7bdb4738..8ea41922 100644 --- a/client_common.c +++ b/client_common.c @@ -326,7 +326,7 @@ static int send_sb_command(struct client_task *ct) * * \sa struct sched, struct task. */ -static void client_post_select(struct sched *s, struct task *t) +static int client_post_select(struct sched *s, struct task *t) { struct client_task *ct = container_of(t, struct client_task, task); struct btr_node *btrn = ct->btrn; @@ -338,7 +338,7 @@ static void client_post_select(struct sched *s, struct task *t) if (ret < 0) goto out; if (ct->scc.fd < 0) - return; + return 0; switch (ct->status) { case CL_CONNECTED: /* receive welcome message */ ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n); @@ -346,10 +346,10 @@ static void client_post_select(struct sched *s, struct task *t) goto out; ct->features = parse_features(buf); ct->status = CL_RECEIVED_WELCOME; - return; + return 0; case CL_RECEIVED_WELCOME: /* send auth command */ if (!FD_ISSET(ct->scc.fd, &s->wfds)) - return; + return 0; if (has_feature("sideband", ct)) { ct->use_sideband = true; sprintf(buf, AUTH_REQUEST_MSG "%s sideband", ct->user); @@ -360,7 +360,7 @@ static void client_post_select(struct sched *s, struct task *t) if (ret < 0) goto out; ct->status = CL_SENT_AUTH; - return; + return 0; case CL_SENT_AUTH: /* * Receive challenge and session keys, decrypt the challenge and @@ -406,7 +406,7 @@ static void client_post_select(struct sched *s, struct task *t) hash_to_asc(ct->challenge_hash, buf); PARA_INFO_LOG("--> %s\n", buf); ct->status = CL_RECEIVED_CHALLENGE; - return; + return 0; } case CL_RECEIVED_CHALLENGE: if (ct->use_sideband) { @@ -447,20 +447,20 @@ static void client_post_select(struct sched *s, struct task *t) if (!strstr(buf, PROCEED_MSG)) goto out; ct->status = CL_RECEIVED_PROCEED; - return; + return 0; } case CL_RECEIVED_PROCEED: /* concat args and send command */ { int i; char *command = NULL; if (!FD_ISSET(ct->scc.fd, &s->wfds)) - return; + return 0; if (ct->use_sideband) { ret = send_sb_command(ct); if (ret <= 0) goto out; ct->status = CL_SENT_COMMAND; - return; + return 0; } for (i = 0; i < ct->conf.inputs_num; i++) { char *tmp = command; @@ -475,7 +475,7 @@ static void client_post_select(struct sched *s, struct task *t) if (ret < 0) goto out; ct->status = CL_SENT_COMMAND; - return; + return 0; } case CL_SENT_COMMAND: { @@ -501,7 +501,7 @@ static void client_post_select(struct sched *s, struct task *t) if (strstr(buf2, AWAITING_DATA_MSG)) { free(buf2); ct->status = CL_SENDING; - return; + return 0; } ct->status = CL_RECEIVING; btr_add_output(buf2, n, btrn); @@ -517,15 +517,15 @@ static void client_post_select(struct sched *s, struct task *t) if (ret < 0) goto out; if (ret == 0) - return; + return 0; if (!FD_ISSET(ct->scc.fd, &s->wfds)) - return; + return 0; sz = btr_next_buffer(btrn, &buf2); ret = sc_send_bin_buffer(&ct->scc, buf2, sz); if (ret < 0) goto out; btr_consume(btrn, sz); - return; + return 0; } case CL_RECEIVING: { @@ -534,13 +534,13 @@ static void client_post_select(struct sched *s, struct task *t) if (ret < 0) goto out; if (ret == 0) - return; + return 0; /* * The FD_ISSET() is not strictly necessary, but is allows us * to skip the malloc below if there is nothing to read anyway. */ if (!FD_ISSET(ct->scc.fd, &s->rfds)) - return; + return 0; if (ct->use_sideband) { struct sb_buffer sbb; ret = recv_sb(ct, &s->rfds, &sbb); @@ -559,13 +559,13 @@ static void client_post_select(struct sched *s, struct task *t) } } out: - t->error = ret; if (ret < 0) { if (!ct->use_sideband && ret != -E_SERVER_EOF && ret != -E_BTR_EOF && ret != -E_EOF) - PARA_ERROR_LOG("%s\n", para_strerror(-t->error)); + PARA_ERROR_LOG("%s\n", para_strerror(-ret)); btr_remove_node(&ct->btrn); } + return ret; } /** diff --git a/command.c b/command.c index aaaaaecf..ec822c82 100644 --- a/command.c +++ b/command.c @@ -8,7 +8,6 @@ #include #include -#include #include #include @@ -124,7 +123,7 @@ static unsigned get_status(struct misc_meta_data *nmmd, int parser_friendly, localtime_r(&nmmd->mtime, &mtime_tm); strftime(mtime, 29, "%b %d %Y", &mtime_tm); } - gettimeofday(¤t_time, NULL); + clock_get_realtime(¤t_time); /* * The calls to WRITE_STATUS_ITEM() below never fail because * b->max_size is zero (unlimited), see para_printf(). However, clang diff --git a/compress_filter.c b/compress_filter.c index 04377b75..d7162791 100644 --- a/compress_filter.c +++ b/compress_filter.c @@ -41,7 +41,7 @@ static void compress_close(struct filter_node *fn) free(fn->private_data); } -static void compress_post_select(__a_unused struct sched *s, struct task *t) +static int compress_post_select(__a_unused struct sched *s, struct task *t) { struct filter_node *fn = container_of(t, struct filter_node, task); struct private_compress_data *pcd = fn->private_data; @@ -56,12 +56,11 @@ static void compress_post_select(__a_unused struct sched *s, struct task *t) //inplace = false; next_buffer: - t->error = 0; ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL); if (ret < 0) goto err; if (ret == 0) - return; + return 0; btr_merge(btrn, fn->min_iqs); length = btr_next_buffer(btrn, &inbuf) & ~(size_t)1; if (length == 0) { /* eof and 1 byte available */ @@ -108,8 +107,8 @@ next_buffer: goto next_buffer; err: assert(ret < 0); - t->error = ret; btr_remove_node(&fn->btrn); + return ret; } /** TODO: Add sanity checks */ diff --git a/configure.ac b/configure.ac index bf83b916..135a0bd9 100644 --- a/configure.ac +++ b/configure.ac @@ -247,6 +247,18 @@ if test x$ac_cv_have_working_snprintf$ac_cv_have_working_vsnprintf != "xyesyes"; AC_MSG_ERROR([fatal: buggy snprintf() detected]) fi]) AX_FUNC_SNPRINTF() +################################################################## clock_gettime +clock_gettime_lib= +AC_CHECK_LIB([c], [clock_gettime], [clock_gettime_lib=c], [ + AC_CHECK_LIB([rt], [clock_gettime], [clock_gettime_lib=rt], [], []) +]) +if test -n "$clock_gettime_lib"; then + AC_DEFINE(HAVE_CLOCK_GETTIME, 1, [ + define to 1 if clock_gettime() is supported]) +fi +if test "$clock_gettime_lib" = "rt"; then + AC_SUBST(clock_gettime_ldflags, -lrt) +fi ########################################################################### osl have_osl=yes OLD_CPPFLAGS="$CPPFLAGS" diff --git a/daemon.c b/daemon.c index 29b00ed2..18ad1568 100644 --- a/daemon.c +++ b/daemon.c @@ -10,7 +10,6 @@ #include #include /* getgrnam() */ #include -#include #include #include "para.h" @@ -374,7 +373,7 @@ __printf_2_3 void daemon_log(int ll, const char* fmt,...) fprintf(fp, "%s", color); if (log_time || log_timing) { struct timeval tv; - gettimeofday(&tv, NULL); + clock_get_realtime(&tv); if (daemon_test_flag(DF_LOG_TIME)) { /* print date and time */ time_t t1 = tv.tv_sec; char str[100]; diff --git a/dccp_recv.c b/dccp_recv.c index eb442f30..1f9df189 100644 --- a/dccp_recv.c +++ b/dccp_recv.c @@ -124,7 +124,7 @@ static void dccp_recv_pre_select(struct sched *s, struct task *t) para_fd_set(rn->fd, &s->rfds, &s->max_fileno); } -static void dccp_recv_post_select(struct sched *s, struct task *t) +static int dccp_recv_post_select(struct sched *s, struct task *t) { struct receiver_node *rn = container_of(t, struct receiver_node, task); struct btr_node *btrn = rn->btrn; @@ -152,10 +152,9 @@ static void dccp_recv_post_select(struct sched *s, struct task *t) btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len, btrn); } out: - if (ret >= 0) - return; - btr_remove_node(&rn->btrn); - t->error = ret; + if (ret < 0) + btr_remove_node(&rn->btrn); + return ret; } static void dccp_recv_free_config(void *conf) diff --git a/fecdec_filter.c b/fecdec_filter.c index d5a708cf..2f0a6746 100644 --- a/fecdec_filter.c +++ b/fecdec_filter.c @@ -436,7 +436,7 @@ static void fecdec_close(struct filter_node *fn) fn->private_data = NULL; } -static void fecdec_post_select(__a_unused struct sched *s, struct task *t) +static int fecdec_post_select(__a_unused struct sched *s, struct task *t) { struct filter_node *fn = container_of(t, struct filter_node, task); struct btr_node *btrn = fn->btrn; @@ -468,9 +468,9 @@ next_buffer: btr_consume(btrn, FEC_HEADER_SIZE + h.slice_bytes); goto next_buffer; out: - t->error = ret; if (ret < 0) btr_remove_node(&fn->btrn); + return ret; } static void fecdec_open(struct filter_node *fn) diff --git a/file_write.c b/file_write.c index a12867d5..28fddcb6 100644 --- a/file_write.c +++ b/file_write.c @@ -8,7 +8,6 @@ #include #include -#include #include "para.h" #include "list.h" @@ -38,10 +37,8 @@ struct private_file_write_data { __must_check __malloc static char *random_filename(void) { char *result, *home = para_homedir(); - struct timeval tv; - gettimeofday(&tv, NULL); - srandom(tv.tv_usec); + srandom(clock_get_realtime(NULL)->tv_usec); result = make_message("%s/.paraslash/%08lu", home, para_random(99999999)); free(home); @@ -100,7 +97,7 @@ static void file_write_close(struct writer_node *wn) free(pfwd); } -static void file_write_post_select(__a_unused struct sched *s, +static int file_write_post_select(__a_unused struct sched *s, struct task *t) { struct writer_node *wn = container_of(t, struct writer_node, task); @@ -121,7 +118,7 @@ static void file_write_post_select(__a_unused struct sched *s, goto out; } if (!FD_ISSET(pfwd->fd, &s->wfds)) - return; + return 0; bytes = btr_next_buffer(btrn, &buf); assert(bytes > 0); //PARA_INFO_LOG("writing %zu\n", bytes); @@ -132,7 +129,7 @@ static void file_write_post_select(__a_unused struct sched *s, out: if (ret < 0) btr_remove_node(&wn->btrn); - t->error = ret; + return ret; } __malloc static void *file_write_parse_config_or_die(int argc, char **argv) diff --git a/filter.h b/filter.h index eefa6f69..9c57904e 100644 --- a/filter.h +++ b/filter.h @@ -109,7 +109,7 @@ struct filter { * post_select function is supposed to set t->error to a (negative) * error code. */ - void (*post_select)(struct sched *s, struct task *t); + int (*post_select)(struct sched *s, struct task *t); /** * Answer a buffer tree query. * diff --git a/flacdec_filter.c b/flacdec_filter.c index b0f499c3..09b319a0 100644 --- a/flacdec_filter.c +++ b/flacdec_filter.c @@ -216,7 +216,7 @@ static void flacdec_pre_select(struct sched *s, struct task *t) return sched_min_delay(s); } -static void flacdec_post_select(__a_unused struct sched *s, struct task *t) +static int flacdec_post_select(__a_unused struct sched *s, struct task *t) { struct filter_node *fn = container_of(t, struct filter_node, task); struct private_flacdec_data *pfd = fn->private_data; @@ -224,7 +224,7 @@ static void flacdec_post_select(__a_unused struct sched *s, struct task *t) int ret; if (output_queue_full(btrn)) - return; + return 0; ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL); if (ret < 0 && ret != -E_BTR_EOF) /* fatal error */ goto out; @@ -255,9 +255,9 @@ static void flacdec_post_select(__a_unused struct sched *s, struct task *t) fn->min_iqs = 0; ret = 1; out: - t->error = ret; if (ret < 0) btr_remove_node(&fn->btrn); + return ret; } static void flacdec_close(struct filter_node *fn) diff --git a/grab_client.c b/grab_client.c index a800b70b..ecc16fc8 100644 --- a/grab_client.c +++ b/grab_client.c @@ -108,7 +108,7 @@ static void gc_pre_select(struct sched *s, struct task *t) * We need this forward declaration as post_select() needs * activate_grab_client and vice versa. */ -static void gc_post_select(struct sched *s, struct task *t); +static int gc_post_select(struct sched *s, struct task *t); /** * Move a grab client to the active list and start it. @@ -184,7 +184,7 @@ static int gc_close(struct grab_client *gc, int err) return 0; } -static void gc_post_select(__a_unused struct sched *s, struct task *t) +static int gc_post_select(__a_unused struct sched *s, struct task *t) { struct grab_client *gc = container_of(t, struct grab_client, task); struct btr_node *btrn = gc->btrn; @@ -192,10 +192,9 @@ static void gc_post_select(__a_unused struct sched *s, struct task *t) size_t sz; char *buf; - t->error = 0; ret = btr_node_status(btrn, 0, BTR_NT_LEAF); if (ret == 0) - return; + return 0; if (ret < 0) goto err; sz = btr_next_buffer(btrn, &buf); @@ -205,10 +204,10 @@ static void gc_post_select(__a_unused struct sched *s, struct task *t) goto err; if (ret > 0) btr_consume(btrn, ret); - return; + return 0; err: gc_close(gc, ret); - t->error = ret; + return ret; } static int gc_check_args(int argc, char **argv, struct grab_client *gc) diff --git a/http_recv.c b/http_recv.c index a913d3a2..f1d85930 100644 --- a/http_recv.c +++ b/http_recv.c @@ -60,7 +60,6 @@ static void http_recv_pre_select(struct sched *s, struct task *t) struct receiver_node *rn = container_of(t, struct receiver_node, task); struct private_http_recv_data *phd = rn->private_data; - t->error = 0; if (generic_recv_pre_select(s, t) <= 0) return; if (phd->status == HTTP_CONNECTED) @@ -74,7 +73,7 @@ static void http_recv_pre_select(struct sched *s, struct task *t) * area with data read from the socket. In any case, update the state of the * connection if necessary. */ -static void http_recv_post_select(struct sched *s, struct task *t) +static int http_recv_post_select(struct sched *s, struct task *t) { struct receiver_node *rn = container_of(t, struct receiver_node, task); struct private_http_recv_data *phd = rn->private_data; @@ -90,11 +89,11 @@ static void http_recv_post_select(struct sched *s, struct task *t) if (ret < 0) goto out; if (ret == 0) - return; + return 0; if (phd->status == HTTP_CONNECTED) { char *rq; if (!FD_ISSET(rn->fd, &s->wfds)) - return; + return 0; rq = make_request_msg(); PARA_INFO_LOG("sending http request\n"); ret = write_va_buffer(rn->fd, "%s", rq); @@ -102,17 +101,17 @@ static void http_recv_post_select(struct sched *s, struct task *t) if (ret < 0) goto out; phd->status = HTTP_SENT_GET_REQUEST; - return; + return 0; } if (phd->status == HTTP_SENT_GET_REQUEST) { ret = read_pattern(rn->fd, HTTP_OK_MSG, strlen(HTTP_OK_MSG), &s->rfds); if (ret < 0) goto out; if (ret == 0) - return; + return 0; PARA_INFO_LOG("received ok msg, streaming\n"); phd->status = HTTP_STREAMING; - return; + return 0; } ret = -E_HTTP_RECV_OVERRUN; iovcnt = btr_pool_get_buffers(rn->btrp, iov); @@ -128,10 +127,9 @@ static void http_recv_post_select(struct sched *s, struct task *t) btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len, btrn); } out: - if (ret >= 0) - return; - btr_remove_node(&rn->btrn); - t->error = ret; + if (ret < 0) + btr_remove_node(&rn->btrn); + return ret; } static void http_recv_close(struct receiver_node *rn) diff --git a/interactive.c b/interactive.c index bc3a7c1e..f2819012 100644 --- a/interactive.c +++ b/interactive.c @@ -307,7 +307,7 @@ static void i9e_line_handler(char *line) } } -static void i9e_post_select(__a_unused struct sched *s, struct task *t) +static int i9e_post_select(__a_unused struct sched *s, __a_unused struct task *t) { int ret; struct i9e_client_info *ici = i9ep->ici; @@ -361,7 +361,7 @@ rm_btrn: wipe_bottom_line(); out: i9ep->caught_sigint = false; - t->error = ret; + return ret; } static void i9e_pre_select(struct sched *s, __a_unused struct task *t) diff --git a/mp3dec_filter.c b/mp3dec_filter.c index bd1f0d35..98627504 100644 --- a/mp3dec_filter.c +++ b/mp3dec_filter.c @@ -77,7 +77,7 @@ static void mp3dec_close(struct filter_node *fn) #define MP3DEC_MAX_FRAME 8192 -static void mp3dec_post_select(__a_unused struct sched *s, struct task *t) +static int mp3dec_post_select(__a_unused struct sched *s, struct task *t) { struct filter_node *fn = container_of(t, struct filter_node, task); int i, ret; @@ -88,13 +88,12 @@ static void mp3dec_post_select(__a_unused struct sched *s, struct task *t) next_buffer: pmd->stream.error = 0; - t->error = 0; iqs = btr_get_input_queue_size(btrn); ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL); if (ret < 0) goto err; if (ret == 0) - return; + return 0; btr_merge(btrn, fn->min_iqs); len = btr_next_buffer(btrn, &inbuffer); /* @@ -120,7 +119,7 @@ next_frame: } if (loaded == 0) goto next_buffer; - return; + return 0; } pmd->sample_rate = pmd->frame.header.samplerate; pmd->channels = MAD_NCHANNELS(&pmd->frame.header); @@ -140,12 +139,12 @@ decode: if (fn->min_iqs > MP3DEC_MAX_FRAME) goto err; mp3dec_consume(btrn, &pmd->stream, len); - return; + return 0; } if (pmd->stream.error != MAD_ERROR_BADDATAPTR) goto decode; mp3dec_consume(btrn, &pmd->stream, len); - return; + return 0; } fn->min_iqs = 0; mad_synth_frame(&pmd->synth, &pmd->frame); @@ -165,8 +164,8 @@ decode: goto next_frame; err: assert(ret < 0); - t->error = ret; btr_remove_node(&fn->btrn); + return ret; } static void mp3dec_open(struct filter_node *fn) diff --git a/oggdec_filter.c b/oggdec_filter.c index b405f869..f41150f2 100644 --- a/oggdec_filter.c +++ b/oggdec_filter.c @@ -199,7 +199,7 @@ static void ogg_pre_select(struct sched *s, struct task *t) sched_min_delay(s); } -static void ogg_post_select(__a_unused struct sched *s, struct task *t) +static int ogg_post_select(__a_unused struct sched *s, struct task *t) { struct filter_node *fn = container_of(t, struct filter_node, task); struct private_oggdec_data *pod = fn->private_data; @@ -217,7 +217,7 @@ static void ogg_post_select(__a_unused struct sched *s, struct task *t) } else if (ret == 0 && !pod->have_more) /* nothing to do */ goto out; if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE) - return; + return 0; if (!pod->vf) { if (ret <= 0) goto out; @@ -254,12 +254,12 @@ static void ogg_post_select(__a_unused struct sched *s, struct task *t) if (ret == OV_HOLE) /* avoid buffer underruns */ fn->min_iqs = 9000; if (ret >= 0 || ret == OV_HOLE) - return; + return 0; ret = -E_OGGDEC_BADLINK; out: - t->error = ret; if (ret < 0) btr_remove_node(&fn->btrn); + return ret; } /** diff --git a/oss_write.c b/oss_write.c index 57bdcfa6..cd327243 100644 --- a/oss_write.c +++ b/oss_write.c @@ -157,7 +157,7 @@ err_free: return ret; } -static void oss_post_select(__a_unused struct sched *s, +static int oss_post_select(__a_unused struct sched *s, struct task *t) { struct writer_node *wn = container_of(t, struct writer_node, task); @@ -181,7 +181,7 @@ static void oss_post_select(__a_unused struct sched *s, ret = oss_init(wn, rate, ch, format); if (ret < 0) goto out; - return; + return 0; } btr_merge(btrn, wn->min_iqs); bytes = btr_next_buffer(btrn, &data); @@ -199,9 +199,9 @@ static void oss_post_select(__a_unused struct sched *s, btr_consume(btrn, ret); ret = 0; out: - t->error = ret; if (ret < 0) btr_remove_node(&wn->btrn); + return ret; } __malloc static void *oss_parse_config_or_die(int argc, char **argv) diff --git a/osx_write.c b/osx_write.c index d38e2fb1..41b19030 100644 --- a/osx_write.c +++ b/osx_write.c @@ -301,7 +301,7 @@ static void osx_write_pre_select(struct sched *s, struct task *t) sched_request_timeout_ms(50, s); } -static void osx_write_post_select(__a_unused struct sched *s, struct task *t) +static int osx_write_post_select(__a_unused struct sched *s, struct task *t) { struct writer_node *wn = container_of(t, struct writer_node, task); struct private_osx_write_data *powd = wn->private_data; @@ -314,7 +314,7 @@ static void osx_write_post_select(__a_unused struct sched *s, struct task *t) if (!powd) { ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF); if (ret == 0) - return; + return 0; if (ret < 0) goto fail; ret = core_audio_init(wn); @@ -337,7 +337,7 @@ static void osx_write_post_select(__a_unused struct sched *s, struct task *t) ret = 0; mutex_unlock(powd->mutex); if (ret >= 0) - return; + return 0; fail: assert(ret < 0); if (powd && powd->callback_btrn) { @@ -348,7 +348,7 @@ fail: } btr_remove_node(&wn->btrn); PARA_NOTICE_LOG("%s\n", para_strerror(-ret)); - t->error = ret; + return ret; } /** diff --git a/para.h b/para.h index 4208ae6a..edab4871 100644 --- a/para.h +++ b/para.h @@ -109,6 +109,7 @@ void ms2tv(const long unsigned n, struct timeval *tv); void compute_chunk_time(long unsigned chunk_num, struct timeval *chunk_tv, struct timeval *stream_start, struct timeval *result); +struct timeval *clock_get_realtime(struct timeval *tv); /** The enum of all status items. */ enum status_items {STATUS_ITEM_ENUM NUM_STAT_ITEMS}; diff --git a/play.c b/play.c index cca203b5..2cb0536d 100644 --- a/play.c +++ b/play.c @@ -7,7 +7,6 @@ /** \file play.c Paraslash's standalone player. */ #include -#include #include #include @@ -1196,7 +1195,7 @@ static unsigned get_time_string(struct play_task *pt, char **result) ); } -static void play_post_select(struct sched *s, struct task *t) +static int play_post_select(struct sched *s, struct task *t) { struct play_task *pt = container_of(t, struct play_task, task); int ret; @@ -1204,7 +1203,7 @@ static void play_post_select(struct sched *s, struct task *t) ret = eof_cleanup(pt); if (ret < 0) { pt->rq = CRT_TERM_RQ; - return; + return 0; } ret = session_post_select(s, t); if (ret < 0) @@ -1234,7 +1233,7 @@ static void play_post_select(struct sched *s, struct task *t) } ret = 1; out: - t->error = ret; + return ret; } /** @@ -1255,7 +1254,7 @@ int main(int argc, char *argv[]) filter_init(); writer_init(); - gettimeofday(now, NULL); + clock_get_realtime(now); sched.default_timeout.tv_sec = 5; parse_config_or_die(argc, argv); diff --git a/prebuffer_filter.c b/prebuffer_filter.c index e1afff5a..0759d678 100644 --- a/prebuffer_filter.c +++ b/prebuffer_filter.c @@ -37,7 +37,6 @@ static void prebuffer_pre_select(struct sched *s, struct task *t) struct prebuffer_filter_args_info *conf = ppd->conf; struct timeval diff; - t->error = 0; if (iqs == 0) return; if (ppd->barrier.tv_sec == 0) { @@ -57,7 +56,7 @@ static void prebuffer_close(struct filter_node *fn) free(fn->private_data); } -static void prebuffer_post_select(__a_unused struct sched *s, struct task *t) +static int prebuffer_post_select(__a_unused struct sched *s, struct task *t) { struct filter_node *fn = container_of(t, struct filter_node, task); struct btr_node *btrn = fn->btrn; @@ -65,15 +64,14 @@ static void prebuffer_post_select(__a_unused struct sched *s, struct task *t) struct private_prebuffer_data *ppd = fn->private_data; struct prebuffer_filter_args_info *conf = ppd->conf; - t->error = 0; if (ppd->barrier.tv_sec == 0) - return; + return 0; if (tv_diff(now, &ppd->barrier, NULL) < 0) - return; + return 0; if (iqs < conf->size_arg) - return; - btr_splice_out_node(btrn); - t->error = -E_PREBUFFER_SUCCESS; + return 0; + btr_splice_out_node(fn->btrn); + return -E_PREBUFFER_SUCCESS; } static int prebuffer_parse_config(int argc, char **argv, void **config) diff --git a/recv.h b/recv.h index f70cbbe8..cc44a602 100644 --- a/recv.h +++ b/recv.h @@ -115,7 +115,7 @@ struct receiver { * * \sa select(2), struct receiver. */ - void (*post_select)(struct sched *s, struct task *t); + int (*post_select)(struct sched *s, struct task *t); /** The two help texts of this receiver. */ struct ggo_help help; diff --git a/resample_filter.c b/resample_filter.c index ed9945ae..bd8ece91 100644 --- a/resample_filter.c +++ b/resample_filter.c @@ -202,7 +202,7 @@ static int resample_frames(int16_t *in, size_t num_frames, bool have_more, return data.input_frames_used; } -static void resample_post_select(__a_unused struct sched *s, struct task *t) +static int resample_post_select(__a_unused struct sched *s, struct task *t) { int ret; struct filter_node *fn = container_of(t, struct filter_node, task); @@ -231,7 +231,8 @@ static void resample_post_select(__a_unused struct sched *s, struct task *t) * btr exec mechanism for the destination samplerate and the * channel count. */ - return btr_pushdown(btrn); + btr_pushdown(btrn); + return 0; } btr_merge(btrn, fn->min_iqs); in_bytes = btr_next_buffer(btrn, (char **)&in); @@ -246,14 +247,14 @@ static void resample_post_select(__a_unused struct sched *s, struct task *t) goto out; btr_consume(btrn, ret * 2 * ctx->channels); btr_add_output((char *)out, num_frames * 2 * ctx->channels, btrn); - return; + return 0; out: if (ret < 0) { - t->error = ret; btr_remove_node(&fn->btrn); /* This releases the check_wav btr node */ check_wav_post_select(ctx->cwc); } + return ret; } static int resample_parse_config(int argc, char **argv, void **config) diff --git a/sched.c b/sched.c index 7ee77027..d42e1498 100644 --- a/sched.c +++ b/sched.c @@ -8,7 +8,6 @@ #include #include -#include #include "para.h" #include "ipc.h" @@ -50,20 +49,12 @@ static inline bool timeout_is_zero(struct sched *s) static void sched_preselect(struct sched *s) { struct task *t, *tmp; + list_for_each_entry_safe(t, tmp, &s->pre_select_list, pre_select_node) { - if (t->error < 0) { - unregister_task(t); - continue; - } - if (t->notification != 0) { + if (t->notification != 0) sched_min_delay(s); - break; - } - if (!t->pre_select) - continue; - t->pre_select(s, t); - if (timeout_is_zero(s)) - break; + if (t->pre_select) + t->pre_select(s, t); } } @@ -71,14 +62,14 @@ static void sched_preselect(struct sched *s) static inline void call_post_select(struct sched *s, struct task *t) { #ifndef SCHED_DEBUG - t->post_select(s, t); + t->error = t->post_select(s, t); #else struct timeval t1, t2, diff; unsigned long pst; - gettimeofday(&t1, NULL); - t->post_select(s, t); - gettimeofday(&t2, NULL); + clock_get_realtime(&t1); + t->error = t->post_select(s, t); + clock_get_realtime(&t2); tv_diff(&t1, &t2, &diff); pst = tv2ms(&diff); if (pst > 50) @@ -128,30 +119,23 @@ again: FD_ZERO(&s->wfds); s->select_timeout = s->default_timeout; s->max_fileno = -1; - gettimeofday(now, NULL); + clock_get_realtime(now); sched_preselect(s); - if (list_empty(&s->pre_select_list) && list_empty(&s->post_select_list)) - return 0; - if (!timeout_is_zero(s)) { - ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds, - &s->select_timeout); - if (ret < 0) - return ret; - if (ret == 0) { - /* - * APUE: Be careful not to check the descriptor sets on return - * unless the return value is greater than zero. The return - * state of the descriptor sets is implementation dependent if - * either a signal is caught or the timer expires. - */ - FD_ZERO(&s->rfds); - FD_ZERO(&s->wfds); - } - gettimeofday(now, NULL); - } else { + ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds, + &s->select_timeout); + if (ret < 0) + return ret; + if (ret == 0) { + /* + * APUE: Be careful not to check the descriptor sets on return + * unless the return value is greater than zero. The return + * state of the descriptor sets is implementation dependent if + * either a signal is caught or the timer expires. + */ FD_ZERO(&s->rfds); FD_ZERO(&s->wfds); } + clock_get_realtime(now); sched_post_select(s); if (list_empty(&s->pre_select_list) && list_empty(&s->post_select_list)) return 0; diff --git a/sched.h b/sched.h index 7a5a4f86..9c5c098e 100644 --- a/sched.h +++ b/sched.h @@ -55,11 +55,13 @@ struct task { */ void (*pre_select)(struct sched *s, struct task *t); /** - * The postselect hook of \a t. + * The post select hook of \a t. * - * Evaluate and act upon the results of the previous select call. + * Its purpose is to evaluate and act upon the results of the previous + * select call. If this function returns a negative value, the + * scheduler unregisters the task. */ - void (*post_select)(struct sched *s, struct task *t); + int (*post_select)(struct sched *s, struct task *t); /** Whether this task is in error state. */ int error; /** Position of the task in the pre_select list of the scheduler. */ @@ -76,7 +78,7 @@ struct task { * This is set by the scheduler at the beginning of its main loop. It may be * used (read-only) from everywhere. As none of the functions called by the * scheduler are allowed to block, this value should be accurate enough so that - * there is no need to call gettimeofday() directly. + * there is no need to call clock_gettime() directly. */ extern struct timeval *now; diff --git a/server.c b/server.c index 2595d9c4..f3d5237f 100644 --- a/server.c +++ b/server.c @@ -67,7 +67,6 @@ */ #include -#include #include #include @@ -279,13 +278,13 @@ static void handle_sighup(void) kill(mmd->afs_pid, SIGHUP); } -static void signal_post_select(struct sched *s, __a_unused struct task *t) +static int signal_post_select(struct sched *s, __a_unused struct task *t) { int signum = para_next_signal(&s->rfds); switch (signum) { case 0: - return; + return 0; case SIGHUP: handle_sighup(); break; @@ -330,6 +329,7 @@ cleanup: shm_detach(mmd); exit(EXIT_FAILURE); } + return 0; } static void init_signal_task(void) @@ -358,7 +358,7 @@ static void command_pre_select(struct sched *s, struct task *t) para_fd_set(sct->listen_fd, &s->rfds, &s->max_fileno); } -static void command_post_select(struct sched *s, struct task *t) +static int command_post_select(struct sched *s, struct task *t) { struct server_command_task *sct = container_of(t, struct server_command_task, task); @@ -392,7 +392,7 @@ static void command_post_select(struct sched *s, struct task *t) if (child_pid) { close(new_fd); /* parent keeps accepting connections */ - return; + return 0; } /* mmd might already have changed at this point */ free(chunk_table); @@ -406,10 +406,12 @@ static void command_post_select(struct sched *s, struct task *t) for (i = sct->argc - 1; i >= 0; i--) memset(sct->argv[i], 0, strlen(sct->argv[i])); sprintf(sct->argv[0], "para_server (serving %s)", peer_name); - return handle_connect(new_fd, peer_name); + handle_connect(new_fd, peer_name); + /* never reached*/ out: if (ret < 0) PARA_CRIT_LOG("%s\n", para_strerror(-ret)); + return 0; } static void init_server_command_task(int argc, char **argv) @@ -493,7 +495,7 @@ static void server_init(int argc, char **argv) log_welcome("para_server"); init_ipc_or_die(); /* init mmd struct and mmd->lock */ /* make sure, the global now pointer is uptodate */ - gettimeofday(now, NULL); + clock_get_realtime(now); set_server_start_time(now); init_user_list(user_list_file); /* become daemon */ diff --git a/spxdec_filter.c b/spxdec_filter.c index 6bd41146..9bba042c 100644 --- a/spxdec_filter.c +++ b/spxdec_filter.c @@ -238,7 +238,7 @@ static int compute_skip_samples(ogg_page *og, struct private_spxdec_data *psd) return ret; } -static void speexdec_post_select(__a_unused struct sched *s, struct task *t) +static int speexdec_post_select(__a_unused struct sched *s, struct task *t) { struct filter_node *fn = container_of(t, struct filter_node, task); struct private_spxdec_data *psd = fn->private_data; @@ -249,7 +249,6 @@ static void speexdec_post_select(__a_unused struct sched *s, struct task *t) size_t nbytes; next_buffer: - t->error = 0; ret = ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL); btr_merge(btrn, fn->min_iqs); if (!psd->shi.state) { @@ -290,10 +289,9 @@ next_buffer: goto next_buffer; ret = ns; fail: - if (ret < 0) { - t->error = ret; + if (ret < 0) btr_remove_node(&fn->btrn); - } + return ret; } /** diff --git a/stdin.c b/stdin.c index fd803ae5..b25a0ba0 100644 --- a/stdin.c +++ b/stdin.c @@ -33,7 +33,6 @@ static void stdin_pre_select(struct sched *s, struct task *t) struct stdin_task *sit = container_of(t, struct stdin_task, task); int ret; - t->error = 0; ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT); if (ret < 0) sched_min_delay(s); @@ -55,22 +54,21 @@ static void stdin_pre_select(struct sched *s, struct task *t) * appears to be readable, data is read from stdin and fed into the buffer * tree. */ -static void stdin_post_select(struct sched *s, struct task *t) +static int stdin_post_select(struct sched *s, struct task *t) { struct stdin_task *sit = container_of(t, struct stdin_task, task); ssize_t ret; size_t sz, n; char *buf = NULL; - t->error = 0; ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT); if (ret < 0) goto err; if (ret == 0) - return; + return 0; sz = btr_pool_get_buffer(sit->btrp, &buf); if (sz == 0) - return; + return 0; /* * Do not use the maximal size to avoid having only a single buffer * reference for the whole pool. This is bad because if that single @@ -81,11 +79,11 @@ static void stdin_post_select(struct sched *s, struct task *t) if (n > 0) btr_add_output_pool(sit->btrp, n, sit->btrn); if (ret >= 0) - return; + return 0; err: btr_remove_node(&sit->btrn); //btr_pool_free(sit->btrp); - t->error = ret; + return ret; } /** diff --git a/stdout.c b/stdout.c index 9c7e64e7..abf3d06f 100644 --- a/stdout.c +++ b/stdout.c @@ -30,7 +30,6 @@ static void stdout_pre_select(struct sched *s, struct task *t) struct stdout_task *sot = container_of(t, struct stdout_task, task); int ret; - t->error = 0; ret = btr_node_status(sot->btrn, 0, BTR_NT_LEAF); if (ret > 0) para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno); @@ -47,7 +46,7 @@ static void stdout_pre_select(struct sched *s, struct task *t) * This function writes input data from the buffer tree to stdout if \p * STDOUT_FILENO is writable. */ -static void stdout_post_select(struct sched *s, struct task *t) +static int stdout_post_select(struct sched *s, struct task *t) { struct stdout_task *sot = container_of(t, struct stdout_task, task); struct btr_node *btrn = sot->btrn; @@ -55,14 +54,13 @@ static void stdout_post_select(struct sched *s, struct task *t) char *buf; size_t sz; - t->error = 0; ret = btr_node_status(btrn, 0, BTR_NT_LEAF); if (ret < 0) goto out; if (ret == 0) - return; + return 0; if (!FD_ISSET(STDOUT_FILENO, &s->wfds)) - return; + return 0; for (;;) { sz = btr_next_buffer(btrn, &buf); @@ -76,7 +74,7 @@ static void stdout_post_select(struct sched *s, struct task *t) out: if (ret < 0) btr_remove_node(&sot->btrn); - t->error = ret; + return ret; } /** * Initialize a stdout task structure with default values. diff --git a/string.c b/string.c index a416dc80..dfcfa2cd 100644 --- a/string.c +++ b/string.c @@ -8,7 +8,6 @@ #define _GNU_SOURCE -#include /* gettimeofday */ #include #include /* uname() */ diff --git a/time.c b/time.c index 18b5a35f..6f6dd49e 100644 --- a/time.c +++ b/time.c @@ -191,3 +191,40 @@ void compute_chunk_time(long unsigned chunk_num, tv_scale(chunk_num, chunk_tv, &tmp); tv_add(&tmp, stream_start, result); } + +/** + * Retrieve the time of the realtime clock. + * + * \param tv Where to store the result. + * + * Gets the current value of the system-wide real-time clock (identified by id + * \p CLOCK_REALTIME). If \a tv is \p NULL, the value is stored in a static + * buffer, otherwise it is stored at the location given by \a tv. + * + * \return This function aborts on errors. On success it returns a pointer to + * memory containing the current time. + * + * \sa clock_gettime(2), gettimeofday(2). + */ +struct timeval *clock_get_realtime(struct timeval *tv) +{ + static struct timeval user_friendly; + + if (!tv) + tv = &user_friendly; +#ifdef HAVE_CLOCK_GETTIME + { + struct timespec t; + int ret; + + ret = clock_gettime(CLOCK_REALTIME, &t); + assert(ret == 0); + tv->tv_sec = t.tv_sec; + tv->tv_usec = t.tv_nsec / 1000; + } +#else + #include + gettimeofday(tv, NULL); +#endif /* HAVE_CLOCK_GETTIME */ + return tv; +} diff --git a/udp_recv.c b/udp_recv.c index 6c5ee026..f9c70ec8 100644 --- a/udp_recv.c +++ b/udp_recv.c @@ -49,7 +49,7 @@ static int udp_check_eof(size_t sz, struct iovec iov[2]) return -E_RECV_EOF; } -static void udp_recv_post_select(__a_unused struct sched *s, struct task *t) +static int udp_recv_post_select(__a_unused struct sched *s, struct task *t) { struct receiver_node *rn = container_of(t, struct receiver_node, task); struct btr_node *btrn = rn->btrn; @@ -83,12 +83,12 @@ static void udp_recv_post_select(__a_unused struct sched *s, struct task *t) } ret = readv_ret; out: - if (ret >= 0) - return; - btr_remove_node(&rn->btrn); - t->error = ret; - close(rn->fd); - rn->fd = -1; + if (ret < 0) { + btr_remove_node(&rn->btrn); + close(rn->fd); + rn->fd = -1; + } + return ret; } static void udp_recv_close(struct receiver_node *rn) diff --git a/udp_send.c b/udp_send.c index b41c0ebf..7930f092 100644 --- a/udp_send.c +++ b/udp_send.c @@ -8,7 +8,7 @@ #include -#include +#include #include #include #include diff --git a/vss.c b/vss.c index cfa64a14..aec357ab 100644 --- a/vss.c +++ b/vss.c @@ -1114,12 +1114,11 @@ static void vss_send(struct vss_task *vsst) } } -static void vss_post_select(struct sched *s, struct task *t) +static int vss_post_select(struct sched *s, struct task *t) { int ret, i; struct vss_task *vsst = container_of(t, struct vss_task, task); - if (mmd->sender_cmd_data.cmd_num >= 0) { int num = mmd->sender_cmd_data.cmd_num, sender_num = mmd->sender_cmd_data.sender_num; @@ -1151,6 +1150,7 @@ static void vss_post_select(struct sched *s, struct task *t) (vss_next() && vss_playing())) tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier); vss_send(vsst); + return 0; } /** diff --git a/wav_filter.c b/wav_filter.c index 716d8dd9..83b81fb2 100644 --- a/wav_filter.c +++ b/wav_filter.c @@ -74,7 +74,7 @@ static void wav_pre_select(struct sched *s, struct task *t) sched_min_delay(s); } -static void wav_post_select(__a_unused struct sched *s, struct task *t) +static int wav_post_select(__a_unused struct sched *s, struct task *t) { struct filter_node *fn = container_of(t, struct filter_node, task); struct btr_node *btrn = fn->btrn; @@ -83,12 +83,11 @@ static void wav_post_select(__a_unused struct sched *s, struct task *t) char *header, *buf; int32_t rate, ch; - t->error = 0; if (iqs == 0) { ret = -E_WAV_EOF; if (btr_no_parent(btrn)) goto err; - return; + return 0; } ret = btr_exec_up(btrn, "sample_rate", &buf); if (ret < 0) { @@ -113,13 +112,13 @@ static void wav_post_select(__a_unused struct sched *s, struct task *t) btr_add_output(header, WAV_HEADER_LEN, btrn); ret = -E_WAV_SUCCESS; err: - t->error = ret; if (ret == -E_WAV_SUCCESS) btr_splice_out_node(btrn); else { btr_remove_node(&fn->btrn); PARA_ERROR_LOG("%s\n", para_strerror(-ret)); } + return ret; } /** diff --git a/wmadec_filter.c b/wmadec_filter.c index 20f9df44..8b751f04 100644 --- a/wmadec_filter.c +++ b/wmadec_filter.c @@ -17,7 +17,6 @@ #define _XOPEN_SOURCE 600 -#include #include #include #include @@ -1211,7 +1210,7 @@ static int wmadec_execute(struct btr_node *btrn, const char *cmd, char **result) #define WMA_OUTPUT_BUFFER_SIZE (128 * 1024) -static void wmadec_post_select(__a_unused struct sched *s, struct task *t) +static int wmadec_post_select(__a_unused struct sched *s, struct task *t) { struct filter_node *fn = container_of(t, struct filter_node, task); int ret, converted, out_size; @@ -1222,12 +1221,11 @@ static void wmadec_post_select(__a_unused struct sched *s, struct task *t) next_buffer: converted = 0; - t->error = 0; ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL); if (ret < 0) goto err; if (ret == 0) - return; + return 0; btr_merge(btrn, fn->min_iqs); len = btr_next_buffer(btrn, (char **)&in); ret = -E_WMADEC_EOF; @@ -1263,11 +1261,11 @@ next_buffer: converted += ret + WMA_FRAME_SKIP; success: btr_consume(btrn, converted); - return; + return 0; err: assert(ret < 0); - t->error = ret; btr_remove_node(&fn->btrn); + return ret; } static void wmadec_open(struct filter_node *fn) diff --git a/write.c b/write.c index 3deef147..866ea433 100644 --- a/write.c +++ b/write.c @@ -86,10 +86,10 @@ static void write_pre_select(struct sched *s, struct task *t) check_wav_pre_select(s, wt->cwc); } -static void write_post_select(__a_unused struct sched *s, struct task *t) +static int write_post_select(__a_unused struct sched *s, struct task *t) { struct write_task *wt = container_of(t, struct write_task, task); - t->error = check_wav_post_select(wt->cwc); + return check_wav_post_select(wt->cwc); } static int setup_and_schedule(void) diff --git a/write.h b/write.h index 2573ba5c..82921608 100644 --- a/write.h +++ b/write.h @@ -65,7 +65,7 @@ struct writer { * * Called from the post_select function of the writer node's task. */ - void (*post_select)(struct sched *s, struct task *t); + int (*post_select)(struct sched *s, struct task *t); /** * Close one instance of the writer. * diff --git a/write_common.c b/write_common.c index a4f908ac..44ccf88a 100644 --- a/write_common.c +++ b/write_common.c @@ -113,8 +113,8 @@ void register_writer_node(struct writer_node *wn, struct btr_node *parent, .handler = w->execute, .context = wn)); strcpy(wn->task.status, name); free(name); - wn->task.post_select = w->post_select; wn->task.pre_select = w->pre_select; + wn->task.post_select = w->post_select; register_task(s, &wn->task); }