diff options
Diffstat (limited to 'src/triplet.c')
-rw-r--r-- | src/triplet.c | 99 |
1 files changed, 40 insertions, 59 deletions
diff --git a/src/triplet.c b/src/triplet.c index 9998046..840ecce 100644 --- a/src/triplet.c +++ b/src/triplet.c @@ -24,22 +24,26 @@ static pthread_mutex_t triplet_table_mutex = PTHREAD_MUTEX_INITIALIZER; /* ... and are organized into a doubly-linked list, using the prev and next members of struct wy_triplet: */ struct triplet_list { - struct wy_triplet *head, *tail; + TAILQ_HEAD(, wy_triplet) head; pthread_mutex_t mutex; pthread_cond_t cond; }; -#define TRIPLET_LIST_INITIALIZER \ - { NULL, NULL, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER } +#define TRIPLET_LIST_INITIALIZER(t) \ + { TAILQ_HEAD_INITIALIZER(t.head), \ + PTHREAD_MUTEX_INITIALIZER, \ + PTHREAD_COND_INITIALIZER } /* Two such lists are maintained. The pending list is ordered so that prev points to a triplet older than this one, and next points to a newer triplet. Its head member points to the oldest triplet available. */ -static struct triplet_list triplet_pending_list = TRIPLET_LIST_INITIALIZER; +static struct triplet_list triplet_pending_list = + TRIPLET_LIST_INITIALIZER(triplet_pending_list); /* The running list contains triplets which are processed by running threads */ -static struct triplet_list triplet_running_list = TRIPLET_LIST_INITIALIZER; +static struct triplet_list triplet_running_list = + TRIPLET_LIST_INITIALIZER(triplet_running_list); /* Functions for building the ordered doubly-linked list of triplets */ @@ -60,24 +64,13 @@ triplet_list_unlock(struct triplet_list *list) void triplet_list_unlink(struct triplet_list *list, struct wy_triplet *tp) { - int head_changed = 0; - - if (tp->prev) - tp->prev->next = tp->next; - else if (tp == list->head) { - list->head = tp->next; - head_changed = 1; + if (list) { + int head_changed = tp == TAILQ_FIRST(&list->head); + TAILQ_REMOVE(&list->head, tp, link); + if (head_changed) + pthread_cond_broadcast(&list->cond); + tp->list = NULL; } - - if (tp->next) - tp->next->prev = tp->prev; - else - list->tail = tp->prev; - - tp->next = tp->prev = NULL; - if (head_changed) - pthread_cond_broadcast(&list->cond); - tp->list = NULL; } static void @@ -85,36 +78,23 @@ triplet_list_insert(struct triplet_list *list, struct wy_triplet *newp, struct wy_triplet *anchor, int after) { - int head_changed = 0; + int head_changed = TAILQ_EMPTY(&list->head); - if (!anchor) { - newp->prev = NULL; - newp->next = list->head; - if (list->head) - list->head->prev = newp; + if (after) { + if (!anchor) + TAILQ_INSERT_TAIL(&list->head, newp, link); else - list->tail = newp; - list->head = newp; - head_changed = 1; - } else if (after) { - if (anchor->next) - anchor->next->prev = newp; - else - list->tail = newp; - newp->prev = anchor; - newp->next = anchor->next; - anchor->next = newp; + TAILQ_INSERT_AFTER(&list->head, anchor, newp, link); } else { - if (anchor->prev) - anchor->prev->next = newp; - else { - list->head = newp; + if (!anchor) { head_changed = 1; + TAILQ_INSERT_HEAD(&list->head, newp, link); + } else { + head_changed = anchor == TAILQ_FIRST(&list->head); + TAILQ_INSERT_BEFORE(anchor, newp, link); } - newp->prev = anchor->prev; - newp->next = anchor; - anchor->prev = newp; } + newp->list = list; if (head_changed) pthread_cond_broadcast(&list->cond); @@ -158,14 +138,16 @@ void triplet_list_ordered_insert(struct triplet_list *list, struct wy_triplet *tp) { time_t t = triplet_timestamp(tp); - struct wy_triplet *p, *prev = NULL; + struct wy_triplet *p; - for (p = list->head; p && triplet_timestamp(p) < t; - prev = p, p = p->next); + TAILQ_FOREACH(p, &list->head, link) { + if (triplet_timestamp(p) >= t) + break; + } if (p) triplet_list_insert(list, tp, p, 0); - else - triplet_list_insert(list, tp, prev ? prev->next : NULL, 1); + else + triplet_list_insert(list, tp, NULL, 1); } static struct wy_user * @@ -281,7 +263,7 @@ register_file(struct file_info *finfo, struct spool *spool) ret->spool = spool; ret->acc = grecs_txtacc_create(); ret->report_acc = grecs_txtacc_create(); - } else if (ret->list) + } else triplet_list_unlink(ret->list, ret); triplet_list_ordered_insert(&triplet_pending_list, ret); triplet_list_unlock(&triplet_pending_list); @@ -372,8 +354,7 @@ triplet_enqueue(struct wy_triplet *trp) triplet_list_unlock(&triplet_pending_list); triplet_list_lock(&triplet_running_list); - triplet_list_insert(&triplet_running_list, trp, - triplet_running_list.tail, 1); + triplet_list_insert(&triplet_running_list, trp, NULL, 1); triplet_list_unlock(&triplet_running_list); trp->in_processing = 1; pthread_create(&tid, NULL, wy_thr_triplet, trp); @@ -497,17 +478,17 @@ wy_thr_cleaner(void *ptr) wy_set_cur_thread_name("tricleaner"); triplet_list_lock(&triplet_pending_list); while (1) { - if (triplet_pending_list.head) { + if (!TAILQ_EMPTY(&triplet_pending_list.head)) { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); - triplet_ttl(triplet_pending_list.head, &ts); + triplet_ttl(TAILQ_FIRST(&triplet_pending_list.head), &ts); pthread_cond_timedwait(&triplet_pending_list.cond, &triplet_pending_list.mutex, &ts); } else pthread_cond_wait(&triplet_pending_list.cond, &triplet_pending_list.mutex); - if (triplet_expired_p(triplet_pending_list.head)) - remove_triplet_unlocked(triplet_pending_list.head); + if (triplet_expired_p(TAILQ_FIRST(&triplet_pending_list.head))) + remove_triplet_unlocked(TAILQ_FIRST(&triplet_pending_list.head)); } } @@ -517,7 +498,7 @@ wy_triplet_wait(void) size_t n; triplet_list_lock(&triplet_running_list); - while (triplet_running_list.head) { + while (TAILQ_FIRST(&triplet_running_list.head)) { pthread_cond_wait(&triplet_running_list.cond, &triplet_running_list.mutex); } |