From: Lennart Poettering Date: Fri, 14 Jul 2006 22:42:01 +0000 (+0000) Subject: try to use send(,,MSG_NOSIGNAL) instead of write() wherever possible (which X-Git-Tag: submit/2.0-panda/20130828.192557~3515 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=860be2e70b33ff5eeb9130f80c4b1c096a2a8f27;p=profile%2Fivi%2Fpulseaudio-panda.git try to use send(,,MSG_NOSIGNAL) instead of write() wherever possible (which will allow us to drop the SIGPIPE check). Cache the results of the last write()/send() to make sure that we do not issue more than necessary system calls. git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1083 fefdeb5f-60dc-0310-8127-8f9354f1896f --- diff --git a/src/daemon/cpulimit.c b/src/daemon/cpulimit.c index a8c9d3f..b8740ea 100644 --- a/src/daemon/cpulimit.c +++ b/src/daemon/cpulimit.c @@ -111,7 +111,7 @@ static void reset_cpu_time(int t) { /* A simple, thread-safe puts() work-alike */ static void write_err(const char *p) { - pa_loop_write(2, p, strlen(p)); + pa_loop_write(2, p, strlen(p), NULL); } /* The signal handler, called on every SIGXCPU */ @@ -159,7 +159,7 @@ static void signal_handler(int sig) { static void callback(pa_mainloop_api*m, pa_io_event*e, int fd, pa_io_event_flags_t f, void *userdata) { char c; assert(m && e && f == PA_IO_EVENT_INPUT && e == io_event && fd == the_pipe[0]); - read(the_pipe[0], &c, sizeof(c)); + pa_read(the_pipe[0], &c, sizeof(c), NULL); m->quit(m, 1); /* Quit the main loop */ } diff --git a/src/daemon/main.c b/src/daemon/main.c index 5e7330a..c41c69d 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -304,7 +304,7 @@ int main(int argc, char *argv[]) { close(daemon_pipe[1]); daemon_pipe[1] = -1; - if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval)) != sizeof(retval)) { + if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval), NULL) != sizeof(retval)) { pa_log(__FILE__": read() failed: %s", pa_cstrerror(errno)); retval = 1; } @@ -368,7 +368,7 @@ int main(int argc, char *argv[]) { pa_log(__FILE__": pa_pid_file_create() failed."); #ifdef HAVE_FORK if (conf->daemonize) - pa_loop_write(daemon_pipe[1], &retval, sizeof(retval)); + pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL); #endif goto finish; } @@ -428,20 +428,20 @@ int main(int argc, char *argv[]) { pa_log(__FILE__": failed to initialize daemon."); #ifdef HAVE_FORK if (conf->daemonize) - pa_loop_write(daemon_pipe[1], &retval, sizeof(retval)); + pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL); #endif } else if (!c->modules || pa_idxset_size(c->modules) == 0) { pa_log(__FILE__": daemon startup without any loaded modules, refusing to work."); #ifdef HAVE_FORK if (conf->daemonize) - pa_loop_write(daemon_pipe[1], &retval, sizeof(retval)); + pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL); #endif } else { retval = 0; #ifdef HAVE_FORK if (conf->daemonize) - pa_loop_write(daemon_pipe[1], &retval, sizeof(retval)); + pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL); #endif c->disallow_module_loading = conf->disallow_module_loading; diff --git a/src/modules/module-esound-compat-spawnfd.c b/src/modules/module-esound-compat-spawnfd.c index c4bc342..54e090e 100644 --- a/src/modules/module-esound-compat-spawnfd.c +++ b/src/modules/module-esound-compat-spawnfd.c @@ -59,7 +59,7 @@ int pa__init(pa_core *c, pa_module*m) { goto finish; } - if (pa_loop_write(fd, &x, sizeof(x)) != sizeof(x)) + if (pa_loop_write(fd, &x, sizeof(x), NULL) != sizeof(x)) pa_log(__FILE__": WARNING: write(%u, 1, 1) failed: %s", fd, pa_cstrerror(errno)); close(fd); diff --git a/src/modules/module-jack-sink.c b/src/modules/module-jack-sink.c index 74f25d5..d761c1f 100644 --- a/src/modules/module-jack-sink.c +++ b/src/modules/module-jack-sink.c @@ -80,6 +80,7 @@ struct userdata { jack_nframes_t frames_requested; int quit_requested; + int pipe_fd_type; int pipe_fds[2]; pa_io_event *io_event; @@ -120,7 +121,7 @@ static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_ assert(u); assert(u->pipe_fds[0] == fd); - read(fd, &x, 1); + pa_read(fd, &x, 1, &u->pipe_fd_type); if (u->quit_requested) { stop_sink(u); @@ -165,7 +166,7 @@ static void request_render(struct userdata *u) { assert(u); assert(u->pipe_fds[1] >= 0); - write(u->pipe_fds[1], &c, 1); + pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type); } static void jack_shutdown(void *arg) { @@ -268,6 +269,7 @@ int pa__init(pa_core *c, pa_module*m) { u->core = c; u->module = m; u->pipe_fds[0] = u->pipe_fds[1] = -1; + u->pipe_fd_type = 0; pthread_mutex_init(&u->mutex, NULL); pthread_cond_init(&u->cond, NULL); diff --git a/src/modules/module-jack-source.c b/src/modules/module-jack-source.c index 9c7f449..649a8f9 100644 --- a/src/modules/module-jack-source.c +++ b/src/modules/module-jack-source.c @@ -81,6 +81,7 @@ struct userdata { int quit_requested; int pipe_fds[2]; + int pipe_fd_type; pa_io_event *io_event; jack_nframes_t frames_in_buffer; @@ -119,7 +120,7 @@ static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_ assert(u); assert(u->pipe_fds[0] == fd); - read(fd, &x, 1); + pa_read(fd, &x, 1, &u->pipe_fd_type); if (u->quit_requested) { stop_source(u); @@ -166,7 +167,7 @@ static void request_post(struct userdata *u) { assert(u); assert(u->pipe_fds[1] >= 0); - write(u->pipe_fds[1], &c, 1); + pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type); } static void jack_shutdown(void *arg) { @@ -266,6 +267,7 @@ int pa__init(pa_core *c, pa_module*m) { u->core = c; u->module = m; u->pipe_fds[0] = u->pipe_fds[1] = -1; + u->pipe_fd_type = 0; pthread_mutex_init(&u->mutex, NULL); pthread_cond_init(&u->cond, NULL); diff --git a/src/modules/module-mmkbd-evdev.c b/src/modules/module-mmkbd-evdev.c index ddbf16d2..c3d0739 100644 --- a/src/modules/module-mmkbd-evdev.c +++ b/src/modules/module-mmkbd-evdev.c @@ -70,7 +70,7 @@ static const char* const valid_modargs[] = { }; struct userdata { - int fd; + int fd, fd_type; pa_io_event *io; char *sink_name; pa_module *module; @@ -89,7 +89,7 @@ static void io_callback(pa_mainloop_api *io, PA_GCC_UNUSED pa_io_event *e, PA_GC if (events & PA_IO_EVENT_INPUT) { struct input_event ev; - if (pa_loop_read(u->fd, &ev, sizeof(ev)) <= 0) { + if (pa_loop_read(u->fd, &ev, sizeof(ev), &u->fd_type) <= 0) { pa_log(__FILE__": failed to read from event device: %s", pa_cstrerror(errno)); goto fail; } @@ -182,6 +182,7 @@ int pa__init(pa_core *c, pa_module*m) { u->io = NULL; u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL)); u->fd = -1; + u->fd_type = 0; if ((u->fd = open(pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), O_RDONLY)) < 0) { pa_log(__FILE__": failed to open evdev device: %s", pa_cstrerror(errno)); diff --git a/src/modules/module-oss.c b/src/modules/module-oss.c index 5a80661..a3ea2c1 100644 --- a/src/modules/module-oss.c +++ b/src/modules/module-oss.c @@ -467,7 +467,7 @@ int pa__init(pa_core *c, pa_module*m) { */ if (u->source) { char *buf = pa_xnew(char, u->sample_size); - read(u->fd, buf, u->sample_size); + pa_read(u->fd, buf, u->sample_size, NULL); pa_xfree(buf); } diff --git a/src/pulse/mainloop-signal.c b/src/pulse/mainloop-signal.c index c3cf362..11a27cd 100644 --- a/src/pulse/mainloop-signal.c +++ b/src/pulse/mainloop-signal.c @@ -67,7 +67,7 @@ static void signal_handler(int sig) { #ifndef HAVE_SIGACTION signal(sig, signal_handler); #endif - pa_write(signal_pipe[1], &sig, sizeof(sig)); + pa_write(signal_pipe[1], &sig, sizeof(sig), NULL); } static void dispatch(pa_mainloop_api*a, int sig) { @@ -86,7 +86,7 @@ static void callback(pa_mainloop_api*a, pa_io_event*e, int fd, pa_io_event_flags int sig; assert(a && e && f == PA_IO_EVENT_INPUT && e == io_event && fd == signal_pipe[0]); - if ((r = pa_read(signal_pipe[0], &sig, sizeof(sig))) < 0) { + if ((r = pa_read(signal_pipe[0], &sig, sizeof(sig), NULL)) < 0) { if (errno == EAGAIN) return; diff --git a/src/pulse/mainloop.c b/src/pulse/mainloop.c index 32f1a84..dfbc337 100644 --- a/src/pulse/mainloop.c +++ b/src/pulse/mainloop.c @@ -100,6 +100,7 @@ struct pa_mainloop { int deferred_pending; int wakeup_pipe[2]; + int wakeup_pipe_type; enum { STATE_PASSIVE, @@ -337,6 +338,7 @@ pa_mainloop *pa_mainloop_new(void) { m = pa_xmalloc(sizeof(pa_mainloop)); + m->wakeup_pipe_type = 0; if (pipe(m->wakeup_pipe) < 0) { pa_log_error(__FILE__": ERROR: cannot create wakeup pipe"); pa_xfree(m); @@ -625,7 +627,7 @@ void pa_mainloop_wakeup(pa_mainloop *m) { assert(m); if (m->wakeup_pipe[1] >= 0) - pa_write(m->wakeup_pipe[1], &c, sizeof(c)); + pa_write(m->wakeup_pipe[1], &c, sizeof(c), &m->wakeup_pipe_type); } static void clear_wakeup(pa_mainloop *m) { @@ -636,7 +638,7 @@ static void clear_wakeup(pa_mainloop *m) { if (m->wakeup_pipe[0] < 0) return; - while (pa_read(m->wakeup_pipe[0], &c, sizeof(c)) == sizeof(c)); + while (pa_read(m->wakeup_pipe[0], &c, sizeof(c), &m->wakeup_pipe_type) == sizeof(c)); } int pa_mainloop_prepare(pa_mainloop *m, int timeout) { diff --git a/src/pulse/xmalloc.c b/src/pulse/xmalloc.c index 46871ae..3675516 100644 --- a/src/pulse/xmalloc.c +++ b/src/pulse/xmalloc.c @@ -49,7 +49,7 @@ static void oom(void) PA_GCC_NORETURN; * exits */ static void oom(void) { static const char e[] = "Not enough memory\n"; - pa_loop_write(STDERR_FILENO, e, sizeof(e)-1); + pa_loop_write(STDERR_FILENO, e, sizeof(e)-1, NULL); #ifdef SIGQUIT raise(SIGQUIT); #endif diff --git a/src/pulsecore/authkey.c b/src/pulsecore/authkey.c index 064209d..a5df3ed 100644 --- a/src/pulsecore/authkey.c +++ b/src/pulsecore/authkey.c @@ -53,7 +53,7 @@ static int generate(int fd, void *ret_data, size_t length) { lseek(fd, 0, SEEK_SET); ftruncate(fd, 0); - if ((r = pa_loop_write(fd, ret_data, length)) < 0 || (size_t) r != length) { + if ((r = pa_loop_write(fd, ret_data, length, NULL)) < 0 || (size_t) r != length) { pa_log(__FILE__": failed to write cookie file: %s", pa_cstrerror(errno)); return -1; } @@ -84,7 +84,7 @@ static int load(const char *fn, void *data, size_t length) { unlock = pa_lock_fd(fd, 1) >= 0; - if ((r = pa_loop_read(fd, data, length)) < 0) { + if ((r = pa_loop_read(fd, data, length, NULL)) < 0) { pa_log(__FILE__": failed to read cookie file '%s': %s", fn, pa_cstrerror(errno)); goto finish; } @@ -188,7 +188,7 @@ int pa_authkey_save(const char *fn, const void *data, size_t length) { unlock = pa_lock_fd(fd, 1) >= 0; - if ((r = pa_loop_write(fd, data, length)) < 0 || (size_t) r != length) { + if ((r = pa_loop_write(fd, data, length, NULL)) < 0 || (size_t) r != length) { pa_log(__FILE__": failed to read cookie file '%s': %s", fn, pa_cstrerror(errno)); goto finish; } diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index 9d694da..16c3631 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -201,57 +201,81 @@ finish: return ret; } -/** Platform independent read function. Necessary since not all systems - * treat all file descriptors equal. */ -ssize_t pa_read(int fd, void *buf, size_t count) { - ssize_t r; +/** Platform independent read function. Necessary since not all + * systems treat all file descriptors equal. If type is + * non-NULL it is used to cache the type of the fd. This is + * useful for making sure that only a single syscall is executed per + * function call. The variable pointed to should be initialized to 0 + * by the caller. */ +ssize_t pa_read(int fd, void *buf, size_t count, int *type) { #ifdef OS_IS_WIN32 - r = recv(fd, buf, count, 0); - if (r < 0) { + + if (!type || *type == 0) { + ssize_t r; + + if ((r = recv(fd, buf, count, 0)) >= 0) + return r; + if (WSAGetLastError() != WSAENOTSOCK) { errno = WSAGetLastError(); return r; } + + if (type) + *type = 1; } - if (r < 0) #endif - r = read(fd, buf, count); - - return r; + + return read(fd, buf, count); } /** Similar to pa_read(), but handles writes */ -ssize_t pa_write(int fd, const void *buf, size_t count) { - ssize_t r; +ssize_t pa_write(int fd, const void *buf, size_t count, int *type) { + + if (!type || *type == 0) { + ssize_t r; + if ((r = send(fd, buf, count, MSG_NOSIGNAL)) >= 0) + return r; + #ifdef OS_IS_WIN32 - r = send(fd, buf, count, 0); - if (r < 0) { if (WSAGetLastError() != WSAENOTSOCK) { errno = WSAGetLastError(); return r; } - } - - if (r < 0) +#else + if (errno != ENOTSOCK) + return r; #endif - r = write(fd, buf, count); - return r; + if (type) + *type = 1; + } + + return write(fd, buf, count); } /** Calls read() in a loop. Makes sure that as much as 'size' bytes, * unless EOF is reached or an error occured */ -ssize_t pa_loop_read(int fd, void*data, size_t size) { +ssize_t pa_loop_read(int fd, void*data, size_t size, int *type) { ssize_t ret = 0; - assert(fd >= 0 && data && size); + int _type; + + assert(fd >= 0); + assert(data); + assert(size); + + if (!type) { + _type = 0; + type = &_type; + } while (size > 0) { ssize_t r; - if ((r = pa_read(fd, data, size)) < 0) + if ((r = pa_read(fd, data, size, type)) < 0) return r; if (r == 0) @@ -266,14 +290,23 @@ ssize_t pa_loop_read(int fd, void*data, size_t size) { } /** Similar to pa_loop_read(), but wraps write() */ -ssize_t pa_loop_write(int fd, const void*data, size_t size) { +ssize_t pa_loop_write(int fd, const void*data, size_t size, int *type) { ssize_t ret = 0; - assert(fd >= 0 && data && size); + int _type; + + assert(fd >= 0); + assert(data); + assert(size); + + if (!type) { + _type = 0; + type = &_type; + } while (size > 0) { ssize_t r; - if ((r = pa_write(fd, data, size)) < 0) + if ((r = pa_write(fd, data, size, type)) < 0) return r; if (r == 0) diff --git a/src/pulsecore/core-util.h b/src/pulsecore/core-util.h index 0012afc..864a96e 100644 --- a/src/pulsecore/core-util.h +++ b/src/pulsecore/core-util.h @@ -36,10 +36,10 @@ void pa_make_nonblock_fd(int fd); int pa_make_secure_dir(const char* dir); int pa_make_secure_parent_dir(const char *fn); -ssize_t pa_read(int fd, void *buf, size_t count); -ssize_t pa_write(int fd, const void *buf, size_t count); -ssize_t pa_loop_read(int fd, void*data, size_t size); -ssize_t pa_loop_write(int fd, const void*data, size_t size); +ssize_t pa_read(int fd, void *buf, size_t count, int *type); +ssize_t pa_write(int fd, const void *buf, size_t count, int *type); +ssize_t pa_loop_read(int fd, void*data, size_t size, int *type); +ssize_t pa_loop_write(int fd, const void*data, size_t size, int *type); void pa_check_signal_is_blocked(int sig); diff --git a/src/pulsecore/iochannel.c b/src/pulsecore/iochannel.c index 8a19094..842c0e6 100644 --- a/src/pulsecore/iochannel.c +++ b/src/pulsecore/iochannel.c @@ -49,6 +49,7 @@ struct pa_iochannel { int ifd, ofd; + int ifd_type, ofd_type; pa_mainloop_api* mainloop; pa_iochannel_cb_t callback; @@ -127,6 +128,7 @@ pa_iochannel* pa_iochannel_new(pa_mainloop_api*m, int ifd, int ofd) { io = pa_xnew(pa_iochannel, 1); io->ifd = ifd; io->ofd = ofd; + io->ifd_type = io->ofd_type = 0; io->mainloop = m; io->userdata = NULL; @@ -204,7 +206,7 @@ ssize_t pa_iochannel_write(pa_iochannel*io, const void*data, size_t l) { assert(l); assert(io->ofd >= 0); - r = pa_write(io->ofd, data, l); + r = pa_write(io->ofd, data, l, &io->ofd_type); if (r >= 0) { io->writable = 0; enable_mainloop_sources(io); @@ -220,7 +222,7 @@ ssize_t pa_iochannel_read(pa_iochannel*io, void*data, size_t l) { assert(data); assert(io->ifd >= 0); - r = pa_read(io->ifd, data, l); + r = pa_read(io->ifd, data, l, &io->ifd_type); if (r >= 0) { io->readable = 0; enable_mainloop_sources(io); diff --git a/src/pulsecore/pid.c b/src/pulsecore/pid.c index b897286..0ad76a6 100644 --- a/src/pulsecore/pid.c +++ b/src/pulsecore/pid.c @@ -56,7 +56,7 @@ static pid_t read_pid(const char *fn, int fd) { assert(fn && fd >= 0); - if ((r = pa_loop_read(fd, t, sizeof(t)-1)) < 0) { + if ((r = pa_loop_read(fd, t, sizeof(t)-1, NULL)) < 0) { pa_log_warn(__FILE__": WARNING: failed to read PID file '%s': %s", fn, pa_cstrerror(errno)); return (pid_t) -1; @@ -177,7 +177,7 @@ int pa_pid_file_create(void) { snprintf(t, sizeof(t), "%lu\n", (unsigned long) getpid()); l = strlen(t); - if (pa_loop_write(fd, t, l) != (ssize_t) l) { + if (pa_loop_write(fd, t, l, NULL) != (ssize_t) l) { pa_log(__FILE__": failed to write PID file."); goto fail; } diff --git a/src/pulsecore/random.c b/src/pulsecore/random.c index 3d3357a..684ead7 100644 --- a/src/pulsecore/random.c +++ b/src/pulsecore/random.c @@ -61,7 +61,7 @@ static int random_proper(void *ret_data, size_t length) { if ((fd = open(*device, O_RDONLY)) >= 0) { - if ((r = pa_loop_read(fd, ret_data, length)) < 0 || (size_t) r != length) + if ((r = pa_loop_read(fd, ret_data, length, NULL)) < 0 || (size_t) r != length) ret = -1; close(fd); diff --git a/src/tests/mcalign-test.c b/src/tests/mcalign-test.c index ccb0613..7a68e6d 100644 --- a/src/tests/mcalign-test.c +++ b/src/tests/mcalign-test.c @@ -82,7 +82,7 @@ int main(PA_GCC_UNUSED int argc, PA_GCC_UNUSED char *argv[]) { if (pa_mcalign_pop(a, &t) < 0) break; - pa_loop_write(STDOUT_FILENO, (uint8_t*) t.memblock->data + t.index, t.length); + pa_loop_write(STDOUT_FILENO, (uint8_t*) t.memblock->data + t.index, t.length, NULL); fprintf(stderr, "Wrote %lu bytes.\n", (unsigned long) t.length); pa_memblock_unref(t.memblock);