xref: /illumos-gate/usr/src/cmd/nscd/nscd_wait.c (revision 6dde88b51419b99fe0aab8e56184c693945826b8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * routines to wait and wake up a client waiting on a list for a
28  * name service request
29  */
30 #include "cache.h"
31 
32 int
33 nscd_wait(nsc_ctx_t *ctx, nsc_db_t *nscdb, nsc_entry_t *entry)
34 {
35 	waiter_t	mywait;
36 	waiter_t	*wchan = &nscdb->db_wait;
37 
38 	(void) cond_init(&(mywait.w_waitcv), USYNC_THREAD, 0);
39 	mywait.w_key = entry;
40 	mywait.w_signaled = 0;
41 	mywait.w_next = wchan->w_next;
42 	mywait.w_prev = wchan;
43 	if (mywait.w_next)
44 		mywait.w_next->w_prev = &mywait;
45 	wchan->w_next = &mywait;
46 
47 	(void) mutex_lock(&ctx->stats_mutex);
48 	ctx->stats.wait_count++;
49 	(void) mutex_unlock(&ctx->stats_mutex);
50 
51 	while (!mywait.w_signaled)
52 		(void) cond_wait(&(mywait.w_waitcv), &nscdb->db_mutex);
53 	if (mywait.w_prev)
54 		mywait.w_prev->w_next = mywait.w_next;
55 	if (mywait.w_next)
56 		mywait.w_next->w_prev = mywait.w_prev;
57 	return (0);
58 }
59 
60 int
61 nscd_signal(nsc_ctx_t *ctx, nsc_db_t *nscdb, nsc_entry_t *entry)
62 {
63 	int		c = 0;
64 	waiter_t	*wchan = &nscdb->db_wait;
65 	waiter_t	*tmp = wchan->w_next;
66 
67 	while (tmp) {
68 		if (tmp->w_key == entry) {
69 			(void) cond_signal(&(tmp->w_waitcv));
70 			tmp->w_signaled = 1;
71 
72 			(void) mutex_lock(&ctx->stats_mutex);
73 			ctx->stats.wait_count--;
74 			(void) mutex_unlock(&ctx->stats_mutex);
75 			c++;
76 		}
77 		tmp = tmp->w_next;
78 	}
79 
80 	return (c);
81 }
82