2 * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
35 #if defined(MKUZ_DEBUG)
40 #include "mkuz_fqueue.h"
41 #include "mkuz_conveyor.h"
43 #include "mkuz_blk_chain.h"
45 struct mkuz_fifo_queue *
46 mkuz_fqueue_ctor(int wakeup_len)
48 struct mkuz_fifo_queue *fqp;
50 fqp = mkuz_safe_zmalloc(sizeof(struct mkuz_fifo_queue));
51 fqp->wakeup_len = wakeup_len;
52 if (pthread_mutex_init(&fqp->mtx, NULL) != 0) {
53 errx(1, "pthread_mutex_init() failed");
55 if (pthread_cond_init(&fqp->cvar, NULL) != 0) {
56 errx(1, "pthread_cond_init() failed");
62 mkuz_fqueue_enq(struct mkuz_fifo_queue *fqp, struct mkuz_blk *bp)
64 struct mkuz_bchain_link *ip;
66 ip = mkuz_safe_zmalloc(sizeof(struct mkuz_bchain_link));
69 pthread_mutex_lock(&fqp->mtx);
70 if (fqp->first != NULL) {
71 fqp->first->prev = ip;
77 if (fqp->length >= fqp->wakeup_len) {
78 pthread_cond_signal(&fqp->cvar);
80 pthread_mutex_unlock(&fqp->mtx);
85 mkuz_fqueue_enq_all(struct mkuz_fifo_queue *fqp, struct mkuz_bchain_link *cip_f,
86 struct mkuz_bchain_link *cip_l, int clen)
90 pthread_mutex_lock(&fqp->mtx);
91 if (fqp->first != NULL) {
92 fqp->first->prev = cip_l;
99 if (fqp->length >= fqp->wakeup_len) {
100 pthread_cond_signal(&fqp->cvar);
102 pthread_mutex_unlock(&fqp->mtx);
108 mkuz_fqueue_check(struct mkuz_fifo_queue *fqp, cmp_cb_t cmp_cb, void *cap)
110 struct mkuz_bchain_link *ip;
112 for (ip = fqp->last; ip != NULL; ip = ip->prev) {
113 if (cmp_cb(ip->this, cap)) {
121 mkuz_fqueue_deq_when(struct mkuz_fifo_queue *fqp, cmp_cb_t cmp_cb, void *cap)
123 struct mkuz_bchain_link *ip, *newlast, *newfirst, *mip;
126 pthread_mutex_lock(&fqp->mtx);
127 while (fqp->last == NULL || !mkuz_fqueue_check(fqp, cmp_cb, cap)) {
128 pthread_cond_wait(&fqp->cvar, &fqp->mtx);
130 if (cmp_cb(fqp->last->this, cap)) {
132 fqp->last = mip->prev;
133 if (fqp->last == NULL) {
134 #if defined(MKUZ_DEBUG)
135 assert(fqp->length == 1);
140 #if defined(MKUZ_DEBUG)
141 assert(fqp->length > 1);
143 newfirst = newlast = fqp->last;
145 for (ip = fqp->last->prev; ip != NULL; ip = ip->prev) {
146 if (cmp_cb(ip->this, cap)) {
153 newfirst->prev = NULL;
154 fqp->first = newfirst;
158 pthread_mutex_unlock(&fqp->mtx);
166 mkuz_fqueue_deq(struct mkuz_fifo_queue *fqp)
168 struct mkuz_bchain_link *ip;
171 pthread_mutex_lock(&fqp->mtx);
172 while (fqp->last == NULL) {
173 pthread_cond_wait(&fqp->cvar, &fqp->mtx);
175 #if defined(MKUZ_DEBUG)
176 assert(fqp->length > 0);
179 fqp->last = ip->prev;
180 if (fqp->last == NULL) {
181 #if defined(MKUZ_DEBUG)
182 assert(fqp->length == 1);
187 pthread_mutex_unlock(&fqp->mtx);
195 struct mkuz_bchain_link *
196 mkuz_fqueue_deq_all(struct mkuz_fifo_queue *fqp, int *rclen)
198 struct mkuz_bchain_link *rchain;
200 pthread_mutex_lock(&fqp->mtx);
201 while (fqp->last == NULL) {
202 pthread_cond_wait(&fqp->cvar, &fqp->mtx);
204 #if defined(MKUZ_DEBUG)
205 assert(fqp->length > 0);
208 fqp->first = fqp->last = NULL;
209 *rclen = fqp->length;
211 pthread_mutex_unlock(&fqp->mtx);