Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
more running flag changes (atomic ops)
  • Loading branch information
perexg committed Mar 8, 2016
1 parent 7b4bb43 commit eaf5a79
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 37 deletions.
16 changes: 8 additions & 8 deletions src/dbus.c
Expand Up @@ -65,7 +65,7 @@ dbus_emit_signal(const char *obj_name, const char *sig_name, htsmsg_t *msg)
int unused __attribute__((unused));
size_t l;

if (!dbus_running) {
if (!atomic_get(&dbus_running)) {
htsmsg_destroy(msg);
return;
}
Expand Down Expand Up @@ -354,13 +354,13 @@ dbus_server_thread(void *aux)

conn = dbus_create_session("org.tvheadend.server");
if (conn == NULL) {
dbus_running = 0;
atomic_set(&dbus_running, 0);
return NULL;
}

notify = dbus_create_session("org.tvheadend.notify");
if (notify == NULL) {
dbus_running = 0;
atomic_set(&dbus_running, 0);
dbus_connection_safe_close(conn);
return NULL;
}
Expand All @@ -373,7 +373,7 @@ dbus_server_thread(void *aux)
tvhpoll_add(poll, &ev, 1);
memset(&ev, 0, sizeof(ev));
if (!dbus_connection_get_unix_fd(conn, &ev.fd)) {
dbus_running = 0;
atomic_set(&dbus_running, 0);
tvhpoll_destroy(poll);
dbus_connection_safe_close(notify);
dbus_connection_safe_close(conn);
Expand All @@ -383,11 +383,11 @@ dbus_server_thread(void *aux)
ev.data.ptr = conn;
tvhpoll_add(poll, &ev, 1);

while (dbus_running) {
while (atomic_get(&dbus_running)) {

n = tvhpoll_wait(poll, &ev, 1, -1);
if (n < 0) {
if (dbus_running && !ERRNO_AGAIN(errno))
if (atomic_get(&dbus_running) && !ERRNO_AGAIN(errno))
tvherror("dbus", "tvhpoll_wait() error");
} else if (n == 0) {
continue;
Expand Down Expand Up @@ -449,7 +449,7 @@ dbus_server_init(int enabled, int session)
if (enabled) {
tvh_pipe(O_NONBLOCK, &dbus_pipe);
dbus_threads_init_default();
dbus_running = 1;
atomic_set(&dbus_running, 1);
dbus_emit_signal_str("/main", "start", tvheadend_version);
}
}
Expand All @@ -467,7 +467,7 @@ dbus_server_done(void)
dbus_rpc_t *rpc;

dbus_emit_signal_str("/main", "stop", "bye");
dbus_running = 0;
atomic_set(&dbus_running, 0);
if (dbus_pipe.wr > 0) {
tvh_write(dbus_pipe.wr, "", 1);
pthread_kill(dbus_tid, SIGTERM);
Expand Down
14 changes: 7 additions & 7 deletions src/epggrab.c
Expand Up @@ -95,10 +95,10 @@ static void* _epggrab_internal_thread ( void* p )
ts.tv_sec = time(NULL) + 120;

/* Time for other jobs */
while (epggrab_running) {
while (atomic_get(&epggrab_running)) {
pthread_mutex_lock(&epggrab_mutex);
err = ETIMEDOUT;
while (epggrab_running) {
while (atomic_get(&epggrab_running)) {
err = pthread_cond_timedwait(&epggrab_cond, &epggrab_mutex, &ts);
if (err == ETIMEDOUT) break;
}
Expand All @@ -108,11 +108,11 @@ static void* _epggrab_internal_thread ( void* p )

time(&ts.tv_sec);

while (epggrab_running) {
while (atomic_get(&epggrab_running)) {

/* Check for config change */
pthread_mutex_lock(&epggrab_mutex);
while (epggrab_running && confver == epggrab_confver) {
while (atomic_get(&epggrab_running) && confver == epggrab_confver) {
err = pthread_cond_timedwait(&epggrab_cond, &epggrab_mutex, &ts);
if (err == ETIMEDOUT) break;
}
Expand All @@ -126,7 +126,7 @@ static void* _epggrab_internal_thread ( void* p )
/* Run grabber(s) */
/* Note: this loop is not protected, assuming static boot allocation */
LIST_FOREACH(mod, &epggrab_modules, link) {
if (!epggrab_running)
if (!atomic_get(&epggrab_running))
break;
if (mod->type == EPGGRAB_INT)
_epggrab_module_grab((epggrab_module_int_t *)mod);
Expand Down Expand Up @@ -420,7 +420,7 @@ void epggrab_init ( void )
epggrab_ota_post();

/* Start internal grab thread */
epggrab_running = 1;
atomic_set(&epggrab_running, 1);
tvhthread_create(&epggrab_tid, NULL, _epggrab_internal_thread, NULL, "epggrabi");
}

Expand All @@ -431,7 +431,7 @@ void epggrab_done ( void )
{
epggrab_module_t *mod;

epggrab_running = 0;
atomic_set(&epggrab_running, 0);
pthread_cond_signal(&epggrab_cond);
pthread_join(epggrab_tid, NULL);

Expand Down
10 changes: 5 additions & 5 deletions src/epggrab/otamux.c
Expand Up @@ -328,7 +328,7 @@ epggrab_ota_register
int save = 0;
epggrab_ota_map_t *map;

if (!epggrab_ota_running)
if (!atomic_get(&epggrab_ota_running))
return NULL;

if (ota == NULL) {
Expand Down Expand Up @@ -698,7 +698,7 @@ epggrab_ota_service_add ( epggrab_ota_map_t *map, epggrab_ota_mux_t *ota,
{
epggrab_ota_svc_link_t *svcl;

if (uuid == NULL || !epggrab_ota_running)
if (uuid == NULL || !atomic_get(&epggrab_ota_running))
return;
SKEL_ALLOC(epggrab_svc_link_skel);
epggrab_svc_link_skel->uuid = (char *)uuid;
Expand All @@ -718,7 +718,7 @@ void
epggrab_ota_service_del ( epggrab_ota_map_t *map, epggrab_ota_mux_t *ota,
epggrab_ota_svc_link_t *svcl, int save )
{
if (svcl == NULL || (!epggrab_ota_running && save))
if (svcl == NULL || (!atomic_get(&epggrab_ota_running) && save))
return;
epggrab_ota_service_trace(ota, svcl, "delete");
RB_REMOVE(&map->om_svcs, svcl, link);
Expand Down Expand Up @@ -842,7 +842,7 @@ epggrab_ota_init ( void )
if (!S_ISDIR(st.st_mode))
hts_settings_remove("epggrab/otamux");

epggrab_ota_running = 1;
atomic_set(&epggrab_ota_running, 1);

/* Load config */
if ((c = hts_settings_load_r(1, "epggrab/otamux"))) {
Expand Down Expand Up @@ -908,7 +908,7 @@ epggrab_ota_shutdown ( void )
{
epggrab_ota_mux_t *ota;

epggrab_ota_running = 0;
atomic_set(&epggrab_ota_running, 0);
while ((ota = TAILQ_FIRST(&epggrab_ota_active)) != NULL)
epggrab_ota_free(&epggrab_ota_active, ota);
while ((ota = TAILQ_FIRST(&epggrab_ota_pending)) != NULL)
Expand Down
2 changes: 1 addition & 1 deletion src/input/mpegts/satip/satip.c
Expand Up @@ -1232,7 +1232,7 @@ satip_discovery_timer_cb(void *aux)
{
if (!tvheadend_is_running())
return;
if (!upnp_running) {
if (!atomic_get(&upnp_running)) {
mtimer_arm_rel(&satip_discovery_timer, satip_discovery_timer_cb,
NULL, sec2mono(1));
return;
Expand Down
6 changes: 3 additions & 3 deletions src/spawn.c
Expand Up @@ -127,7 +127,7 @@ spawn_pipe_thread(void *aux)
ev[1].data.ptr = &spawn_pipe_error;
tvhpoll_add(efd, ev, 2);

while (spawn_pipe_running) {
while (atomic_get(&spawn_pipe_running)) {

nfds = tvhpoll_wait(efd, ev, 2, 500);

Expand Down Expand Up @@ -643,15 +643,15 @@ void spawn_init(void)
{
tvh_pipe(O_NONBLOCK, &spawn_pipe_info);
tvh_pipe(O_NONBLOCK, &spawn_pipe_error);
spawn_pipe_running = 1;
atomic_set(&spawn_pipe_running, 1);
pthread_create(&spawn_pipe_tid, NULL, spawn_pipe_thread, NULL);
}

void spawn_done(void)
{
spawn_t *s;

spawn_pipe_running = 0;
atomic_set(&spawn_pipe_running, 0);
pthread_kill(spawn_pipe_tid, SIGTERM);
pthread_join(spawn_pipe_tid, NULL);
tvh_pipe_close(&spawn_pipe_error);
Expand Down
6 changes: 3 additions & 3 deletions src/tcp.c
Expand Up @@ -667,7 +667,7 @@ tcp_server_loop(void *aux)
socklen_t slen;
char c;

while(tcp_server_running) {
while(atomic_get(&tcp_server_running)) {
r = tvhpoll_wait(tcp_server_poll, &ev, 1, -1);
if(r < 0) {
if (ERRNO_AGAIN(r))
Expand Down Expand Up @@ -1085,7 +1085,7 @@ tcp_server_init(void)
ev.data.ptr = &tcp_server_pipe;
tvhpoll_add(tcp_server_poll, &ev, 1);

tcp_server_running = 1;
atomic_set(&tcp_server_running, 1);
tvhthread_create(&tcp_server_tid, NULL, tcp_server_loop, NULL, "tcp-loop");
}

Expand All @@ -1097,7 +1097,7 @@ tcp_server_done(void)
char c = 'E';
int64_t t;

tcp_server_running = 0;
atomic_set(&tcp_server_running, 0);
tvh_write(tcp_server_pipe.wr, &c, 1);

pthread_mutex_lock(&global_lock);
Expand Down
10 changes: 5 additions & 5 deletions src/upnp.c
Expand Up @@ -88,7 +88,7 @@ upnp_send( htsbuf_queue_t *q, struct sockaddr_storage *storage,
{
upnp_data_t *data;

if (!upnp_running)
if (!atomic_get(&upnp_running))
return;
data = calloc(1, sizeof(upnp_data_t));
htsbuf_queue_init(&data->queue, 0);
Expand Down Expand Up @@ -158,7 +158,7 @@ upnp_thread( void *aux )

delay_ms = 0;

while (upnp_running && multicast->fd >= 0) {
while (atomic_get(&upnp_running) && multicast->fd >= 0) {
r = tvhpoll_wait(poll, ev, 2, delay_ms ?: 1000);
if (r == 0) /* timeout */
delay_ms = 0;
Expand Down Expand Up @@ -224,7 +224,7 @@ upnp_thread( void *aux )
}

error:
upnp_running = 0;
atomic_set(&upnp_running, 0);
tvhpoll_destroy(poll);
udp_close(unicast);
udp_close(multicast);
Expand All @@ -248,7 +248,7 @@ upnp_server_init(const char *bindaddr)
pthread_mutex_init(&upnp_lock, NULL);
TAILQ_INIT(&upnp_data_write);
TAILQ_INIT(&upnp_services);
upnp_running = 1;
atomic_set(&upnp_running, 1);
tvhthread_create(&upnp_tid, NULL, upnp_thread, (char *)bindaddr, "upnp");
}

Expand All @@ -258,7 +258,7 @@ upnp_server_done(void)
upnp_data_t *data;
upnp_service_t *us;

upnp_running = 0;
atomic_set(&upnp_running, 0);
pthread_kill(upnp_tid, SIGTERM);
pthread_join(upnp_tid, NULL);
while ((us = TAILQ_FIRST(&upnp_services)) != NULL)
Expand Down
10 changes: 5 additions & 5 deletions src/webui/comet.c
Expand Up @@ -246,7 +246,7 @@ comet_mailbox_poll(http_connection_t *hc, const char *remain, void *opaque)
tvh_safe_usleep(100000); /* Always sleep 0.1 sec to avoid comet storms */

pthread_mutex_lock(&comet_mutex);
if (!comet_running) {
if (!atomic_get(&comet_running)) {
pthread_mutex_unlock(&comet_mutex);
return HTTP_STATUS_BAD_REQUEST;
}
Expand All @@ -270,7 +270,7 @@ comet_mailbox_poll(http_connection_t *hc, const char *remain, void *opaque)
if (e == ETIMEDOUT)
break;
} while (ERRNO_AGAIN(e));
if (!comet_running) {
if (!atomic_get(&comet_running)) {
pthread_mutex_unlock(&comet_mutex);
return 400;
}
Expand Down Expand Up @@ -338,7 +338,7 @@ comet_init(void)
{
pthread_mutex_lock(&comet_mutex);
tvh_cond_init(&comet_cond);
comet_running = 1;
atomic_set(&comet_running, 1);
pthread_mutex_unlock(&comet_mutex);
http_path_add("/comet/poll", NULL, comet_mailbox_poll, ACCESS_WEB_INTERFACE);
http_path_add("/comet/debug", NULL, comet_mailbox_dbg, ACCESS_WEB_INTERFACE);
Expand All @@ -350,7 +350,7 @@ comet_done(void)
comet_mailbox_t *cmb;

pthread_mutex_lock(&comet_mutex);
comet_running = 0;
atomic_set(&comet_running, 0);
while ((cmb = LIST_FIRST(&mailboxes)) != NULL)
cmb_destroy(cmb);
pthread_mutex_unlock(&comet_mutex);
Expand Down Expand Up @@ -391,7 +391,7 @@ comet_mailbox_add_message(htsmsg_t *m, int isdebug, int rewrite)

pthread_mutex_lock(&comet_mutex);

if (comet_running) {
if (atomic_get(&comet_running)) {
LIST_FOREACH(cmb, &mailboxes, cmb_link) {

if(isdebug && !cmb->cmb_debug)
Expand Down

0 comments on commit eaf5a79

Please sign in to comment.