xref: /freebsd/usr.bin/mkuzip/mkuz_fqueue.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1*4fc55e3eSMaxim Sobolev /*
2*4fc55e3eSMaxim Sobolev  * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org>
3*4fc55e3eSMaxim Sobolev  * All rights reserved.
4*4fc55e3eSMaxim Sobolev  *
5*4fc55e3eSMaxim Sobolev  * Redistribution and use in source and binary forms, with or without
6*4fc55e3eSMaxim Sobolev  * modification, are permitted provided that the following conditions
7*4fc55e3eSMaxim Sobolev  * are met:
8*4fc55e3eSMaxim Sobolev  * 1. Redistributions of source code must retain the above copyright
9*4fc55e3eSMaxim Sobolev  *    notice, this list of conditions and the following disclaimer.
10*4fc55e3eSMaxim Sobolev  * 2. Redistributions in binary form must reproduce the above copyright
11*4fc55e3eSMaxim Sobolev  *    notice, this list of conditions and the following disclaimer in the
12*4fc55e3eSMaxim Sobolev  *    documentation and/or other materials provided with the distribution.
13*4fc55e3eSMaxim Sobolev  *
14*4fc55e3eSMaxim Sobolev  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*4fc55e3eSMaxim Sobolev  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*4fc55e3eSMaxim Sobolev  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*4fc55e3eSMaxim Sobolev  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*4fc55e3eSMaxim Sobolev  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*4fc55e3eSMaxim Sobolev  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*4fc55e3eSMaxim Sobolev  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*4fc55e3eSMaxim Sobolev  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*4fc55e3eSMaxim Sobolev  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*4fc55e3eSMaxim Sobolev  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*4fc55e3eSMaxim Sobolev  * SUCH DAMAGE.
25*4fc55e3eSMaxim Sobolev  */
26*4fc55e3eSMaxim Sobolev 
27*4fc55e3eSMaxim Sobolev #include <sys/cdefs.h>
28*4fc55e3eSMaxim Sobolev #include <err.h>
29*4fc55e3eSMaxim Sobolev #include <pthread.h>
30*4fc55e3eSMaxim Sobolev #include <stdint.h>
31*4fc55e3eSMaxim Sobolev #include <stdlib.h>
32*4fc55e3eSMaxim Sobolev 
33*4fc55e3eSMaxim Sobolev #if defined(MKUZ_DEBUG)
34*4fc55e3eSMaxim Sobolev # include <assert.h>
35*4fc55e3eSMaxim Sobolev #endif
36*4fc55e3eSMaxim Sobolev 
37*4fc55e3eSMaxim Sobolev #include "mkuzip.h"
38*4fc55e3eSMaxim Sobolev #include "mkuz_fqueue.h"
39*4fc55e3eSMaxim Sobolev #include "mkuz_conveyor.h"
40*4fc55e3eSMaxim Sobolev #include "mkuz_blk.h"
41*4fc55e3eSMaxim Sobolev #include "mkuz_blk_chain.h"
42*4fc55e3eSMaxim Sobolev 
43*4fc55e3eSMaxim Sobolev struct mkuz_fifo_queue *
mkuz_fqueue_ctor(int wakeup_len)44*4fc55e3eSMaxim Sobolev mkuz_fqueue_ctor(int wakeup_len)
45*4fc55e3eSMaxim Sobolev {
46*4fc55e3eSMaxim Sobolev     struct mkuz_fifo_queue *fqp;
47*4fc55e3eSMaxim Sobolev 
48*4fc55e3eSMaxim Sobolev     fqp = mkuz_safe_zmalloc(sizeof(struct mkuz_fifo_queue));
49*4fc55e3eSMaxim Sobolev     fqp->wakeup_len = wakeup_len;
50*4fc55e3eSMaxim Sobolev     if (pthread_mutex_init(&fqp->mtx, NULL) != 0) {
51*4fc55e3eSMaxim Sobolev         errx(1, "pthread_mutex_init() failed");
52*4fc55e3eSMaxim Sobolev     }
53*4fc55e3eSMaxim Sobolev     if (pthread_cond_init(&fqp->cvar, NULL) != 0) {
54*4fc55e3eSMaxim Sobolev         errx(1, "pthread_cond_init() failed");
55*4fc55e3eSMaxim Sobolev     }
56*4fc55e3eSMaxim Sobolev     return (fqp);
57*4fc55e3eSMaxim Sobolev }
58*4fc55e3eSMaxim Sobolev 
59*4fc55e3eSMaxim Sobolev void
mkuz_fqueue_enq(struct mkuz_fifo_queue * fqp,struct mkuz_blk * bp)60*4fc55e3eSMaxim Sobolev mkuz_fqueue_enq(struct mkuz_fifo_queue *fqp, struct mkuz_blk *bp)
61*4fc55e3eSMaxim Sobolev {
62*4fc55e3eSMaxim Sobolev     struct mkuz_bchain_link *ip;
63*4fc55e3eSMaxim Sobolev 
64*4fc55e3eSMaxim Sobolev     ip = mkuz_safe_zmalloc(sizeof(struct mkuz_bchain_link));
65*4fc55e3eSMaxim Sobolev     ip->this = bp;
66*4fc55e3eSMaxim Sobolev 
67*4fc55e3eSMaxim Sobolev     pthread_mutex_lock(&fqp->mtx);
68*4fc55e3eSMaxim Sobolev     if (fqp->first != NULL) {
69*4fc55e3eSMaxim Sobolev         fqp->first->prev = ip;
70*4fc55e3eSMaxim Sobolev     } else {
71*4fc55e3eSMaxim Sobolev         fqp->last = ip;
72*4fc55e3eSMaxim Sobolev     }
73*4fc55e3eSMaxim Sobolev     fqp->first = ip;
74*4fc55e3eSMaxim Sobolev     fqp->length += 1;
75*4fc55e3eSMaxim Sobolev     if (fqp->length >= fqp->wakeup_len) {
76*4fc55e3eSMaxim Sobolev         pthread_cond_signal(&fqp->cvar);
77*4fc55e3eSMaxim Sobolev     }
78*4fc55e3eSMaxim Sobolev     pthread_mutex_unlock(&fqp->mtx);
79*4fc55e3eSMaxim Sobolev }
80*4fc55e3eSMaxim Sobolev 
81*4fc55e3eSMaxim Sobolev #if defined(NOTYET)
82*4fc55e3eSMaxim Sobolev int
mkuz_fqueue_enq_all(struct mkuz_fifo_queue * fqp,struct mkuz_bchain_link * cip_f,struct mkuz_bchain_link * cip_l,int clen)83*4fc55e3eSMaxim Sobolev mkuz_fqueue_enq_all(struct mkuz_fifo_queue *fqp, struct mkuz_bchain_link *cip_f,
84*4fc55e3eSMaxim Sobolev   struct mkuz_bchain_link *cip_l, int clen)
85*4fc55e3eSMaxim Sobolev {
86*4fc55e3eSMaxim Sobolev     int rval;
87*4fc55e3eSMaxim Sobolev 
88*4fc55e3eSMaxim Sobolev     pthread_mutex_lock(&fqp->mtx);
89*4fc55e3eSMaxim Sobolev     if (fqp->first != NULL) {
90*4fc55e3eSMaxim Sobolev         fqp->first->prev = cip_l;
91*4fc55e3eSMaxim Sobolev     } else {
92*4fc55e3eSMaxim Sobolev         fqp->last = cip_l;
93*4fc55e3eSMaxim Sobolev     }
94*4fc55e3eSMaxim Sobolev     fqp->first = cip_f;
95*4fc55e3eSMaxim Sobolev     fqp->length += clen;
96*4fc55e3eSMaxim Sobolev     rval = fqp->length;
97*4fc55e3eSMaxim Sobolev     if (fqp->length >= fqp->wakeup_len) {
98*4fc55e3eSMaxim Sobolev         pthread_cond_signal(&fqp->cvar);
99*4fc55e3eSMaxim Sobolev     }
100*4fc55e3eSMaxim Sobolev     pthread_mutex_unlock(&fqp->mtx);
101*4fc55e3eSMaxim Sobolev     return (rval);
102*4fc55e3eSMaxim Sobolev }
103*4fc55e3eSMaxim Sobolev #endif
104*4fc55e3eSMaxim Sobolev 
105*4fc55e3eSMaxim Sobolev static int
mkuz_fqueue_check(struct mkuz_fifo_queue * fqp,cmp_cb_t cmp_cb,void * cap)106*4fc55e3eSMaxim Sobolev mkuz_fqueue_check(struct mkuz_fifo_queue *fqp, cmp_cb_t cmp_cb, void *cap)
107*4fc55e3eSMaxim Sobolev {
108*4fc55e3eSMaxim Sobolev     struct mkuz_bchain_link *ip;
109*4fc55e3eSMaxim Sobolev 
110*4fc55e3eSMaxim Sobolev     for (ip = fqp->last; ip != NULL; ip = ip->prev) {
111*4fc55e3eSMaxim Sobolev         if (cmp_cb(ip->this, cap)) {
112*4fc55e3eSMaxim Sobolev             return (1);
113*4fc55e3eSMaxim Sobolev         }
114*4fc55e3eSMaxim Sobolev     }
115*4fc55e3eSMaxim Sobolev     return (0);
116*4fc55e3eSMaxim Sobolev }
117*4fc55e3eSMaxim Sobolev 
118*4fc55e3eSMaxim Sobolev struct mkuz_blk *
mkuz_fqueue_deq_when(struct mkuz_fifo_queue * fqp,cmp_cb_t cmp_cb,void * cap)119*4fc55e3eSMaxim Sobolev mkuz_fqueue_deq_when(struct mkuz_fifo_queue *fqp, cmp_cb_t cmp_cb, void *cap)
120*4fc55e3eSMaxim Sobolev {
121*4fc55e3eSMaxim Sobolev     struct mkuz_bchain_link *ip, *newlast, *newfirst, *mip;
122*4fc55e3eSMaxim Sobolev     struct mkuz_blk *bp;
123*4fc55e3eSMaxim Sobolev 
124*4fc55e3eSMaxim Sobolev     pthread_mutex_lock(&fqp->mtx);
125*4fc55e3eSMaxim Sobolev     while (fqp->last == NULL || !mkuz_fqueue_check(fqp, cmp_cb, cap)) {
126*4fc55e3eSMaxim Sobolev         pthread_cond_wait(&fqp->cvar, &fqp->mtx);
127*4fc55e3eSMaxim Sobolev     }
128*4fc55e3eSMaxim Sobolev     if (cmp_cb(fqp->last->this, cap)) {
129*4fc55e3eSMaxim Sobolev         mip = fqp->last;
130*4fc55e3eSMaxim Sobolev         fqp->last = mip->prev;
131*4fc55e3eSMaxim Sobolev         if (fqp->last == NULL) {
132*4fc55e3eSMaxim Sobolev #if defined(MKUZ_DEBUG)
133*4fc55e3eSMaxim Sobolev             assert(fqp->length == 1);
134*4fc55e3eSMaxim Sobolev #endif
135*4fc55e3eSMaxim Sobolev             fqp->first = NULL;
136*4fc55e3eSMaxim Sobolev         }
137*4fc55e3eSMaxim Sobolev     } else {
138*4fc55e3eSMaxim Sobolev #if defined(MKUZ_DEBUG)
139*4fc55e3eSMaxim Sobolev         assert(fqp->length > 1);
140*4fc55e3eSMaxim Sobolev #endif
141*4fc55e3eSMaxim Sobolev         newfirst = newlast = fqp->last;
142*4fc55e3eSMaxim Sobolev         mip = NULL;
143*4fc55e3eSMaxim Sobolev         for (ip = fqp->last->prev; ip != NULL; ip = ip->prev) {
144*4fc55e3eSMaxim Sobolev             if (cmp_cb(ip->this, cap)) {
145*4fc55e3eSMaxim Sobolev                 mip = ip;
146*4fc55e3eSMaxim Sobolev                 continue;
147*4fc55e3eSMaxim Sobolev             }
148*4fc55e3eSMaxim Sobolev             newfirst->prev = ip;
149*4fc55e3eSMaxim Sobolev             newfirst = ip;
150*4fc55e3eSMaxim Sobolev         }
151*4fc55e3eSMaxim Sobolev         newfirst->prev = NULL;
152*4fc55e3eSMaxim Sobolev         fqp->first = newfirst;
153*4fc55e3eSMaxim Sobolev         fqp->last = newlast;
154*4fc55e3eSMaxim Sobolev     }
155*4fc55e3eSMaxim Sobolev     fqp->length -= 1;
156*4fc55e3eSMaxim Sobolev     pthread_mutex_unlock(&fqp->mtx);
157*4fc55e3eSMaxim Sobolev     bp = mip->this;
158*4fc55e3eSMaxim Sobolev     free(mip);
159*4fc55e3eSMaxim Sobolev 
160*4fc55e3eSMaxim Sobolev     return bp;
161*4fc55e3eSMaxim Sobolev }
162*4fc55e3eSMaxim Sobolev 
163*4fc55e3eSMaxim Sobolev struct mkuz_blk *
mkuz_fqueue_deq(struct mkuz_fifo_queue * fqp)164*4fc55e3eSMaxim Sobolev mkuz_fqueue_deq(struct mkuz_fifo_queue *fqp)
165*4fc55e3eSMaxim Sobolev {
166*4fc55e3eSMaxim Sobolev     struct mkuz_bchain_link *ip;
167*4fc55e3eSMaxim Sobolev     struct mkuz_blk *bp;
168*4fc55e3eSMaxim Sobolev 
169*4fc55e3eSMaxim Sobolev     pthread_mutex_lock(&fqp->mtx);
170*4fc55e3eSMaxim Sobolev     while (fqp->last == NULL) {
171*4fc55e3eSMaxim Sobolev         pthread_cond_wait(&fqp->cvar, &fqp->mtx);
172*4fc55e3eSMaxim Sobolev     }
173*4fc55e3eSMaxim Sobolev #if defined(MKUZ_DEBUG)
174*4fc55e3eSMaxim Sobolev     assert(fqp->length > 0);
175*4fc55e3eSMaxim Sobolev #endif
176*4fc55e3eSMaxim Sobolev     ip = fqp->last;
177*4fc55e3eSMaxim Sobolev     fqp->last = ip->prev;
178*4fc55e3eSMaxim Sobolev     if (fqp->last == NULL) {
179*4fc55e3eSMaxim Sobolev #if defined(MKUZ_DEBUG)
180*4fc55e3eSMaxim Sobolev         assert(fqp->length == 1);
181*4fc55e3eSMaxim Sobolev #endif
182*4fc55e3eSMaxim Sobolev         fqp->first = NULL;
183*4fc55e3eSMaxim Sobolev     }
184*4fc55e3eSMaxim Sobolev     fqp->length -= 1;
185*4fc55e3eSMaxim Sobolev     pthread_mutex_unlock(&fqp->mtx);
186*4fc55e3eSMaxim Sobolev     bp = ip->this;
187*4fc55e3eSMaxim Sobolev     free(ip);
188*4fc55e3eSMaxim Sobolev 
189*4fc55e3eSMaxim Sobolev     return bp;
190*4fc55e3eSMaxim Sobolev }
191*4fc55e3eSMaxim Sobolev 
192*4fc55e3eSMaxim Sobolev #if defined(NOTYET)
193*4fc55e3eSMaxim Sobolev struct mkuz_bchain_link *
mkuz_fqueue_deq_all(struct mkuz_fifo_queue * fqp,int * rclen)194*4fc55e3eSMaxim Sobolev mkuz_fqueue_deq_all(struct mkuz_fifo_queue *fqp, int *rclen)
195*4fc55e3eSMaxim Sobolev {
196*4fc55e3eSMaxim Sobolev     struct mkuz_bchain_link *rchain;
197*4fc55e3eSMaxim Sobolev 
198*4fc55e3eSMaxim Sobolev     pthread_mutex_lock(&fqp->mtx);
199*4fc55e3eSMaxim Sobolev     while (fqp->last == NULL) {
200*4fc55e3eSMaxim Sobolev         pthread_cond_wait(&fqp->cvar, &fqp->mtx);
201*4fc55e3eSMaxim Sobolev     }
202*4fc55e3eSMaxim Sobolev #if defined(MKUZ_DEBUG)
203*4fc55e3eSMaxim Sobolev     assert(fqp->length > 0);
204*4fc55e3eSMaxim Sobolev #endif
205*4fc55e3eSMaxim Sobolev     rchain = fqp->last;
206*4fc55e3eSMaxim Sobolev     fqp->first = fqp->last = NULL;
207*4fc55e3eSMaxim Sobolev     *rclen = fqp->length;
208*4fc55e3eSMaxim Sobolev     fqp->length = 0;
209*4fc55e3eSMaxim Sobolev     pthread_mutex_unlock(&fqp->mtx);
210*4fc55e3eSMaxim Sobolev     return (rchain);
211*4fc55e3eSMaxim Sobolev }
212*4fc55e3eSMaxim Sobolev #endif
213