1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

added: insert element in linked list by orderng/priority criteria

This commit is contained in:
Steffen Vogel 2015-06-10 14:56:55 +02:00
parent 4530056e77
commit 517412f7c2
2 changed files with 49 additions and 6 deletions

View file

@ -14,20 +14,22 @@
#include <pthread.h>
#include "hooks.h"
/* Forward declarations */
struct list_elm;
struct node;
struct path;
struct interface;
struct socket;
struct gtfpga;
struct opal;
/** Static list initialization */
#define LIST_INIT { \
#define LIST_INIT(dtor) (struct list) { \
.head = NULL, \
.tail = NULL, \
.length = 0, \
.lock = PTHREAD_MUTEX_INITIALIZER \
.lock = PTHREAD_MUTEX_INITIALIZER, \
.destructor = dtor \
}
#define FOREACH(list, elm) \
@ -65,9 +67,9 @@ struct list_elm {
struct socket *socket;
struct opal *opal;
struct gtfpga *gtfpga;
hook_cb_t hook;
} /* anonymous */;
int priority;
struct list_elm *prev, *next;
};
@ -77,6 +79,8 @@ void list_destroy(struct list *l);
void list_push(struct list *l, void *p);
void list_insert(struct list *l, int prio, void *p);
struct list_elm * list_search(struct list *l, int (*cmp)(void *));
#endif /* _LIST_H_ */

View file

@ -58,12 +58,51 @@ void list_push(struct list *l, void *p)
l->head = e;
l->tail = e;
l->length++;
pthread_mutex_unlock(&l->lock);
}
void list_insert(struct list *l, int prio, void *p)
{
struct list_elm *d;
struct list_elm *e = alloc(sizeof(struct list_elm));
e->priority = prio;
e->ptr = p;
pthread_mutex_lock(&l->lock);
/* Search for first entry with higher priority */
for (d = l->head; d && d->priority < prio; d = d->next);
/* Insert new element in front of d */
e->next = d;
if (d) { /* Between or Head */
e->prev = d->prev;
if (d == l->head) /* Head */
l->head = e;
else /* Between */
d->prev = e;
}
else { /* Tail or Head */
e->prev = l->tail;
if (l->length == 0) /* List was empty */
l->head = e;
else
l->tail->next = e;
l->tail = e;
}
l->length++;
pthread_mutex_unlock(&l->lock);
}
struct list_elm * list_search(struct list *l, int (*cmp)(void *))
{
FOREACH(l, it) {