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