2015-03-18 00:31:48 +01:00
|
|
|
/** 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"
|
2015-03-18 00:31:48 +01:00
|
|
|
#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);
|
2015-03-18 00:31:48 +01:00
|
|
|
pthread_mutex_destroy(&l->lock);
|
|
|
|
}
|
|
|
|
|
2015-03-18 15:47:49 +01:00
|
|
|
void list_push(struct list *l, void *p)
|
2015-03-18 00:31:48 +01:00
|
|
|
{
|
|
|
|
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;
|
2015-03-18 00:31:48 +01:00
|
|
|
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;
|
|
|
|
|
2015-03-18 00:31:48 +01:00
|
|
|
l->tail = e;
|
|
|
|
|
2015-03-18 15:47:49 +01:00
|
|
|
l->count++;
|
|
|
|
|
2015-03-18 00:31:48 +01:00
|
|
|
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))
|
2015-03-18 00:31:48 +01:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|