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>
33 #if defined(MKUZ_DEBUG)
38 #include "mkuz_fqueue.h"
39 #include "mkuz_conveyor.h"
41 #include "mkuz_blk_chain.h"
43 struct mkuz_fifo_queue *
44 mkuz_fqueue_ctor(int wakeup_len)
46 struct mkuz_fifo_queue *fqp;
48 fqp = mkuz_safe_zmalloc(sizeof(struct mkuz_fifo_queue));
49 fqp->wakeup_len = wakeup_len;
50 if (pthread_mutex_init(&fqp->mtx, NULL) != 0) {
51 errx(1, "pthread_mutex_init() failed");
53 if (pthread_cond_init(&fqp->cvar, NULL) != 0) {
54 errx(1, "pthread_cond_init() failed");
60 mkuz_fqueue_enq(struct mkuz_fifo_queue *fqp, struct mkuz_blk *bp)
62 struct mkuz_bchain_link *ip;
64 ip = mkuz_safe_zmalloc(sizeof(struct mkuz_bchain_link));
67 pthread_mutex_lock(&fqp->mtx);
68 if (fqp->first != NULL) {
69 fqp->first->prev = ip;
75 if (fqp->length >= fqp->wakeup_len) {
76 pthread_cond_signal(&fqp->cvar);
78 pthread_mutex_unlock(&fqp->mtx);
83 mkuz_fqueue_enq_all(struct mkuz_fifo_queue *fqp, struct mkuz_bchain_link *cip_f,
84 struct mkuz_bchain_link *cip_l, int clen)
88 pthread_mutex_lock(&fqp->mtx);
89 if (fqp->first != NULL) {
90 fqp->first->prev = cip_l;
97 if (fqp->length >= fqp->wakeup_len) {
98 pthread_cond_signal(&fqp->cvar);
100 pthread_mutex_unlock(&fqp->mtx);
106 mkuz_fqueue_check(struct mkuz_fifo_queue *fqp, cmp_cb_t cmp_cb, void *cap)
108 struct mkuz_bchain_link *ip;
110 for (ip = fqp->last; ip != NULL; ip = ip->prev) {
111 if (cmp_cb(ip->this, cap)) {
119 mkuz_fqueue_deq_when(struct mkuz_fifo_queue *fqp, cmp_cb_t cmp_cb, void *cap)
121 struct mkuz_bchain_link *ip, *newlast, *newfirst, *mip;
124 pthread_mutex_lock(&fqp->mtx);
125 while (fqp->last == NULL || !mkuz_fqueue_check(fqp, cmp_cb, cap)) {
126 pthread_cond_wait(&fqp->cvar, &fqp->mtx);
128 if (cmp_cb(fqp->last->this, cap)) {
130 fqp->last = mip->prev;
131 if (fqp->last == NULL) {
132 #if defined(MKUZ_DEBUG)
133 assert(fqp->length == 1);
138 #if defined(MKUZ_DEBUG)
139 assert(fqp->length > 1);
141 newfirst = newlast = fqp->last;
143 for (ip = fqp->last->prev; ip != NULL; ip = ip->prev) {
144 if (cmp_cb(ip->this, cap)) {
151 newfirst->prev = NULL;
152 fqp->first = newfirst;
156 pthread_mutex_unlock(&fqp->mtx);
164 mkuz_fqueue_deq(struct mkuz_fifo_queue *fqp)
166 struct mkuz_bchain_link *ip;
169 pthread_mutex_lock(&fqp->mtx);
170 while (fqp->last == NULL) {
171 pthread_cond_wait(&fqp->cvar, &fqp->mtx);
173 #if defined(MKUZ_DEBUG)
174 assert(fqp->length > 0);
177 fqp->last = ip->prev;
178 if (fqp->last == NULL) {
179 #if defined(MKUZ_DEBUG)
180 assert(fqp->length == 1);
185 pthread_mutex_unlock(&fqp->mtx);
193 struct mkuz_bchain_link *
194 mkuz_fqueue_deq_all(struct mkuz_fifo_queue *fqp, int *rclen)
196 struct mkuz_bchain_link *rchain;
198 pthread_mutex_lock(&fqp->mtx);
199 while (fqp->last == NULL) {
200 pthread_cond_wait(&fqp->cvar, &fqp->mtx);
202 #if defined(MKUZ_DEBUG)
203 assert(fqp->length > 0);
206 fqp->first = fqp->last = NULL;
207 *rclen = fqp->length;
209 pthread_mutex_unlock(&fqp->mtx);