xref: /titanic_41/usr/src/cmd/lvm/rpc.metamedd/med_synch.c (revision dd49f125507979bb2ab505a8daf2a46d1be27051)
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 (c) 1994, 2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "med_local.h"
30 
31 #ifdef	_REENTRANT
32 /*
33  * manipulate conditional variables, handle errors
34  */
35 void
36 med_cv_init(
37 	cond_t	*cvp
38 )
39 {
40 	if (cond_init(cvp, USYNC_THREAD, NULL) != 0) {
41 		med_perror("cond_init");
42 		med_exit(1);
43 	}
44 }
45 
46 void
47 med_cv_destroy(
48 	cond_t	*cvp
49 )
50 {
51 	if (cond_destroy(cvp) != 0) {
52 		med_perror("cond_destroy");
53 		med_exit(1);
54 	}
55 }
56 
57 void
58 med_cv_wait(
59 	cond_t	*cvp,
60 	mutex_t	*mp
61 )
62 {
63 	int	err;
64 
65 	assert(MUTEX_HELD(mp));
66 	if (((err = cond_wait(cvp, mp)) != 0) &&
67 	    (err != EINTR)) {
68 		errno = err;
69 		med_perror("cond_wait");
70 		med_exit(1);
71 	}
72 }
73 
74 void
75 med_cv_timedwait(
76 	cond_t			*cvp,
77 	mutex_t			*mp,
78 	med_msec_t		to
79 )
80 {
81 	struct itimerval	new, old;
82 	int			err;
83 
84 	/* check lock */
85 	assert(MUTEX_HELD(mp));
86 	assert(to != 0);
87 
88 	/* set timer */
89 	new.it_interval.tv_sec = 0;
90 	new.it_interval.tv_usec = 0;
91 	new.it_value.tv_sec = to / 1000;
92 	new.it_value.tv_usec = (to % 1000) * 1000;
93 	if (setitimer(ITIMER_REAL, &new, &old) != 0) {
94 		med_perror("cond_wait");
95 		med_exit(1);
96 	}
97 
98 	/* wait for condition or timeout */
99 	if (((err = cond_wait(cvp, mp)) != 0) &&
100 	    (err != EINTR)) {
101 		errno = err;
102 		med_perror("cond_wait");
103 		med_exit(1);
104 	}
105 
106 	/* reset timer */
107 	if (err != EINTR) {
108 		new.it_interval.tv_sec = 0;
109 		new.it_interval.tv_usec = 0;
110 		new.it_value.tv_sec = 0;
111 		new.it_value.tv_usec = 0;
112 		if (setitimer(ITIMER_REAL, &new, &old) != 0) {
113 			med_perror("cond_wait");
114 			med_exit(1);
115 		}
116 	}
117 }
118 
119 void
120 med_cv_broadcast(
121 	cond_t	*cvp
122 )
123 {
124 	if (cond_broadcast(cvp) != 0) {
125 		med_perror("cond_broadcast");
126 		med_exit(1);
127 	}
128 }
129 
130 /*
131  * manipulate mutexs, handle errors
132  */
133 void
134 med_mx_init(
135 	mutex_t	*mp
136 )
137 {
138 	if (mutex_init(mp, USYNC_THREAD, NULL) != 0) {
139 		med_perror("mutex_init");
140 		med_exit(1);
141 	}
142 }
143 
144 void
145 med_mx_destroy(
146 	mutex_t	*mp
147 )
148 {
149 	if (mutex_destroy(mp) != 0) {
150 		med_perror("mutex_destroy");
151 		med_exit(1);
152 	}
153 }
154 
155 void
156 med_mx_lock(
157 	mutex_t	*mp
158 )
159 {
160 	if (mutex_lock(mp) != 0) {
161 		med_perror("mutex_lock");
162 		med_exit(1);
163 	}
164 }
165 
166 void
167 med_mx_unlock(
168 	mutex_t	*mp
169 )
170 {
171 	assert(MUTEX_HELD(mp));
172 	if (mutex_unlock(mp) != 0) {
173 		med_perror("mutex_unlock");
174 		med_exit(1);
175 	}
176 }
177 
178 /*
179  * manipulate rwlockss, handle errors
180  */
181 void
182 med_rw_rdlock(
183 	rwlock_t	*rwlp
184 )
185 {
186 	if (rw_rdlock(rwlp) != 0) {
187 		med_perror("rw_rdlock");
188 		med_exit(1);
189 	}
190 }
191 
192 void
193 med_rw_wrlock(
194 	rwlock_t	*rwlp
195 )
196 {
197 	if (rw_wrlock(rwlp) != 0) {
198 		med_perror("rw_wrlock");
199 		med_exit(1);
200 	}
201 }
202 
203 void
204 med_rw_unlock(
205 	rwlock_t	*rwlp
206 )
207 {
208 	if (rw_unlock(rwlp) != 0) {
209 		med_perror("rw_unlock");
210 		med_exit(1);
211 	}
212 }
213 #endif	/* _REENTRANT */
214