diff --git a/server/include/list.h b/server/include/list.h index 25095a5fc..c04541fa7 100644 --- a/server/include/list.h +++ b/server/include/list.h @@ -14,20 +14,22 @@ #include -#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_ */ diff --git a/server/src/list.c b/server/src/list.c index d9cd18af8..42fa0a668 100644 --- a/server/src/list.c +++ b/server/src/list.c @@ -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) {