Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
mpegts table: use atomic refcounting
  • Loading branch information
perexg committed Nov 12, 2014
1 parent 7211380 commit 9990780
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/atomic.h
Expand Up @@ -24,6 +24,12 @@ atomic_add(volatile int *ptr, int incr)
return __sync_fetch_and_add(ptr, incr);
}

static inline int
atomic_dec(volatile int *ptr, int decr)
{
return __sync_fetch_and_sub(ptr, decr);
}

static inline int
atomic_exchange(volatile int *ptr, int new)
{
Expand Down
14 changes: 10 additions & 4 deletions src/input/mpegts.h
Expand Up @@ -24,6 +24,7 @@
#error "Use header file input.h not input/mpegts.h"
#endif

#include "atomic.h"
#include "input.h"
#include "service.h"
#include "mpegts/dvb.h"
Expand Down Expand Up @@ -198,7 +199,7 @@ struct mpegts_table
int mt_mask; // mask

int mt_destroyed; // Refcounting
int mt_refcount;
int mt_arefcount;

int8_t mt_cc;

Expand Down Expand Up @@ -794,14 +795,19 @@ void mpegts_input_close_pid
void mpegts_table_dispatch
(const uint8_t *sec, size_t r, void *mt);
static inline void mpegts_table_grab
(mpegts_table_t *mt) { mt->mt_refcount++; }
(mpegts_table_t *mt)
{
int v = atomic_add(&mt->mt_arefcount, 1);
assert(v > 0);
}
void mpegts_table_release_
(mpegts_table_t *mt);
static inline void mpegts_table_release
(mpegts_table_t *mt)
{
assert(mt->mt_refcount > 0);
if(--mt->mt_refcount == 0) {
int v = atomic_dec(&mt->mt_arefcount, 1);
assert(v > 0);
if (v == 1) {
assert(mt->mt_destroyed == 1);
mpegts_table_release_(mt);
}
Expand Down
20 changes: 10 additions & 10 deletions src/input/mpegts/mpegts_table.c
Expand Up @@ -241,16 +241,16 @@ mpegts_table_add

/* Create */
mt = calloc(1, sizeof(mpegts_table_t));
mt->mt_refcount = 1;
mt->mt_name = strdup(name);
mt->mt_callback = callback;
mt->mt_opaque = opaque;
mt->mt_pid = pid;
mt->mt_flags = flags & ~(MT_SKIPSUBS|MT_SCANSUBS);
mt->mt_table = tableid;
mt->mt_mask = mask;
mt->mt_mux = mm;
mt->mt_cc = -1;
mt->mt_arefcount = 1;
mt->mt_name = strdup(name);
mt->mt_callback = callback;
mt->mt_opaque = opaque;
mt->mt_pid = pid;
mt->mt_flags = flags & ~(MT_SKIPSUBS|MT_SCANSUBS);
mt->mt_table = tableid;
mt->mt_mask = mask;
mt->mt_mux = mm;
mt->mt_cc = -1;

/* Open table */
if (pid < 0) {
Expand Down

0 comments on commit 9990780

Please sign in to comment.