mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
signal: add reference count
This commit is contained in:
parent
86b03265b2
commit
7c74f0f2e9
2 changed files with 32 additions and 2 deletions
|
@ -25,6 +25,8 @@
|
|||
|
||||
#include <jansson.h>
|
||||
|
||||
#include <villas/atomic.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
@ -47,16 +49,24 @@ enum signal_format {
|
|||
* This data structure contains meta data about samples values in struct sample::data
|
||||
*/
|
||||
struct signal {
|
||||
char *name; /**< The name of the signal. */
|
||||
char *unit; /**< The unit of the signal. */
|
||||
char *name; /**< The name of the signal. */
|
||||
char *unit; /**< The unit of the signal. */
|
||||
|
||||
int enabled;
|
||||
|
||||
atomic_int refcnt; /**< Reference counter. */
|
||||
|
||||
enum signal_format format;
|
||||
};
|
||||
|
||||
int signal_init(struct signal *s);
|
||||
|
||||
/** Increase reference counter. */
|
||||
int signal_get(struct signal *s);
|
||||
|
||||
/** Decrease reference counter. */
|
||||
int signal_put(struct signal *s);
|
||||
|
||||
int signal_init_from_mapping(struct signal *s, const struct mapping_entry *me, unsigned index);
|
||||
|
||||
int signal_destroy(struct signal *s);
|
||||
|
|
20
lib/signal.c
20
lib/signal.c
|
@ -30,8 +30,12 @@
|
|||
|
||||
int signal_init(struct signal *s)
|
||||
{
|
||||
s->name = NULL;
|
||||
s->unit = NULL;
|
||||
s->format = SIGNAL_FORMAT_UNKNOWN;
|
||||
|
||||
s->refcnt = ATOMIC_VAR_INIT(1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -95,6 +99,22 @@ int signal_destroy(struct signal *s)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int signal_get(struct signal *s)
|
||||
{
|
||||
return atomic_fetch_add(&s->refcnt, 1) + 1;
|
||||
}
|
||||
|
||||
int signal_put(struct signal *s)
|
||||
{
|
||||
int prev = atomic_fetch_sub(&s->refcnt, 1);
|
||||
|
||||
/* Did we had the last reference? */
|
||||
if (prev == 1)
|
||||
signal_destroy(s);
|
||||
|
||||
return prev - 1;
|
||||
}
|
||||
|
||||
int signal_parse(struct signal *s, json_t *cfg)
|
||||
{
|
||||
int ret;
|
||||
|
|
Loading…
Add table
Reference in a new issue