Skip to content

Commit 2d4c58c

Browse files
committed
waitlist.c: use list.h
1 parent 04539d9 commit 2d4c58c

File tree

1 file changed

+10
-37
lines changed

1 file changed

+10
-37
lines changed

lib/waitlist.c

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string.h>
99
#include <time.h>
1010

11+
#include "list.h"
1112
#include "waitlist.h"
1213
#include "xlog.h"
1314

@@ -20,16 +21,14 @@ static struct atomics_mutex g_atomics_lock = {
2021
};
2122

2223
struct waiter {
23-
struct waiter *next;
24-
struct waiter **prevnextp;
24+
LIST_ENTRY(struct waiter) e;
2525
pthread_cond_t cv;
2626
bool woken;
2727
};
2828

2929
struct waiter_list {
3030
struct waiter_list *next;
31-
struct waiter *waiters;
32-
struct waiter **waiters_tailnextp;
31+
LIST_HEAD(struct waiter) waiters;
3332
uint32_t ident;
3433
uint32_t nwaiters;
3534
};
@@ -73,8 +72,7 @@ waiter_list_lookup(struct waiter_list_table *tab, uint32_t ident,
7372
if (allocate) {
7473
l = malloc(sizeof(*l));
7574
if (l != NULL) {
76-
l->waiters = NULL;
77-
l->waiters_tailnextp = &l->waiters;
75+
LIST_HEAD_INIT(&l->waiters);
7876
l->ident = ident;
7977
l->nwaiters = 0;
8078
l->next = *headp;
@@ -99,8 +97,7 @@ waiter_list_free(struct waiter_list_table *tab, struct waiter_list *l1)
9997
}
10098
assert(l != NULL);
10199
assert(l->nwaiters == 0);
102-
assert(l->waiters == NULL);
103-
assert(l->waiters_tailnextp == &l->waiters);
100+
assert(LIST_EMPTY(&l->waiters));
104101
*pp = l->next;
105102
free(l);
106103
}
@@ -130,31 +127,14 @@ static void
130127
waiter_remove(struct waiter_list *l, struct waiter *w)
131128
{
132129
assert(l->nwaiters > 0);
133-
assert(l->waiters != NULL);
134-
assert(*l->waiters_tailnextp == NULL);
135-
assert(*w->prevnextp == w);
136-
*w->prevnextp = w->next;
137-
if (w->next == NULL) {
138-
assert(l->waiters_tailnextp == &w->next);
139-
l->waiters_tailnextp = w->prevnextp;
140-
} else {
141-
assert(w->next->prevnextp == &w->next);
142-
w->next->prevnextp = w->prevnextp;
143-
}
144-
assert(*l->waiters_tailnextp == NULL);
130+
LIST_REMOVE(&l->waiters, w, e);
145131
l->nwaiters--;
146132
}
147133

148134
static void
149135
waiter_insert_tail(struct waiter_list *l, struct waiter *w)
150136
{
151-
assert(*l->waiters_tailnextp == NULL);
152-
w->prevnextp = l->waiters_tailnextp;
153-
*l->waiters_tailnextp = w;
154-
l->waiters_tailnextp = &w->next;
155-
w->next = NULL;
156-
assert(*l->waiters_tailnextp == NULL);
157-
assert(*w->prevnextp == w);
137+
LIST_INSERT_TAIL(&l->waiters, w, e);
158138
l->nwaiters++;
159139
}
160140

@@ -200,26 +180,19 @@ atomics_notify(struct waiter_list_table *tab, uint32_t ident, uint32_t count)
200180
if (l == NULL) {
201181
return 0;
202182
}
203-
assert(l->nwaiters > 0);
204-
assert(*l->waiters_tailnextp == NULL);
183+
assert(!LIST_EMPTY(&l->waiters));
205184
struct waiter *w;
206-
struct waiter *next = l->waiters;
207185
uint32_t left = count;
208-
for (w = next; left > 0 && w != NULL; w = next) {
186+
while (left > 0 && (w = LIST_FIRST(&l->waiters)) != NULL) {
209187
left--;
210-
next = w->next;
188+
LIST_REMOVE(&l->waiters, w, e);
211189
waiter_wakeup(l, lock, w);
212190
}
213191
assert(left <= count);
214192
uint32_t nwoken = count - left;
215193
assert(nwoken <= count);
216194
assert(nwoken <= l->nwaiters);
217195
l->nwaiters -= nwoken;
218-
l->waiters = next;
219-
if (next == NULL) {
220-
l->waiters_tailnextp = &l->waiters;
221-
}
222-
assert(*l->waiters_tailnextp == NULL);
223196
if (l->nwaiters == 0) {
224197
waiter_list_free(tab, l);
225198
}

0 commit comments

Comments
 (0)