]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - lib/libthr/thread/thr_umtx.c
Merge 233103, 233912 from head:
[FreeBSD/stable/8.git] / lib / libthr / thread / thr_umtx.c
1 /*
2  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  *
28  */
29
30 #include "thr_private.h"
31 #include "thr_umtx.h"
32
33 #ifndef HAS__UMTX_OP_ERR
34 int _umtx_op_err(void *obj, int op, u_long val, void *uaddr, void *uaddr2)
35 {
36         if (_umtx_op(obj, op, val, uaddr, uaddr2) == -1)
37                 return (errno);
38         return (0);
39 }
40 #endif
41
42 void
43 _thr_umutex_init(struct umutex *mtx)
44 {
45         static struct umutex default_mtx = DEFAULT_UMUTEX;
46
47         *mtx = default_mtx;
48 }
49
50 int
51 __thr_umutex_lock(struct umutex *mtx, uint32_t id)
52 {
53         uint32_t owner;
54
55         if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
56                 for (;;) {
57                         /* wait in kernel */
58                         _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0);
59
60                         owner = mtx->m_owner;
61                         if ((owner & ~UMUTEX_CONTESTED) == 0 &&
62                              atomic_cmpset_acq_32(&mtx->m_owner, owner, id|owner))
63                                 return (0);
64                 }
65         }
66
67         return  _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0);
68 }
69
70 int
71 __thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
72         const struct timespec *ets)
73 {
74         struct timespec timo, cts;
75         uint32_t owner;
76         int ret;
77
78         clock_gettime(CLOCK_REALTIME, &cts);
79         TIMESPEC_SUB(&timo, ets, &cts);
80
81         if (timo.tv_sec < 0)
82                 return (ETIMEDOUT);
83
84         for (;;) {
85                 if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
86
87                         /* wait in kernel */
88                         ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, &timo);
89
90                         /* now try to lock it */
91                         owner = mtx->m_owner;
92                         if ((owner & ~UMUTEX_CONTESTED) == 0 &&
93                              atomic_cmpset_acq_32(&mtx->m_owner, owner, id|owner))
94                                 return (0);
95                 } else {
96                         ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, &timo);
97                         if (ret == 0)
98                                 break;
99                 }
100                 if (ret == ETIMEDOUT)
101                         break;
102                 clock_gettime(CLOCK_REALTIME, &cts);
103                 TIMESPEC_SUB(&timo, ets, &cts);
104                 if (timo.tv_sec < 0 || (timo.tv_sec == 0 && timo.tv_nsec == 0)) {
105                         ret = ETIMEDOUT;
106                         break;
107                 }
108         }
109         return (ret);
110 }
111
112 int
113 __thr_umutex_unlock(struct umutex *mtx, uint32_t id)
114 {
115         static int wake2_avail = 0;
116
117         if (__predict_false(wake2_avail == 0)) {
118                 struct umutex test = DEFAULT_UMUTEX;
119
120                 if (_umtx_op(&test, UMTX_OP_MUTEX_WAKE2, test.m_flags, 0, 0) == -1)
121                         wake2_avail = -1;
122                 else 
123                         wake2_avail = 1;
124         }
125
126         if (wake2_avail != 1)
127                 goto unlock;
128
129         uint32_t flags = mtx->m_flags;
130
131         if ((flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) {
132                 uint32_t owner;
133                 do {
134                         owner = mtx->m_owner;
135                         if (__predict_false((owner & ~UMUTEX_CONTESTED) != id))
136                                 return (EPERM);
137                 } while (__predict_false(!atomic_cmpset_rel_32(&mtx->m_owner,
138                                          owner, UMUTEX_UNOWNED)));
139                 if ((owner & UMUTEX_CONTESTED))
140                         (void)_umtx_op_err(mtx, UMTX_OP_MUTEX_WAKE2, flags, 0, 0);
141                 return (0);
142         }
143 unlock:
144         return _umtx_op_err(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0);
145 }
146
147 int
148 __thr_umutex_trylock(struct umutex *mtx)
149 {
150         return _umtx_op_err(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0);
151 }
152
153 int
154 __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
155         uint32_t *oldceiling)
156 {
157         return _umtx_op_err(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0);
158 }
159
160 int
161 _thr_umtx_wait(volatile long *mtx, long id, const struct timespec *timeout)
162 {
163         if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
164                 timeout->tv_nsec <= 0)))
165                 return (ETIMEDOUT);
166         return _umtx_op_err(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT, id, 0,
167                 __DECONST(void*, timeout));
168 }
169
170 int
171 _thr_umtx_wait_uint(volatile u_int *mtx, u_int id, const struct timespec *timeout, int shared)
172 {
173         if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
174                 timeout->tv_nsec <= 0)))
175                 return (ETIMEDOUT);
176         return _umtx_op_err(__DEVOLATILE(void *, mtx), 
177                         shared ? UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id, 0,
178                         __DECONST(void*, timeout));
179 }
180
181 int
182 _thr_umtx_wake(volatile void *mtx, int nr_wakeup, int shared)
183 {
184         return _umtx_op_err(__DEVOLATILE(void *, mtx), shared ? UMTX_OP_WAKE : UMTX_OP_WAKE_PRIVATE,
185                 nr_wakeup, 0, 0);
186 }
187
188 void
189 _thr_ucond_init(struct ucond *cv)
190 {
191         bzero(cv, sizeof(struct ucond));
192 }
193
194 int
195 _thr_ucond_wait(struct ucond *cv, struct umutex *m,
196         const struct timespec *timeout, int check_unparking)
197 {
198         if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 &&
199             timeout->tv_nsec <= 0))) {
200                 struct pthread *curthread = _get_curthread();
201                 _thr_umutex_unlock(m, TID(curthread));
202                 return (ETIMEDOUT);
203         }
204         return _umtx_op_err(cv, UMTX_OP_CV_WAIT,
205                      check_unparking ? UMTX_CHECK_UNPARKING : 0, 
206                      m, __DECONST(void*, timeout));
207 }
208  
209 int
210 _thr_ucond_signal(struct ucond *cv)
211 {
212         if (!cv->c_has_waiters)
213                 return (0);
214         return _umtx_op_err(cv, UMTX_OP_CV_SIGNAL, 0, NULL, NULL);
215 }
216
217 int
218 _thr_ucond_broadcast(struct ucond *cv)
219 {
220         if (!cv->c_has_waiters)
221                 return (0);
222         return _umtx_op_err(cv, UMTX_OP_CV_BROADCAST, 0, NULL, NULL);
223 }
224
225 int
226 __thr_rwlock_rdlock(struct urwlock *rwlock, int flags, struct timespec *tsp)
227 {
228         return _umtx_op_err(rwlock, UMTX_OP_RW_RDLOCK, flags, NULL, tsp);
229 }
230
231 int
232 __thr_rwlock_wrlock(struct urwlock *rwlock, struct timespec *tsp)
233 {
234         return _umtx_op_err(rwlock, UMTX_OP_RW_WRLOCK, 0, NULL, tsp);
235 }
236
237 int
238 __thr_rwlock_unlock(struct urwlock *rwlock)
239 {
240         return _umtx_op_err(rwlock, UMTX_OP_RW_UNLOCK, 0, NULL, NULL);
241 }