xref: /onnv-gate/usr/src/cmd/syslogd/queue.c (revision 2104:ce72b74b05fd)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2104Sjjj  * Common Development and Distribution License (the "License").
6*2104Sjjj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2104Sjjj  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <pthread.h>
290Sstevel@tonic-gate #include <malloc.h>
300Sstevel@tonic-gate #include <memory.h>
310Sstevel@tonic-gate #include "dataq.h"
320Sstevel@tonic-gate #include <assert.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #ifndef NDEBUG
350Sstevel@tonic-gate static int
dataq_check(dataq_t * ptr)360Sstevel@tonic-gate dataq_check(dataq_t *ptr)	/* call while holding lock! */
370Sstevel@tonic-gate {
380Sstevel@tonic-gate 	assert(ptr->num_data == ll_check(&ptr->data));
390Sstevel@tonic-gate 	assert(ptr->num_waiters == ll_check(&ptr->waiters));
400Sstevel@tonic-gate 	return (1);
410Sstevel@tonic-gate }
420Sstevel@tonic-gate #endif
430Sstevel@tonic-gate 
440Sstevel@tonic-gate int
dataq_init(dataq_t * ptr)450Sstevel@tonic-gate dataq_init(dataq_t *ptr)
460Sstevel@tonic-gate {
470Sstevel@tonic-gate 	ptr->num_data = 0;
480Sstevel@tonic-gate 	ptr->num_waiters = 0;
490Sstevel@tonic-gate 	ll_init(&ptr->data);
500Sstevel@tonic-gate 	ll_init(&ptr->waiters);
51*2104Sjjj 	(void) pthread_mutex_init(&ptr->lock, NULL);
520Sstevel@tonic-gate 	assert((pthread_mutex_lock(&ptr->lock) == 0) &&
530Sstevel@tonic-gate 		(dataq_check(ptr) == 1) &&
540Sstevel@tonic-gate 		(pthread_mutex_unlock(&ptr->lock) == 0));
550Sstevel@tonic-gate 	return (0);
560Sstevel@tonic-gate }
570Sstevel@tonic-gate 
580Sstevel@tonic-gate int
dataq_enqueue(dataq_t * dataq,void * in)590Sstevel@tonic-gate dataq_enqueue(dataq_t *dataq, void *in)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	dataq_data_t *ptr = (dataq_data_t *)malloc(sizeof (*ptr));
620Sstevel@tonic-gate 	dataq_waiter_t *sleeper;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	if (ptr == NULL)
650Sstevel@tonic-gate 		return (-1);
660Sstevel@tonic-gate 	ptr->data = in;
67*2104Sjjj 	(void) pthread_mutex_lock(&dataq->lock);
680Sstevel@tonic-gate 	assert(dataq_check(dataq));
690Sstevel@tonic-gate 	ll_enqueue(&dataq->data, &ptr->list);
700Sstevel@tonic-gate 	dataq->num_data++;
710Sstevel@tonic-gate 	if (dataq->num_waiters) {
720Sstevel@tonic-gate 		/*LINTED*/
730Sstevel@tonic-gate 		sleeper = (dataq_waiter_t *)ll_peek(&dataq->waiters);
740Sstevel@tonic-gate 		sleeper->wakeup = 1;
75*2104Sjjj 		(void) pthread_cond_signal(&sleeper->cv);
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate 	assert(dataq_check(dataq));
78*2104Sjjj 	(void) pthread_mutex_unlock(&dataq->lock);
790Sstevel@tonic-gate 	return (0);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate int
dataq_dequeue(dataq_t * dataq,void ** outptr,int try)830Sstevel@tonic-gate dataq_dequeue(dataq_t *dataq, void **outptr, int try)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	dataq_data_t *dptr;
860Sstevel@tonic-gate 	dataq_waiter_t *sleeper;
870Sstevel@tonic-gate 
88*2104Sjjj 	(void) pthread_mutex_lock(&dataq->lock);
890Sstevel@tonic-gate 	if ((dataq->num_waiters > 0) ||
900Sstevel@tonic-gate 	    ((dptr = (dataq_data_t *)ll_dequeue(&dataq->data)) == NULL)) {
910Sstevel@tonic-gate 		dataq_waiter_t wait;
920Sstevel@tonic-gate 		if (try) {
93*2104Sjjj 			(void) pthread_mutex_unlock(&dataq->lock);
940Sstevel@tonic-gate 			return (1);
950Sstevel@tonic-gate 		}
960Sstevel@tonic-gate 		wait.wakeup = 0;
97*2104Sjjj 		(void) pthread_cond_init(&wait.cv, NULL);
980Sstevel@tonic-gate 		dataq->num_waiters++;
990Sstevel@tonic-gate 		ll_enqueue(&dataq->waiters, &wait.list);
1000Sstevel@tonic-gate 		while (wait.wakeup == 0)
101*2104Sjjj 			(void) pthread_cond_wait(&wait.cv, &dataq->lock);
102*2104Sjjj 		(void) ll_dequeue(&dataq->waiters);
1030Sstevel@tonic-gate 		dataq->num_waiters--;
104*2104Sjjj 		(void) pthread_cond_destroy(&wait.cv);
1050Sstevel@tonic-gate 		dptr = (dataq_data_t *)ll_dequeue(&dataq->data);
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 	dataq->num_data--;
1080Sstevel@tonic-gate 	if (dataq->num_data && dataq->num_waiters) {
1090Sstevel@tonic-gate 		/*LINTED*/
1100Sstevel@tonic-gate 		sleeper = (dataq_waiter_t *)ll_peek(&dataq->waiters);
1110Sstevel@tonic-gate 		sleeper->wakeup = 1;
112*2104Sjjj 		(void) pthread_cond_signal(&sleeper->cv);
1130Sstevel@tonic-gate 	}
114*2104Sjjj 	(void) pthread_mutex_unlock(&dataq->lock);
1150Sstevel@tonic-gate 	*outptr = dptr->data;
1160Sstevel@tonic-gate 	free(dptr);
1170Sstevel@tonic-gate 	return (0);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate static void
dataq_data_destroy(void * p)1210Sstevel@tonic-gate dataq_data_destroy(void * p)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	dataq_data_t *d = (dataq_data_t *)p;
1240Sstevel@tonic-gate 	free(d->data);
1250Sstevel@tonic-gate 	free(d);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate static void
dataq_waiters_destroy(void * p)1290Sstevel@tonic-gate dataq_waiters_destroy(void * p)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	dataq_waiter_t *d = (dataq_waiter_t *)p;
132*2104Sjjj 	(void) pthread_cond_destroy(&d->cv);
1330Sstevel@tonic-gate 	free(d);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate int
dataq_destroy(dataq_t * dataq)1370Sstevel@tonic-gate dataq_destroy(dataq_t *dataq)
1380Sstevel@tonic-gate {
139*2104Sjjj 	(void) pthread_mutex_destroy(&dataq->lock);
1400Sstevel@tonic-gate 	ll_mapf(&dataq->data, dataq_data_destroy);
1410Sstevel@tonic-gate 	ll_mapf(&dataq->waiters, dataq_waiters_destroy);
1420Sstevel@tonic-gate 	return (0);
1430Sstevel@tonic-gate }
144