Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
notify: move notify_delayed from idnode.c to notify.c (for EPG)
  • Loading branch information
perexg committed May 5, 2015
1 parent 2a35e4b commit ee54976
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 89 deletions.
1 change: 0 additions & 1 deletion src/dvr/dvr_db.c
Expand Up @@ -922,7 +922,6 @@ static dvr_entry_t *_dvr_entry_update
de->de_bcast = e;
e->getref(e);
save = 1;

}

/* Episode */
Expand Down
89 changes: 1 addition & 88 deletions src/idnode.c
Expand Up @@ -44,10 +44,6 @@ typedef struct idclass_link
static idnodes_rb_t idnodes;
static RB_HEAD(,idclass_link) idclasses;
static RB_HEAD(,idclass_link) idrootclasses;
static pthread_cond_t idnode_cond;
static pthread_mutex_t idnode_mutex;
static htsmsg_t *idnode_queue;
static void* idnode_thread(void* p);

SKEL_DECLARE(idclasses_skel, idclass_link_t);

Expand Down Expand Up @@ -84,26 +80,16 @@ pthread_t idnode_tid;
void
idnode_init(void)
{
idnode_queue = NULL;
RB_INIT(&idnodes);
RB_INIT(&idclasses);
RB_INIT(&idrootclasses);
pthread_mutex_init(&idnode_mutex, NULL);
pthread_cond_init(&idnode_cond, NULL);
tvhthread_create(&idnode_tid, NULL, idnode_thread, NULL);
}

void
idnode_done(void)
{
idclass_link_t *il;

pthread_cond_signal(&idnode_cond);
pthread_join(idnode_tid, NULL);
pthread_mutex_lock(&idnode_mutex);
htsmsg_destroy(idnode_queue);
idnode_queue = NULL;
pthread_mutex_unlock(&idnode_mutex);
while ((il = RB_FIRST(&idclasses)) != NULL) {
RB_REMOVE(&idclasses, il, link);
free(il);
Expand Down Expand Up @@ -1337,38 +1323,6 @@ idnode_serialize0(idnode_t *self, htsmsg_t *list, int optmask)
* Notification
* *************************************************************************/

/**
* Delayed notification
*/
static void
idnode_notify_delayed
( const char *uuid, const char *event, const char *action )
{
htsmsg_t *m = NULL, *e = NULL;
htsmsg_field_t *f;

pthread_mutex_lock(&idnode_mutex);
if (idnode_queue == NULL) {
idnode_queue = htsmsg_create_map();
} else {
m = htsmsg_get_map(idnode_queue, event);
}
if (m == NULL) {
m = htsmsg_add_msg(idnode_queue, event, htsmsg_create_map());
} else {
e = htsmsg_get_list(m, action);
}
if (e == NULL)
e = htsmsg_add_msg(m, action, htsmsg_create_list());
HTSMSG_FOREACH(f, e)
if (strcmp(htsmsg_field_get_str(f) ?: "", uuid) == 0)
goto skip;
htsmsg_add_str(e, NULL, uuid);
pthread_cond_signal(&idnode_cond);
skip:
pthread_mutex_unlock(&idnode_mutex);
}

/**
* Notify about a change
*/
Expand All @@ -1383,7 +1337,7 @@ idnode_notify ( idnode_t *in, const char *action )

while (ic) {
if (ic->ic_event)
idnode_notify_delayed(uuid, ic->ic_event, action);
notify_delayed(uuid, ic->ic_event, action);
ic = ic->ic_super;
}
}
Expand All @@ -1404,47 +1358,6 @@ idnode_notify_title_changed (void *in)
idnode_notify_changed(in);
}

/*
* Thread for handling notifications
*/
void*
idnode_thread ( void *p )
{
htsmsg_t *q = NULL;
htsmsg_field_t *f;

pthread_mutex_lock(&idnode_mutex);

while (tvheadend_running) {

/* Get queue */
if (!idnode_queue) {
pthread_cond_wait(&idnode_cond, &idnode_mutex);
continue;
}
q = idnode_queue;
idnode_queue = NULL;
pthread_mutex_unlock(&idnode_mutex);

/* Process */
pthread_mutex_lock(&global_lock);

HTSMSG_FOREACH(f, q)
notify_by_msg(f->hmf_name, htsmsg_detach_submsg(f));

/* Finished */
pthread_mutex_unlock(&global_lock);
htsmsg_destroy(q);

/* Wait */
usleep(500000);
pthread_mutex_lock(&idnode_mutex);
}
pthread_mutex_unlock(&idnode_mutex);

return NULL;
}

