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