xref: /titanic_44/usr/src/cmd/nscd/nscd_wait.c (revision 02e56f3f1bfc8d9977bafb8cb5202f576dcded27)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * routines to wait and wake up a client waiting on a list for a
31  * name service request
32  */
33 #include <thread.h>
34 #include <synch.h>
35 #include "getxby_door.h"
36 
37 int
38 nscd_wait(waiter_t * wchan,  mutex_t * lock, char ** key)
39 {
40 	waiter_t mywait;
41 	cond_init(&(mywait.w_waitcv), USYNC_THREAD, 0);
42 	mywait.w_key = key;
43 	mywait.w_next = wchan->w_next;
44 	mywait.w_prev = wchan;
45 	if(mywait.w_next)
46 		mywait.w_next->w_prev = &mywait;
47 	wchan->w_next = &mywait;
48 
49 	while( *key == (char *) -1)
50 		cond_wait(&(mywait.w_waitcv), lock);
51 	if(mywait.w_prev)
52 		mywait.w_prev->w_next = mywait.w_next;
53 	if(mywait.w_next)
54 		mywait.w_next->w_prev = mywait.w_prev;
55 	return(0);
56 }
57 
58 int
59 nscd_signal(waiter_t * wchan, char ** key)
60 {
61 	int c = 0;
62 	waiter_t * tmp = wchan->w_next;
63 
64 	while(tmp) {
65 		if(tmp->w_key == key) {
66 			cond_signal(&(tmp->w_waitcv));
67 			c++;
68 		}
69 		tmp = tmp->w_next;
70 	}
71 
72 	return(c);
73 }
74 
75 #ifdef TESTPROG
76 
77 static waiter_t w;
78 static mutex_t  l;
79 static char ** blocks;
80 
81 static int num_threads;
82 
83 main(int argc, char * argv[])
84 {
85 	int i;
86 	void * go();
87 	if(argc != 2) {
88 		printf("usage: %s numthreads\n", argv[0]);
89 		exit(1);
90 		}
91 
92 	num_threads = atoi(argv[1]);
93 
94 	blocks = (char **) malloc(sizeof(char **) * num_threads);
95 
96 	memset(blocks, -1, sizeof(char**) * num_threads);
97 
98 	mutex_lock(&l);
99 
100 	for(i=0;i<num_threads;i++)
101 		if(thr_create(NULL, NULL, go, (void*)i, THR_NEW_LWP, NULL) != 0) {
102 			printf("thread_create failed\n");
103 			exit(2);
104 		}
105 
106 
107 
108 	mutex_unlock(&l);
109 
110 	sleep(5);
111 
112 	printf("going\n");
113 	mutex_lock(&l);
114 
115 	memset(blocks, 0, sizeof(char**) * num_threads);
116 
117 	for(i=0;i<num_threads;i++)
118 		nscd_signal(&w, blocks+i);
119 
120 	mutex_unlock(&l);
121 
122 	while(num_threads--) {
123 		if(thr_join(NULL, NULL, NULL) < 0) {
124 			printf("error in join\n");
125 			exit(2);
126 		}
127 	}
128 
129 	printf("all done\n");
130 	exit(0);
131 }
132 
133 void * go(int index)
134 {
135 	printf("thread %d locking\n", index);
136 
137 	mutex_lock(&l);
138 
139 	printf("thread %d waiting\n", index);
140 
141 	nscd_wait(&w,&l, blocks+index);
142 
143 	printf("thread %d unlocking\n", index);
144 
145 	mutex_unlock(&l);
146 
147 	thr_exit(NULL);
148 }
149 
150 
151 #endif /* TESTPROG */
152 
153