1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * A synchronized FIFO queue for inter-thread producer-consumer semantics. 31*7c478bd9Sstevel@tonic-gate * This queue will handle multiple writers and readers simultaneously. 32*7c478bd9Sstevel@tonic-gate * 33*7c478bd9Sstevel@tonic-gate * The following operations are provided: 34*7c478bd9Sstevel@tonic-gate * slp_new_queue: create a new queue 35*7c478bd9Sstevel@tonic-gate * slp_enqueue: place a message at the end of the queue 36*7c478bd9Sstevel@tonic-gate * slp_enqueue_at_head: place a message the the start of the queue 37*7c478bd9Sstevel@tonic-gate * slp_dequeue: remove and return the next message on the queue 38*7c478bd9Sstevel@tonic-gate * (waits indefinately) 39*7c478bd9Sstevel@tonic-gate * slp_dequeue_timed: remove and return the next message on the queue 40*7c478bd9Sstevel@tonic-gate * (waits only for a specified time) 41*7c478bd9Sstevel@tonic-gate * slp_flush_queue: flushes and frees all messages on a queue 42*7c478bd9Sstevel@tonic-gate * slp_destroy_queue: frees an empty queue. 43*7c478bd9Sstevel@tonic-gate */ 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate #include <stdio.h> 46*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 47*7c478bd9Sstevel@tonic-gate #include <thread.h> 48*7c478bd9Sstevel@tonic-gate #include <synch.h> 49*7c478bd9Sstevel@tonic-gate #include <syslog.h> 50*7c478bd9Sstevel@tonic-gate #include <slp.h> 51*7c478bd9Sstevel@tonic-gate #include <slp-internal.h> 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate /* Private implementation details */ 54*7c478bd9Sstevel@tonic-gate struct queue_entry { 55*7c478bd9Sstevel@tonic-gate void *msg; 56*7c478bd9Sstevel@tonic-gate struct queue_entry *next; 57*7c478bd9Sstevel@tonic-gate }; 58*7c478bd9Sstevel@tonic-gate typedef struct queue_entry slp_queue_entry_t; 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate struct queue { 61*7c478bd9Sstevel@tonic-gate slp_queue_entry_t *head; 62*7c478bd9Sstevel@tonic-gate slp_queue_entry_t *tail; 63*7c478bd9Sstevel@tonic-gate mutex_t *lock; 64*7c478bd9Sstevel@tonic-gate cond_t *wait; 65*7c478bd9Sstevel@tonic-gate int count; 66*7c478bd9Sstevel@tonic-gate }; 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate /* 69*7c478bd9Sstevel@tonic-gate * Creates, initializes, and returns a new queue. 70*7c478bd9Sstevel@tonic-gate * If an initialization error occured, returns NULL and sets err to 71*7c478bd9Sstevel@tonic-gate * the appropriate SLP error code. 72*7c478bd9Sstevel@tonic-gate * queues can operate in one of two modes: timed-wait, and infinite 73*7c478bd9Sstevel@tonic-gate * wait. The timeout parameter specifies which of these modes should 74*7c478bd9Sstevel@tonic-gate * be enabled for the new queue. 75*7c478bd9Sstevel@tonic-gate */ 76*7c478bd9Sstevel@tonic-gate slp_queue_t *slp_new_queue(SLPError *err) { 77*7c478bd9Sstevel@tonic-gate mutex_t *lock; 78*7c478bd9Sstevel@tonic-gate cond_t *wait; 79*7c478bd9Sstevel@tonic-gate struct queue *q; 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate *err = SLP_OK; 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate /* initialize new mutex and semaphore */ 84*7c478bd9Sstevel@tonic-gate if ((lock = calloc(1, sizeof (*lock))) == NULL) { 85*7c478bd9Sstevel@tonic-gate *err = SLP_MEMORY_ALLOC_FAILED; 86*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_new_queue", "out of memory"); 87*7c478bd9Sstevel@tonic-gate return (NULL); 88*7c478bd9Sstevel@tonic-gate } 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate /* intialize condition vars */ 91*7c478bd9Sstevel@tonic-gate if (!(wait = calloc(1, sizeof (*wait)))) { 92*7c478bd9Sstevel@tonic-gate *err = SLP_MEMORY_ALLOC_FAILED; 93*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_new_queue", "out of memory"); 94*7c478bd9Sstevel@tonic-gate return (NULL); 95*7c478bd9Sstevel@tonic-gate } 96*7c478bd9Sstevel@tonic-gate (void) cond_init(wait, NULL, NULL); 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate /* create the queue */ 99*7c478bd9Sstevel@tonic-gate if ((q = malloc(sizeof (*q))) == NULL) { 100*7c478bd9Sstevel@tonic-gate *err = SLP_MEMORY_ALLOC_FAILED; 101*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_new_queue", "out of memory"); 102*7c478bd9Sstevel@tonic-gate return (NULL); 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate q->head = NULL; 106*7c478bd9Sstevel@tonic-gate q->lock = lock; 107*7c478bd9Sstevel@tonic-gate q->wait = wait; 108*7c478bd9Sstevel@tonic-gate q->count = 0; 109*7c478bd9Sstevel@tonic-gate 110*7c478bd9Sstevel@tonic-gate return (q); 111*7c478bd9Sstevel@tonic-gate } 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate /* 114*7c478bd9Sstevel@tonic-gate * Adds msg to the tail of queue q. 115*7c478bd9Sstevel@tonic-gate * Returns an SLP error code: SLP_OK for no error, or SLP_MEMORY_ALLOC_FAILED 116*7c478bd9Sstevel@tonic-gate * if it couldn't allocate memory. 117*7c478bd9Sstevel@tonic-gate */ 118*7c478bd9Sstevel@tonic-gate SLPError slp_enqueue(slp_queue_t *qa, void *msg) { 119*7c478bd9Sstevel@tonic-gate slp_queue_entry_t *qe; 120*7c478bd9Sstevel@tonic-gate struct queue *q = qa; 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate if ((qe = malloc(sizeof (*qe))) == NULL) { 123*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_enqueue", "out of memory"); 124*7c478bd9Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED); 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate (void) mutex_lock(q->lock); 128*7c478bd9Sstevel@tonic-gate qe->msg = msg; 129*7c478bd9Sstevel@tonic-gate qe->next = NULL; 130*7c478bd9Sstevel@tonic-gate if (q->head != NULL) { /* queue is not emptry */ 131*7c478bd9Sstevel@tonic-gate q->tail->next = qe; 132*7c478bd9Sstevel@tonic-gate q->tail = qe; 133*7c478bd9Sstevel@tonic-gate } else { /* queue is empty */ 134*7c478bd9Sstevel@tonic-gate q->head = q->tail = qe; 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate q->count++; 137*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(q->lock); 138*7c478bd9Sstevel@tonic-gate (void) cond_signal(q->wait); 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate return (SLP_OK); 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate /* 144*7c478bd9Sstevel@tonic-gate * Inserts a message at the head of the queue. This is useful for inserting 145*7c478bd9Sstevel@tonic-gate * things like cancel messages. 146*7c478bd9Sstevel@tonic-gate */ 147*7c478bd9Sstevel@tonic-gate SLPError slp_enqueue_at_head(slp_queue_t *qa, void *msg) { 148*7c478bd9Sstevel@tonic-gate slp_queue_entry_t *qe; 149*7c478bd9Sstevel@tonic-gate struct queue *q = qa; 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate if ((qe = malloc(sizeof (*qe))) == NULL) { 152*7c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_enqueue", "out of memory"); 153*7c478bd9Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED); 154*7c478bd9Sstevel@tonic-gate } 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate (void) mutex_lock(q->lock); 157*7c478bd9Sstevel@tonic-gate qe->msg = msg; 158*7c478bd9Sstevel@tonic-gate qe->next = q->head; 159*7c478bd9Sstevel@tonic-gate q->head = qe; 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate q->count++; 162*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(q->lock); 163*7c478bd9Sstevel@tonic-gate (void) cond_signal(q->wait); 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate return (SLP_OK); 166*7c478bd9Sstevel@tonic-gate } 167*7c478bd9Sstevel@tonic-gate 168*7c478bd9Sstevel@tonic-gate /* 169*7c478bd9Sstevel@tonic-gate * The core functionality for dequeue. 170*7c478bd9Sstevel@tonic-gate */ 171*7c478bd9Sstevel@tonic-gate static void *dequeue_nolock(struct queue *q) { 172*7c478bd9Sstevel@tonic-gate void *msg; 173*7c478bd9Sstevel@tonic-gate slp_queue_entry_t *qe = q->head; 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate if (!qe) 176*7c478bd9Sstevel@tonic-gate return (NULL); /* shouldn't get here */ 177*7c478bd9Sstevel@tonic-gate msg = qe->msg; 178*7c478bd9Sstevel@tonic-gate if (!qe->next) /* last one in queue */ 179*7c478bd9Sstevel@tonic-gate q->head = q->tail = NULL; 180*7c478bd9Sstevel@tonic-gate else 181*7c478bd9Sstevel@tonic-gate q->head = qe->next; 182*7c478bd9Sstevel@tonic-gate free(qe); 183*7c478bd9Sstevel@tonic-gate q->count--; 184*7c478bd9Sstevel@tonic-gate return (msg); 185*7c478bd9Sstevel@tonic-gate } 186*7c478bd9Sstevel@tonic-gate 187*7c478bd9Sstevel@tonic-gate /* 188*7c478bd9Sstevel@tonic-gate * Returns the first message waiting or arriving in the queue, or if no 189*7c478bd9Sstevel@tonic-gate * message is available after waiting the amount of time specified in 190*7c478bd9Sstevel@tonic-gate * 'to', returns NULL, and sets 'etimed' to true. If an error occured, 191*7c478bd9Sstevel@tonic-gate * returns NULL and sets 'etimed' to false. 192*7c478bd9Sstevel@tonic-gate */ 193*7c478bd9Sstevel@tonic-gate void *slp_dequeue_timed(slp_queue_t *qa, timestruc_t *to, SLPBoolean *etimed) { 194*7c478bd9Sstevel@tonic-gate int err; 195*7c478bd9Sstevel@tonic-gate void *ans; 196*7c478bd9Sstevel@tonic-gate struct queue *q = qa; 197*7c478bd9Sstevel@tonic-gate 198*7c478bd9Sstevel@tonic-gate if (etimed) 199*7c478bd9Sstevel@tonic-gate *etimed = SLP_FALSE; 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate (void) mutex_lock(q->lock); 202*7c478bd9Sstevel@tonic-gate if (q->count > 0) { 203*7c478bd9Sstevel@tonic-gate /* something's in the q, so no need to wait */ 204*7c478bd9Sstevel@tonic-gate goto msg_available; 205*7c478bd9Sstevel@tonic-gate } 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate /* else wait */ 208*7c478bd9Sstevel@tonic-gate while (q->count == 0) { 209*7c478bd9Sstevel@tonic-gate if (to) { 210*7c478bd9Sstevel@tonic-gate err = cond_timedwait(q->wait, q->lock, to); 211*7c478bd9Sstevel@tonic-gate } else { 212*7c478bd9Sstevel@tonic-gate err = cond_wait(q->wait, q->lock); 213*7c478bd9Sstevel@tonic-gate } 214*7c478bd9Sstevel@tonic-gate if (err == ETIME) { 215*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(q->lock); 216*7c478bd9Sstevel@tonic-gate *etimed = SLP_TRUE; 217*7c478bd9Sstevel@tonic-gate return (NULL); 218*7c478bd9Sstevel@tonic-gate } 219*7c478bd9Sstevel@tonic-gate } 220*7c478bd9Sstevel@tonic-gate 221*7c478bd9Sstevel@tonic-gate msg_available: 222*7c478bd9Sstevel@tonic-gate ans = dequeue_nolock(q); 223*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(q->lock); 224*7c478bd9Sstevel@tonic-gate return (ans); 225*7c478bd9Sstevel@tonic-gate } 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate /* 228*7c478bd9Sstevel@tonic-gate * Removes the first message from the queue and returns it. 229*7c478bd9Sstevel@tonic-gate * Returns NULL only on internal error. 230*7c478bd9Sstevel@tonic-gate */ 231*7c478bd9Sstevel@tonic-gate void *slp_dequeue(slp_queue_t *qa) { 232*7c478bd9Sstevel@tonic-gate return (slp_dequeue_timed(qa, NULL, NULL)); 233*7c478bd9Sstevel@tonic-gate } 234*7c478bd9Sstevel@tonic-gate 235*7c478bd9Sstevel@tonic-gate /* 236*7c478bd9Sstevel@tonic-gate * Flushes the queue, using the caller-specified free function to 237*7c478bd9Sstevel@tonic-gate * free each message in the queue. 238*7c478bd9Sstevel@tonic-gate */ 239*7c478bd9Sstevel@tonic-gate void slp_flush_queue(slp_queue_t *qa, void (*free_f)(void *)) { 240*7c478bd9Sstevel@tonic-gate slp_queue_entry_t *p, *pn; 241*7c478bd9Sstevel@tonic-gate struct queue *q = qa; 242*7c478bd9Sstevel@tonic-gate 243*7c478bd9Sstevel@tonic-gate for (p = q->head; p; p = pn) { 244*7c478bd9Sstevel@tonic-gate pn = p->next; 245*7c478bd9Sstevel@tonic-gate free_f(p); 246*7c478bd9Sstevel@tonic-gate } 247*7c478bd9Sstevel@tonic-gate } 248*7c478bd9Sstevel@tonic-gate 249*7c478bd9Sstevel@tonic-gate /* 250*7c478bd9Sstevel@tonic-gate * Frees a queue. 251*7c478bd9Sstevel@tonic-gate * The queue must be empty before it can be destroyed; slp_flush_queue 252*7c478bd9Sstevel@tonic-gate * can be used to empty a queue. 253*7c478bd9Sstevel@tonic-gate */ 254*7c478bd9Sstevel@tonic-gate void slp_destroy_queue(slp_queue_t *qa) { 255*7c478bd9Sstevel@tonic-gate struct queue *q = qa; 256*7c478bd9Sstevel@tonic-gate 257*7c478bd9Sstevel@tonic-gate (void) mutex_destroy(q->lock); 258*7c478bd9Sstevel@tonic-gate (void) cond_destroy(q->wait); 259*7c478bd9Sstevel@tonic-gate free(q->lock); 260*7c478bd9Sstevel@tonic-gate free(q->wait); 261*7c478bd9Sstevel@tonic-gate free(q); 262*7c478bd9Sstevel@tonic-gate } 263