From 429c07f1e36894802e0e3bbe6f161901e06f7fca Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 6 Mar 2017 13:26:23 -0400 Subject: [PATCH] add little FSM to pool, web, api, queue to track state of initialisation --- include/villas/api.h | 5 +++++ include/villas/pool.h | 5 +++++ include/villas/queue.h | 5 +++++ include/villas/web.h | 5 +++++ lib/api.c | 6 ++++++ lib/pool.c | 7 ++++++- lib/queue.c | 11 ++++++++++- lib/web.c | 7 ++++++- 8 files changed, 48 insertions(+), 3 deletions(-) diff --git a/include/villas/api.h b/include/villas/api.h index f6fb7a500..2258d7b90 100644 --- a/include/villas/api.h +++ b/include/villas/api.h @@ -43,6 +43,11 @@ enum api_mode { struct api { struct list sessions; /**< List of currently active connections */ + enum { + API_STATE_DESTROYED, + API_STATE_INITIALIZED + } state; + struct cfg *cfg; }; diff --git a/include/villas/pool.h b/include/villas/pool.h index f54f43081..5285b2a6e 100644 --- a/include/villas/pool.h +++ b/include/villas/pool.h @@ -19,6 +19,11 @@ struct pool { void *buffer; /**< Address of the underlying memory area */ const struct memtype *mem; + enum { + POOL_STATE_DESTROYED, + POOL_STATE_INITIALIZED + } state; + size_t len; /**< Length of the underlying memory area */ size_t blocksz; /**< Length of a block in bytes */ diff --git a/include/villas/queue.h b/include/villas/queue.h index 7c57a23fc..3137e874b 100644 --- a/include/villas/queue.h +++ b/include/villas/queue.h @@ -43,6 +43,11 @@ typedef char cacheline_pad_t[CACHELINE_SIZE]; struct queue { cacheline_pad_t _pad0; /**< Shared area: all threads read */ + + enum { + QUEUE_STATE_DESTROYED, + QUEUE_STATE_INITIALIZED + } state; struct memtype const * mem; size_t buffer_mask; diff --git a/include/villas/web.h b/include/villas/web.h index f9c720393..42c3f62dd 100644 --- a/include/villas/web.h +++ b/include/villas/web.h @@ -13,6 +13,11 @@ struct api; struct web { struct api *api; + enum { + WEB_STATE_DESTROYED, + WEB_STATE_INITIALIZED + } state; + struct lws_context *context; /**< The libwebsockets server context. */ struct lws_vhost *vhost; /**< The libwebsockets vhost. */ diff --git a/lib/api.c b/lib/api.c index f6a260593..5d8996053 100644 --- a/lib/api.c +++ b/lib/api.c @@ -224,12 +224,18 @@ int api_init(struct api *a, struct cfg *cfg) list_init(&a->sessions); a->cfg = cfg; + a->state = API_STATE_INITIALIZED; return 0; } int api_destroy(struct api *a) { + // if (a->state = API_STATE_INITIALIZED) + // do something + + a->state = API_STATE_DESTROYED; + return 0; } diff --git a/lib/pool.c b/lib/pool.c index bd0eace0a..e80b5bff5 100644 --- a/lib/pool.c +++ b/lib/pool.c @@ -32,6 +32,8 @@ int pool_init(struct pool *p, size_t cnt, size_t blocksz, const struct memtype * for (int i = 0; i < cnt; i++) queue_push(&p->queue, (char *) p->buffer + i * p->blocksz); + + p->state = POOL_STATE_INITIALIZED; return 0; } @@ -40,7 +42,10 @@ int pool_destroy(struct pool *p) { queue_destroy(&p->queue); - return memory_free(p->mem, p->buffer, p->len); + if (p->state == POOL_STATE_INITIALIZED) + return memory_free(p->mem, p->buffer, p->len); + p->state = POOL_STATE_DESTROYED; + return 0; } \ No newline at end of file diff --git a/lib/queue.c b/lib/queue.c index 7862dad4f..a2cc3bcc6 100644 --- a/lib/queue.c +++ b/lib/queue.c @@ -57,12 +57,21 @@ int queue_init(struct queue *q, size_t size, const struct memtype *mem) atomic_store_explicit(&q->tail, 0, memory_order_relaxed); atomic_store_explicit(&q->head, 0, memory_order_relaxed); + q->state = QUEUE_STATE_INITIALIZED; + return 0; } int queue_destroy(struct queue *q) { - return memory_free(q->mem, q->buffer, (q->buffer_mask + 1) * sizeof(q->buffer[0])); + int ret = 0; + + if (q->state == QUEUE_STATE_INITIALIZED) + ret = memory_free(q->mem, q->buffer, (q->buffer_mask + 1) * sizeof(q->buffer[0])); + + q->state = QUEUE_STATE_DESTROYED; + + return ret; } /** Return estimation of current queue usage. diff --git a/lib/web.c b/lib/web.c index ccd81baa6..6c8c218fd 100644 --- a/lib/web.c +++ b/lib/web.c @@ -162,13 +162,18 @@ int web_init(struct web *w, struct api *a) w->vhost = lws_create_vhost(w->context, &vhost_info); if (w->vhost == NULL) error("WebSocket: failed to initialize server"); + + w->state = WEB_STATE_INITIALIZED; return 0; } int web_destroy(struct web *w) { - lws_context_destroy(w->context); + if (w->state == WEB_STATE_INITIALIZED) + lws_context_destroy(w->context); + + w->state = WEB_STATE_DESTROYED; return 0; }