Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
subscriptions: use ths_state atomically (clang sanitizer)
  • Loading branch information
perexg committed Mar 10, 2016
1 parent b028dc9 commit 6b3a506
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
34 changes: 23 additions & 11 deletions src/subscriptions.c
Expand Up @@ -62,6 +62,18 @@ shortid(th_subscription_t *s)
return s->ths_id & 0xffff;
}

static inline void
subsetstate(th_subscription_t *s, ths_state_t state)
{
atomic_set(&s->ths_state, state);
}

static inline ths_state_t
subgetstate(th_subscription_t *s)
{
return atomic_get(&s->ths_state);
}

/* **************************************************************************
* Subscription linking
* *************************************************************************/
Expand All @@ -73,7 +85,7 @@ static void
subscription_link_service(th_subscription_t *s, service_t *t)
{
streaming_message_t *sm;
s->ths_state = SUBSCRIPTION_TESTING_SERVICE;
subsetstate(s, SUBSCRIPTION_TESTING_SERVICE);

s->ths_service = t;
LIST_INSERT_HEAD(&t->s_subscriptions, s, ths_service_link);
Expand Down Expand Up @@ -102,7 +114,7 @@ subscription_link_service(th_subscription_t *s, service_t *t)

if(s->ths_start_message != NULL && t->s_streaming_status & TSS_PACKETS) {

s->ths_state = SUBSCRIPTION_GOT_SERVICE;
subsetstate(s, SUBSCRIPTION_GOT_SERVICE);

// Send a START message to the subscription client
streaming_target_deliver(s->ths_output, s->ths_start_message);
Expand Down Expand Up @@ -326,7 +338,7 @@ subscription_reschedule(void)
if(t != NULL && s->ths_current_instance != NULL) {
/* Already got a service */

if(s->ths_state != SUBSCRIPTION_BAD_SERVICE)
if(subgetstate(s) != SUBSCRIPTION_BAD_SERVICE)
continue; /* And it not bad, so we're happy */

tvhwarn("subscription", "%04X: service instance is bad, reason: %s",
Expand Down Expand Up @@ -451,7 +463,7 @@ static void
subscription_input_null(void *opaque, streaming_message_t *sm)
{
th_subscription_t *s = opaque;
if (sm->sm_type == SMT_STOP && s->ths_state != SUBSCRIPTION_ZOMBIE) {
if (sm->sm_type == SMT_STOP && subgetstate(s) != SUBSCRIPTION_ZOMBIE) {
LIST_INSERT_HEAD(&subscriptions_remove, s, ths_remove_link);
mtimer_arm_rel(&subscription_reschedule_timer,
subscription_reschedule_cb, NULL, 0);
Expand Down Expand Up @@ -494,7 +506,7 @@ subscription_input(void *opauqe, streaming_message_t *sm)
int error;
th_subscription_t *s = opauqe;

if(s->ths_state == SUBSCRIPTION_TESTING_SERVICE) {
if(subgetstate(s) == SUBSCRIPTION_TESTING_SERVICE) {
// We are just testing if this service is good

if(sm->sm_type == SMT_GRACE) {
Expand All @@ -517,7 +529,7 @@ subscription_input(void *opauqe, streaming_message_t *sm)
(s->ths_flags & SUBSCRIPTION_CONTACCESS) == 0) {
if (error > s->ths_testing_error)
s->ths_testing_error = error;
s->ths_state = SUBSCRIPTION_BAD_SERVICE;
subsetstate(s, SUBSCRIPTION_BAD_SERVICE);
streaming_msg_free(sm);
}
return;
Expand All @@ -531,11 +543,11 @@ subscription_input(void *opauqe, streaming_message_t *sm)
if (s->ths_service)
s->ths_service->s_running = 1;
}
s->ths_state = SUBSCRIPTION_GOT_SERVICE;
subsetstate(s, SUBSCRIPTION_GOT_SERVICE);
}
}

if(s->ths_state != SUBSCRIPTION_GOT_SERVICE) {
if(subgetstate(s) != SUBSCRIPTION_GOT_SERVICE) {
streaming_msg_free(sm);
return;
}
Expand Down Expand Up @@ -601,14 +613,14 @@ subscription_unsubscribe(th_subscription_t *s, int flags)
t = s->ths_service;
raw = s->ths_raw_service;

if (s->ths_state == SUBSCRIPTION_ZOMBIE) {
if (subgetstate(s) == SUBSCRIPTION_ZOMBIE) {
if ((flags & UNSUBSCRIBE_FINAL) != 0) {
subscription_destroy(s);
return;
}
abort();
}
s->ths_state = SUBSCRIPTION_ZOMBIE;
subsetstate(s, SUBSCRIPTION_ZOMBIE);

LIST_REMOVE(s, ths_global_link);
LIST_SAFE_REMOVE(s, ths_remove_link);
Expand Down Expand Up @@ -889,7 +901,7 @@ subscription_create_msg(th_subscription_t *s, const char *lang)
htsmsg_add_u32(m, "errors", atomic_get(&s->ths_total_err));

const char *state;
switch(s->ths_state) {
switch(subgetstate(s)) {
default:
state = N_("Idle");
break;
Expand Down
17 changes: 10 additions & 7 deletions src/subscriptions.h
Expand Up @@ -57,6 +57,15 @@ extern struct th_subscription_list subscriptions;
#define UNSUBSCRIBE_QUIET 0x01
#define UNSUBSCRIBE_FINAL 0x02

/* States */
typedef enum {
SUBSCRIPTION_IDLE,
SUBSCRIPTION_TESTING_SERVICE,
SUBSCRIPTION_GOT_SERVICE,
SUBSCRIPTION_BAD_SERVICE,
SUBSCRIPTION_ZOMBIE
} ths_state_t;

typedef struct th_subscription {

int ths_id;
Expand All @@ -68,13 +77,7 @@ typedef struct th_subscription {

int ths_weight;

enum {
SUBSCRIPTION_IDLE,
SUBSCRIPTION_TESTING_SERVICE,
SUBSCRIPTION_GOT_SERVICE,
SUBSCRIPTION_BAD_SERVICE,
SUBSCRIPTION_ZOMBIE
} ths_state;
int ths_state;

int ths_testing_error;

Expand Down

0 comments on commit 6b3a506

Please sign in to comment.