xref: /illumos-gate/usr/src/cmd/nscd/nscd_wait.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6cb5caa98Sdjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*3ea037ccSmichen  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23f166393fSesolom  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * routines to wait and wake up a client waiting on a list for a
287c478bd9Sstevel@tonic-gate  * name service request
297c478bd9Sstevel@tonic-gate  */
30cb5caa98Sdjl #include "cache.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate int
nscd_wait(nsc_ctx_t * ctx,nsc_db_t * nscdb,nsc_entry_t * entry)33*3ea037ccSmichen nscd_wait(nsc_ctx_t *ctx, nsc_db_t *nscdb, nsc_entry_t *entry)
347c478bd9Sstevel@tonic-gate {
35*3ea037ccSmichen 	waiter_t	mywait;
36*3ea037ccSmichen 	waiter_t	*wchan = &nscdb->db_wait;
37*3ea037ccSmichen 
38cb5caa98Sdjl 	(void) cond_init(&(mywait.w_waitcv), USYNC_THREAD, 0);
39*3ea037ccSmichen 	mywait.w_key = entry;
40*3ea037ccSmichen 	mywait.w_signaled = 0;
417c478bd9Sstevel@tonic-gate 	mywait.w_next = wchan->w_next;
427c478bd9Sstevel@tonic-gate 	mywait.w_prev = wchan;
43cb5caa98Sdjl 	if (mywait.w_next)
447c478bd9Sstevel@tonic-gate 		mywait.w_next->w_prev = &mywait;
457c478bd9Sstevel@tonic-gate 	wchan->w_next = &mywait;
46cb5caa98Sdjl 
47*3ea037ccSmichen 	(void) mutex_lock(&ctx->stats_mutex);
48*3ea037ccSmichen 	ctx->stats.wait_count++;
49*3ea037ccSmichen 	(void) mutex_unlock(&ctx->stats_mutex);
50*3ea037ccSmichen 
51*3ea037ccSmichen 	while (!mywait.w_signaled)
52*3ea037ccSmichen 		(void) cond_wait(&(mywait.w_waitcv), &nscdb->db_mutex);
53cb5caa98Sdjl 	if (mywait.w_prev)
547c478bd9Sstevel@tonic-gate 		mywait.w_prev->w_next = mywait.w_next;
55cb5caa98Sdjl 	if (mywait.w_next)
567c478bd9Sstevel@tonic-gate 		mywait.w_next->w_prev = mywait.w_prev;
57cb5caa98Sdjl 	return (0);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate int
nscd_signal(nsc_ctx_t * ctx,nsc_db_t * nscdb,nsc_entry_t * entry)61*3ea037ccSmichen nscd_signal(nsc_ctx_t *ctx, nsc_db_t *nscdb, nsc_entry_t *entry)
627c478bd9Sstevel@tonic-gate {
63*3ea037ccSmichen 	int		c = 0;
64*3ea037ccSmichen 	waiter_t	*wchan = &nscdb->db_wait;
65*3ea037ccSmichen 	waiter_t	*tmp = wchan->w_next;
667c478bd9Sstevel@tonic-gate 
67cb5caa98Sdjl 	while (tmp) {
68*3ea037ccSmichen 		if (tmp->w_key == entry) {
69cb5caa98Sdjl 			(void) cond_signal(&(tmp->w_waitcv));
70*3ea037ccSmichen 			tmp->w_signaled = 1;
71*3ea037ccSmichen 
72*3ea037ccSmichen 			(void) mutex_lock(&ctx->stats_mutex);
73*3ea037ccSmichen 			ctx->stats.wait_count--;
74*3ea037ccSmichen 			(void) mutex_unlock(&ctx->stats_mutex);
757c478bd9Sstevel@tonic-gate 			c++;
767c478bd9Sstevel@tonic-gate 		}
777c478bd9Sstevel@tonic-gate 		tmp = tmp->w_next;
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
80cb5caa98Sdjl 	return (c);
817c478bd9Sstevel@tonic-gate }
82