#endif
#include <stdio.h>
-#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
-#include <string.h>
#include <fcntl.h>
#include <errno.h>
-#ifdef HAVE_POLL_H
-#include <poll.h>
-#else
-#include <pulsecore/poll.h>
-#endif
-
#ifndef HAVE_PIPE
#include <pulsecore/pipe.h>
#endif
-#include <pulse/i18n.h>
#include <pulse/rtclock.h>
#include <pulse/timeval.h>
#include <pulse/xmalloc.h>
+#include <pulsecore/poll.h>
#include <pulsecore/core-rtclock.h>
#include <pulsecore/core-util.h>
+#include <pulsecore/i18n.h>
#include <pulsecore/llist.h>
#include <pulsecore/log.h>
#include <pulsecore/core-error.h>
-#include <pulsecore/winsock.h>
+#include <pulsecore/socket.h>
#include <pulsecore/macro.h>
#include "mainloop.h"
struct pa_io_event {
pa_mainloop *mainloop;
- pa_bool_t dead:1;
+ bool dead:1;
int fd;
pa_io_event_flags_t events;
struct pa_time_event {
pa_mainloop *mainloop;
- pa_bool_t dead:1;
+ bool dead:1;
- pa_bool_t enabled:1;
- pa_bool_t use_rtclock:1;
+ bool enabled:1;
+ bool use_rtclock:1;
pa_usec_t time;
pa_time_event_cb_t callback;
struct pa_defer_event {
pa_mainloop *mainloop;
- pa_bool_t dead:1;
+ bool dead:1;
- pa_bool_t enabled:1;
+ bool enabled:1;
pa_defer_event_cb_t callback;
void *userdata;
unsigned n_enabled_defer_events, n_enabled_time_events, n_io_events;
unsigned io_events_please_scan, time_events_please_scan, defer_events_please_scan;
- pa_bool_t rebuild_pollfds:1;
+ bool rebuild_pollfds:1;
struct pollfd *pollfds;
unsigned max_pollfds, n_pollfds;
pa_mainloop_api api;
int retval;
- pa_bool_t quit:1;
+ bool quit:1;
- pa_bool_t wakeup_requested:1;
+ pa_atomic_t wakeup_requested;
int wakeup_pipe[2];
int wakeup_pipe_type;
/* IO events */
static pa_io_event* mainloop_io_new(
- pa_mainloop_api*a,
+ pa_mainloop_api *a,
int fd,
pa_io_event_flags_t events,
pa_io_event_cb_t callback,
e->callback = callback;
e->userdata = userdata;
-#ifdef OS_IS_WIN32
- {
- fd_set xset;
- struct timeval tv;
-
- tv.tv_sec = 0;
- tv.tv_usec = 0;
-
- FD_ZERO (&xset);
- FD_SET (fd, &xset);
-
- if ((select((SELECT_TYPE_ARG1) fd, NULL, NULL, SELECT_TYPE_ARG234 &xset,
- SELECT_TYPE_ARG5 &tv) == -1) &&
- (WSAGetLastError() == WSAENOTSOCK)) {
- pa_log_warn("Cannot monitor non-socket file descriptors.");
- e->dead = TRUE;
- }
- }
-#endif
-
PA_LLIST_PREPEND(pa_io_event, m->io_events, e);
- m->rebuild_pollfds = TRUE;
+ m->rebuild_pollfds = true;
m->n_io_events ++;
pa_mainloop_wakeup(m);
if (e->pollfd)
e->pollfd->events = map_flags_to_libc(events);
else
- e->mainloop->rebuild_pollfds = TRUE;
+ e->mainloop->rebuild_pollfds = true;
pa_mainloop_wakeup(e->mainloop);
}
pa_assert(e);
pa_assert(!e->dead);
- e->dead = TRUE;
+ e->dead = true;
e->mainloop->io_events_please_scan ++;
e->mainloop->n_io_events --;
- e->mainloop->rebuild_pollfds = TRUE;
+ e->mainloop->rebuild_pollfds = true;
pa_mainloop_wakeup(e->mainloop);
}
/* Defer events */
static pa_defer_event* mainloop_defer_new(
- pa_mainloop_api*a,
+ pa_mainloop_api *a,
pa_defer_event_cb_t callback,
void *userdata) {
e = pa_xnew0(pa_defer_event, 1);
e->mainloop = m;
- e->enabled = TRUE;
+ e->enabled = true;
m->n_enabled_defer_events++;
e->callback = callback;
pa_assert(e);
pa_assert(!e->dead);
- e->dead = TRUE;
+ e->dead = true;
e->mainloop->defer_events_please_scan ++;
if (e->enabled) {
pa_assert(e->mainloop->n_enabled_defer_events > 0);
e->mainloop->n_enabled_defer_events--;
- e->enabled = FALSE;
+ e->enabled = false;
}
}
}
/* Time events */
-static pa_usec_t make_rt(const struct timeval *tv, pa_bool_t *use_rtclock) {
+static pa_usec_t make_rt(const struct timeval *tv, bool *use_rtclock) {
struct timeval ttv;
if (!tv) {
- *use_rtclock = FALSE;
+ *use_rtclock = false;
return PA_USEC_INVALID;
}
- if (tv->tv_usec & PA_TIMEVAL_RTCLOCK) {
- ttv = *tv;
- ttv.tv_usec &= ~PA_TIMEVAL_RTCLOCK;
- tv = pa_rtclock_from_wallclock(&ttv);
+ ttv = *tv;
+ *use_rtclock = !!(ttv.tv_usec & PA_TIMEVAL_RTCLOCK);
- *use_rtclock = TRUE;
- } else
- *use_rtclock = FALSE;
+ if (*use_rtclock)
+ ttv.tv_usec &= ~PA_TIMEVAL_RTCLOCK;
+ else
+ pa_rtclock_from_wallclock(&ttv);
- return pa_timeval_load(tv);
+ return pa_timeval_load(&ttv);
}
static pa_time_event* mainloop_time_new(
- pa_mainloop_api*a,
+ pa_mainloop_api *a,
const struct timeval *tv,
pa_time_event_cb_t callback,
void *userdata) {
pa_mainloop *m;
pa_time_event *e;
pa_usec_t t;
- pa_bool_t use_rtclock = FALSE;
+ bool use_rtclock = false;
pa_assert(a);
pa_assert(a->userdata);
if ((e->enabled = (t != PA_USEC_INVALID))) {
e->time = t;
- e->use_rtclock= use_rtclock;
+ e->use_rtclock = use_rtclock;
m->n_enabled_time_events++;
}
static void mainloop_time_restart(pa_time_event *e, const struct timeval *tv) {
- pa_bool_t valid;
+ bool valid;
pa_usec_t t;
- pa_bool_t use_rtclock = FALSE;
+ bool use_rtclock = false;
pa_assert(e);
pa_assert(!e->dead);
pa_mainloop_wakeup(e->mainloop);
}
+ if (e->mainloop->cached_next_time_event == e)
+ e->mainloop->cached_next_time_event = NULL;
+
if (e->mainloop->cached_next_time_event && e->enabled) {
pa_assert(e->mainloop->cached_next_time_event->enabled);
if (t < e->mainloop->cached_next_time_event->time)
e->mainloop->cached_next_time_event = e;
- } else if (e->mainloop->cached_next_time_event == e)
- e->mainloop->cached_next_time_event = NULL;
+ }
}
static void mainloop_time_free(pa_time_event *e) {
pa_assert(e);
pa_assert(!e->dead);
- e->dead = TRUE;
+ e->dead = true;
e->mainloop->time_events_please_scan ++;
if (e->enabled) {
pa_assert(e->mainloop->n_enabled_time_events > 0);
e->mainloop->n_enabled_time_events--;
- e->enabled = FALSE;
+ e->enabled = false;
}
if (e->mainloop->cached_next_time_event == e)
/* quit() */
-static void mainloop_quit(pa_mainloop_api*a, int retval) {
+static void mainloop_quit(pa_mainloop_api *a, int retval) {
pa_mainloop *m;
pa_assert(a);
m = pa_xnew0(pa_mainloop, 1);
- if (pipe(m->wakeup_pipe) < 0) {
+ if (pa_pipe_cloexec(m->wakeup_pipe) < 0) {
pa_log_error("ERROR: cannot create wakeup pipe");
pa_xfree(m);
return NULL;
pa_make_fd_nonblock(m->wakeup_pipe[0]);
pa_make_fd_nonblock(m->wakeup_pipe[1]);
- pa_make_fd_cloexec(m->wakeup_pipe[0]);
- pa_make_fd_cloexec(m->wakeup_pipe[1]);
- m->rebuild_pollfds = TRUE;
+ m->rebuild_pollfds = true;
m->api = vtable;
m->api.userdata = m;
return m;
}
-static void cleanup_io_events(pa_mainloop *m, pa_bool_t force) {
+static void cleanup_io_events(pa_mainloop *m, bool force) {
pa_io_event *e, *n;
PA_LLIST_FOREACH_SAFE(e, n, m->io_events) {
pa_xfree(e);
- m->rebuild_pollfds = TRUE;
+ m->rebuild_pollfds = true;
}
}
pa_assert(m->io_events_please_scan == 0);
}
-static void cleanup_time_events(pa_mainloop *m, pa_bool_t force) {
+static void cleanup_time_events(pa_mainloop *m, bool force) {
pa_time_event *e, *n;
PA_LLIST_FOREACH_SAFE(e, n, m->time_events) {
if (!e->dead && e->enabled) {
pa_assert(m->n_enabled_time_events > 0);
m->n_enabled_time_events--;
- e->enabled = FALSE;
+ e->enabled = false;
}
if (e->destroy_callback)
pa_assert(m->time_events_please_scan == 0);
}
-static void cleanup_defer_events(pa_mainloop *m, pa_bool_t force) {
+static void cleanup_defer_events(pa_mainloop *m, bool force) {
pa_defer_event *e, *n;
PA_LLIST_FOREACH_SAFE(e, n, m->defer_events) {
if (!e->dead && e->enabled) {
pa_assert(m->n_enabled_defer_events > 0);
m->n_enabled_defer_events--;
- e->enabled = FALSE;
+ e->enabled = false;
}
if (e->destroy_callback)
pa_assert(m->defer_events_please_scan == 0);
}
-
-void pa_mainloop_free(pa_mainloop* m) {
+void pa_mainloop_free(pa_mainloop *m) {
pa_assert(m);
- cleanup_io_events(m, TRUE);
- cleanup_defer_events(m, TRUE);
- cleanup_time_events(m, TRUE);
+ cleanup_io_events(m, true);
+ cleanup_defer_events(m, true);
+ cleanup_time_events(m, true);
pa_xfree(m->pollfds);
pa_assert(m);
if (m->io_events_please_scan)
- cleanup_io_events(m, FALSE);
+ cleanup_io_events(m, false);
if (m->time_events_please_scan)
- cleanup_time_events(m, FALSE);
+ cleanup_time_events(m, false);
if (m->defer_events_please_scan)
- cleanup_defer_events(m, FALSE);
+ cleanup_defer_events(m, false);
}
static void rebuild_pollfds(pa_mainloop *m) {
m->n_pollfds = 0;
p = m->pollfds;
- if (m->wakeup_pipe[0] >= 0) {
- m->pollfds[0].fd = m->wakeup_pipe[0];
- m->pollfds[0].events = POLLIN;
- m->pollfds[0].revents = 0;
- p++;
- m->n_pollfds++;
- }
+ m->pollfds[0].fd = m->wakeup_pipe[0];
+ m->pollfds[0].events = POLLIN;
+ m->pollfds[0].revents = 0;
+ p++;
+ m->n_pollfds++;
PA_LLIST_FOREACH(e, m->io_events) {
if (e->dead) {
m->n_pollfds++;
}
- m->rebuild_pollfds = FALSE;
+ m->rebuild_pollfds = false;
}
static unsigned dispatch_pollfds(pa_mainloop *m) {
char c = 'W';
pa_assert(m);
- if (m->wakeup_pipe[1] >= 0 && m->state == STATE_POLLING) {
- pa_write(m->wakeup_pipe[1], &c, sizeof(c), &m->wakeup_pipe_type);
- m->wakeup_requested++;
- }
+ if (pa_write(m->wakeup_pipe[1], &c, sizeof(c), &m->wakeup_pipe_type) < 0)
+ /* Not much options for recovering from the error. Let's at least log something. */
+ pa_log("pa_write() failed while trying to wake up the mainloop: %s", pa_cstrerror(errno));
+
+ pa_atomic_store(&m->wakeup_requested, true);
}
static void clear_wakeup(pa_mainloop *m) {
pa_assert(m);
- if (m->wakeup_pipe[0] < 0)
- return;
-
- if (m->wakeup_requested) {
+ if (pa_atomic_cmpxchg(&m->wakeup_requested, true, false)) {
while (pa_read(m->wakeup_pipe[0], &c, sizeof(c), &m->wakeup_pipe_type) == sizeof(c))
;
- m->wakeup_requested = 0;
}
}
}
static int usec_to_timeout(pa_usec_t u) {
+ int timeout;
+
if (u == PA_USEC_INVALID)
return -1;
- return (u + PA_USEC_PER_MSEC - 1) / PA_USEC_PER_MSEC;
+ timeout = (u + PA_USEC_PER_MSEC - 1) / PA_USEC_PER_MSEC;
+ pa_assert(timeout >= 0);
+
+ return timeout;
}
int pa_mainloop_poll(pa_mainloop *m) {
m->prepared_timeout == PA_USEC_INVALID ? NULL : pa_timespec_store(&ts, m->prepared_timeout),
NULL);
#else
- m->poll_func_ret = poll(
+ m->poll_func_ret = pa_poll(
m->pollfds, m->n_pollfds,
usec_to_timeout(m->prepared_timeout));
#endif
if (r == -2)
return 1;
- else if (r < 0)
- return -1;
else
- return 0;
+ return -1;
}
void pa_mainloop_quit(pa_mainloop *m, int retval) {
pa_assert(m);
- m->quit = TRUE;
+ m->quit = true;
m->retval = retval;
pa_mainloop_wakeup(m);
}
-pa_mainloop_api* pa_mainloop_get_api(pa_mainloop*m) {
+pa_mainloop_api* pa_mainloop_get_api(pa_mainloop *m) {
pa_assert(m);
return &m->api;
m->poll_func_userdata = userdata;
}
-pa_bool_t pa_mainloop_is_our_api(pa_mainloop_api*m) {
+bool pa_mainloop_is_our_api(pa_mainloop_api *m) {
pa_assert(m);
return m->io_new == mainloop_io_new;