/******************************************************************************
* Editor Configuration
*
Expand Down
3 changes: 3 additions & 0 deletions src/main.c
Expand Up @@ -58,6 +58,7 @@
#include "trap.h"
#include "settings.h"
#include "config.h"
#include "notify.h"
#include "idnode.h"
#include "imagecache.h"
#include "timeshift.h"
Expand Down Expand Up @@ -949,6 +950,7 @@ main(int argc, char **argv)
SSL_library_init();

/* Initialise configuration */
notify_init();
idnode_init();
spawn_init();
config_init(opt_nobackup == 0);
Expand Down Expand Up @@ -1117,6 +1119,7 @@ main(int argc, char **argv)
tvhftrace("main", intlconv_done);
tvhftrace("main", urlparse_done);
tvhftrace("main", idnode_done);
tvhftrace("main", notify_done);
tvhftrace("main", spawn_done);

tvhlog(LOG_NOTICE, "STOP", "Exiting HTS Tvheadend");
Expand Down
97 changes: 97 additions & 0 deletions src/notify.c
Expand Up @@ -26,6 +26,12 @@
#include "notify.h"
#include "webui/webui.h"

static pthread_cond_t notify_cond;
static pthread_mutex_t notify_mutex;
static htsmsg_t *notify_queue;
static pthread_t notify_tid;
static void* notify_thread(void* p);

void
notify_by_msg(const char *class, htsmsg_t *m)
{
Expand All @@ -42,3 +48,94 @@ notify_reload(const char *class)
htsmsg_add_u32(m, "reload", 1);
notify_by_msg(class, m);
}

void
notify_delayed(const char *id, const char *event, const char *action)
{
htsmsg_t *m = NULL, *e = NULL;
htsmsg_field_t *f;

if (!tvheadend_running)
return;

pthread_mutex_lock(&notify_mutex);
if (notify_queue == NULL) {
notify_queue = htsmsg_create_map();
} else {
m = htsmsg_get_map(notify_queue, event);
}
if (m == NULL) {
m = htsmsg_add_msg(notify_queue, event, htsmsg_create_map());
} else {
e = htsmsg_get_list(m, action);
}
if (e == NULL)
e = htsmsg_add_msg(m, action, htsmsg_create_list());
HTSMSG_FOREACH(f, e)
if (strcmp(htsmsg_field_get_str(f) ?: "", id) == 0)
goto skip;
htsmsg_add_str(e, NULL, id);
pthread_cond_signal(&notify_cond);
skip:
pthread_mutex_unlock(&notify_mutex);
}

void *
notify_thread ( void *p )
{
htsmsg_t *q = NULL;
htsmsg_field_t *f;

pthread_mutex_lock(&notify_mutex);

while (tvheadend_running) {

/* Get queue */
if (!notify_queue) {
pthread_cond_wait(&notify_cond, &notify_mutex);
continue;
}
q = notify_queue;
notify_queue = NULL;
pthread_mutex_unlock(&notify_mutex);

/* Process */
pthread_mutex_lock(&global_lock);

HTSMSG_FOREACH(f, q)
notify_by_msg(f->hmf_name, htsmsg_detach_submsg(f));

/* Finished */
pthread_mutex_unlock(&global_lock);
htsmsg_destroy(q);

/* Wait */
usleep(500000);
pthread_mutex_lock(&notify_mutex);
}
pthread_mutex_unlock(&notify_mutex);

return NULL;
}

/*
*
*/

void notify_init( void )
{
notify_queue = NULL;
pthread_mutex_init(&notify_mutex, NULL);
pthread_cond_init(&notify_cond, NULL);
tvhthread_create(&notify_tid, NULL, notify_thread, NULL);
}

void notify_done( void )
{
pthread_cond_signal(&notify_cond);
pthread_join(notify_tid, NULL);
pthread_mutex_lock(&notify_mutex);
htsmsg_destroy(notify_queue);
notify_queue = NULL;
pthread_mutex_unlock(&notify_mutex);
}
5 changes: 5 additions & 0 deletions src/notify.h
Expand Up @@ -25,4 +25,9 @@ void notify_by_msg(const char *class, htsmsg_t *m);

void notify_reload(const char *class);

void notify_delayed(const char *id, const char *event, const char *action);

void notify_init(void);
void notify_done(void);

#endif /* NOTIFY_H_ */

0 comments on commit ee54976

Please sign in to comment.