1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/server/src/list.c

71 lines
1.1 KiB
C
Raw Normal View History

/** A generic linked list
*
* Linked lists a used for several data structures in the code.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2015, Institute for Automation of Complex Power Systems, EONERC
* @file
*/
2015-03-18 15:47:49 +01:00
#include "utils.h"
#include "list.h"
void list_init(struct list *l)
{
pthread_mutex_init(&l->lock, NULL);
l->count = 0;
l->head = NULL;
l->tail = NULL;
}
void list_destroy(struct list *l)
{
pthread_mutex_lock(&l->lock);
struct list_elm *elm = l->head;
while (elm) {
struct list_elm *tmp = elm;
free(tmp);
elm = elm->next;
}
2015-03-21 15:20:22 +01:00
pthread_mutex_unlock(&l->lock);
pthread_mutex_destroy(&l->lock);
}
2015-03-18 15:47:49 +01:00
void list_push(struct list *l, void *p)
{
struct list_elm *e = alloc(sizeof(struct list_elm));
pthread_mutex_lock(&l->lock);
2015-03-18 15:47:49 +01:00
e->ptr = p;
e->prev = l->tail;
e->next = NULL;
2015-03-18 15:47:49 +01:00
if (l->tail)
l->tail->next = e;
if (l->head)
l->head->prev = e;
else
l->head = e;
l->tail = e;
2015-03-18 15:47:49 +01:00
l->count++;
pthread_mutex_unlock(&l->lock);
}
struct list_elm * list_search(struct list *l, int (*cmp)(void *))
{
2015-03-18 15:47:49 +01:00
FOREACH(l, it) {
if (!cmp(it->ptr))
return it;
}
return NULL;
}