2 * Copyright (c) 2004, David Xu <davidxu@freebsd.org>
3 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_compat.h"
32 #include "opt_umtx_profiling.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
43 #include <sys/sched.h>
45 #include <sys/sysctl.h>
46 #include <sys/sysent.h>
47 #include <sys/systm.h>
48 #include <sys/sysproto.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/eventhandler.h>
54 #include <vm/vm_param.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_object.h>
59 #include <machine/cpu.h>
61 #ifdef COMPAT_FREEBSD32
62 #include <compat/freebsd32/freebsd32_proto.h>
66 #define _UMUTEX_WAIT 2
69 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
70 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
73 /* Priority inheritance mutex info. */
76 struct thread *pi_owner;
81 /* List entry to link umtx holding by thread */
82 TAILQ_ENTRY(umtx_pi) pi_link;
84 /* List entry in hash */
85 TAILQ_ENTRY(umtx_pi) pi_hashlink;
87 /* List for waiters */
88 TAILQ_HEAD(,umtx_q) pi_blocked;
90 /* Identify a userland lock object */
91 struct umtx_key pi_key;
94 /* A userland synchronous object user. */
96 /* Linked list for the hash. */
97 TAILQ_ENTRY(umtx_q) uq_link;
100 struct umtx_key uq_key;
104 #define UQF_UMTXQ 0x0001
106 /* The thread waits on. */
107 struct thread *uq_thread;
110 * Blocked on PI mutex. read can use chain lock
111 * or umtx_lock, write must have both chain lock and
112 * umtx_lock being hold.
114 struct umtx_pi *uq_pi_blocked;
116 /* On blocked list */
117 TAILQ_ENTRY(umtx_q) uq_lockq;
119 /* Thread contending with us */
120 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
122 /* Inherited priority from PP mutex */
123 u_char uq_inherited_pri;
125 /* Spare queue ready to be reused */
126 struct umtxq_queue *uq_spare_queue;
128 /* The queue we on */
129 struct umtxq_queue *uq_cur_queue;
132 TAILQ_HEAD(umtxq_head, umtx_q);
134 /* Per-key wait-queue */
136 struct umtxq_head head;
138 LIST_ENTRY(umtxq_queue) link;
142 LIST_HEAD(umtxq_list, umtxq_queue);
144 /* Userland lock object's wait-queue chain */
146 /* Lock for this chain. */
149 /* List of sleep queues. */
150 struct umtxq_list uc_queue[2];
151 #define UMTX_SHARED_QUEUE 0
152 #define UMTX_EXCLUSIVE_QUEUE 1
154 LIST_HEAD(, umtxq_queue) uc_spare_queue;
159 /* Chain lock waiters */
162 /* All PI in the list */
163 TAILQ_HEAD(,umtx_pi) uc_pi_list;
165 #ifdef UMTX_PROFILING
171 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
174 * Don't propagate time-sharing priority, there is a security reason,
175 * a user can simply introduce PI-mutex, let thread A lock the mutex,
176 * and let another thread B block on the mutex, because B is
177 * sleeping, its priority will be boosted, this causes A's priority to
178 * be boosted via priority propagating too and will never be lowered even
179 * if it is using 100%CPU, this is unfair to other processes.
182 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
183 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
184 PRI_MAX_TIMESHARE : (td)->td_user_pri)
186 #define GOLDEN_RATIO_PRIME 2654404609U
187 #define UMTX_CHAINS 512
188 #define UMTX_SHIFTS (__WORD_BIT - 9)
190 #define GET_SHARE(flags) \
191 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
193 #define BUSY_SPINS 200
201 static uma_zone_t umtx_pi_zone;
202 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
203 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
204 static int umtx_pi_allocated;
206 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
207 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
208 &umtx_pi_allocated, 0, "Allocated umtx_pi");
210 #ifdef UMTX_PROFILING
211 static long max_length;
212 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
213 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD, 0, "umtx chain stats");
216 static void umtxq_sysinit(void *);
217 static void umtxq_hash(struct umtx_key *key);
218 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
219 static void umtxq_lock(struct umtx_key *key);
220 static void umtxq_unlock(struct umtx_key *key);
221 static void umtxq_busy(struct umtx_key *key);
222 static void umtxq_unbusy(struct umtx_key *key);
223 static void umtxq_insert_queue(struct umtx_q *uq, int q);
224 static void umtxq_remove_queue(struct umtx_q *uq, int q);
225 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
226 static int umtxq_count(struct umtx_key *key);
227 static struct umtx_pi *umtx_pi_alloc(int);
228 static void umtx_pi_free(struct umtx_pi *pi);
229 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags);
230 static void umtx_thread_cleanup(struct thread *td);
231 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
232 struct image_params *imgp __unused);
233 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
235 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
236 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
237 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
239 static struct mtx umtx_lock;
241 #ifdef UMTX_PROFILING
243 umtx_init_profiling(void)
245 struct sysctl_oid *chain_oid;
249 for (i = 0; i < UMTX_CHAINS; ++i) {
250 snprintf(chain_name, sizeof(chain_name), "%d", i);
251 chain_oid = SYSCTL_ADD_NODE(NULL,
252 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
253 chain_name, CTLFLAG_RD, NULL, "umtx hash stats");
254 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
255 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
256 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
257 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
262 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
266 struct umtxq_chain *uc;
267 u_int fract, i, j, tot, whole;
268 u_int sf0, sf1, sf2, sf3, sf4;
269 u_int si0, si1, si2, si3, si4;
270 u_int sw0, sw1, sw2, sw3, sw4;
272 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
273 for (i = 0; i < 2; i++) {
275 for (j = 0; j < UMTX_CHAINS; ++j) {
276 uc = &umtxq_chains[i][j];
277 mtx_lock(&uc->uc_lock);
278 tot += uc->max_length;
279 mtx_unlock(&uc->uc_lock);
282 sbuf_printf(&sb, "%u) Empty ", i);
284 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
285 si0 = si1 = si2 = si3 = si4 = 0;
286 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
287 for (j = 0; j < UMTX_CHAINS; j++) {
288 uc = &umtxq_chains[i][j];
289 mtx_lock(&uc->uc_lock);
290 whole = uc->max_length * 100;
291 mtx_unlock(&uc->uc_lock);
292 fract = (whole % tot) * 100;
293 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
297 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
302 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
307 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
312 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
319 sbuf_printf(&sb, "queue %u:\n", i);
320 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
322 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
324 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
326 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
328 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
334 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
340 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
342 struct umtxq_chain *uc;
347 error = sysctl_handle_int(oidp, &clear, 0, req);
348 if (error != 0 || req->newptr == NULL)
352 for (i = 0; i < 2; ++i) {
353 for (j = 0; j < UMTX_CHAINS; ++j) {
354 uc = &umtxq_chains[i][j];
355 mtx_lock(&uc->uc_lock);
358 mtx_unlock(&uc->uc_lock);
365 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
366 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
367 sysctl_debug_umtx_chains_clear, "I", "Clear umtx chains statistics");
368 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
369 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
370 sysctl_debug_umtx_chains_peaks, "A", "Highest peaks in chains max length");
374 umtxq_sysinit(void *arg __unused)
378 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
379 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
380 for (i = 0; i < 2; ++i) {
381 for (j = 0; j < UMTX_CHAINS; ++j) {
382 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
383 MTX_DEF | MTX_DUPOK);
384 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
385 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
386 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
387 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
388 umtxq_chains[i][j].uc_busy = 0;
389 umtxq_chains[i][j].uc_waiters = 0;
390 #ifdef UMTX_PROFILING
391 umtxq_chains[i][j].length = 0;
392 umtxq_chains[i][j].max_length = 0;
396 #ifdef UMTX_PROFILING
397 umtx_init_profiling();
399 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
400 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
401 EVENTHANDLER_PRI_ANY);
409 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
410 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX, M_WAITOK | M_ZERO);
411 TAILQ_INIT(&uq->uq_spare_queue->head);
412 TAILQ_INIT(&uq->uq_pi_contested);
413 uq->uq_inherited_pri = PRI_MAX;
418 umtxq_free(struct umtx_q *uq)
420 MPASS(uq->uq_spare_queue != NULL);
421 free(uq->uq_spare_queue, M_UMTX);
426 umtxq_hash(struct umtx_key *key)
428 unsigned n = (uintptr_t)key->info.both.a + key->info.both.b;
429 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
432 static inline struct umtxq_chain *
433 umtxq_getchain(struct umtx_key *key)
435 if (key->type <= TYPE_SEM)
436 return (&umtxq_chains[1][key->hash]);
437 return (&umtxq_chains[0][key->hash]);
444 umtxq_lock(struct umtx_key *key)
446 struct umtxq_chain *uc;
448 uc = umtxq_getchain(key);
449 mtx_lock(&uc->uc_lock);
456 umtxq_unlock(struct umtx_key *key)
458 struct umtxq_chain *uc;
460 uc = umtxq_getchain(key);
461 mtx_unlock(&uc->uc_lock);
465 * Set chain to busy state when following operation
466 * may be blocked (kernel mutex can not be used).
469 umtxq_busy(struct umtx_key *key)
471 struct umtxq_chain *uc;
473 uc = umtxq_getchain(key);
474 mtx_assert(&uc->uc_lock, MA_OWNED);
478 int count = BUSY_SPINS;
481 while (uc->uc_busy && --count > 0)
487 while (uc->uc_busy) {
489 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
500 umtxq_unbusy(struct umtx_key *key)
502 struct umtxq_chain *uc;
504 uc = umtxq_getchain(key);
505 mtx_assert(&uc->uc_lock, MA_OWNED);
506 KASSERT(uc->uc_busy != 0, ("not busy"));
513 umtxq_unbusy_unlocked(struct umtx_key *key)
521 static struct umtxq_queue *
522 umtxq_queue_lookup(struct umtx_key *key, int q)
524 struct umtxq_queue *uh;
525 struct umtxq_chain *uc;
527 uc = umtxq_getchain(key);
528 UMTXQ_LOCKED_ASSERT(uc);
529 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
530 if (umtx_key_match(&uh->key, key))
538 umtxq_insert_queue(struct umtx_q *uq, int q)
540 struct umtxq_queue *uh;
541 struct umtxq_chain *uc;
543 uc = umtxq_getchain(&uq->uq_key);
544 UMTXQ_LOCKED_ASSERT(uc);
545 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
546 uh = umtxq_queue_lookup(&uq->uq_key, q);
548 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
550 uh = uq->uq_spare_queue;
551 uh->key = uq->uq_key;
552 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
553 #ifdef UMTX_PROFILING
555 if (uc->length > uc->max_length) {
556 uc->max_length = uc->length;
557 if (uc->max_length > max_length)
558 max_length = uc->max_length;
562 uq->uq_spare_queue = NULL;
564 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
566 uq->uq_flags |= UQF_UMTXQ;
567 uq->uq_cur_queue = uh;
572 umtxq_remove_queue(struct umtx_q *uq, int q)
574 struct umtxq_chain *uc;
575 struct umtxq_queue *uh;
577 uc = umtxq_getchain(&uq->uq_key);
578 UMTXQ_LOCKED_ASSERT(uc);
579 if (uq->uq_flags & UQF_UMTXQ) {
580 uh = uq->uq_cur_queue;
581 TAILQ_REMOVE(&uh->head, uq, uq_link);
583 uq->uq_flags &= ~UQF_UMTXQ;
584 if (TAILQ_EMPTY(&uh->head)) {
585 KASSERT(uh->length == 0,
586 ("inconsistent umtxq_queue length"));
587 #ifdef UMTX_PROFILING
590 LIST_REMOVE(uh, link);
592 uh = LIST_FIRST(&uc->uc_spare_queue);
593 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
594 LIST_REMOVE(uh, link);
596 uq->uq_spare_queue = uh;
597 uq->uq_cur_queue = NULL;
602 * Check if there are multiple waiters
605 umtxq_count(struct umtx_key *key)
607 struct umtxq_chain *uc;
608 struct umtxq_queue *uh;
610 uc = umtxq_getchain(key);
611 UMTXQ_LOCKED_ASSERT(uc);
612 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
619 * Check if there are multiple PI waiters and returns first
623 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
625 struct umtxq_chain *uc;
626 struct umtxq_queue *uh;
629 uc = umtxq_getchain(key);
630 UMTXQ_LOCKED_ASSERT(uc);
631 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
633 *first = TAILQ_FIRST(&uh->head);
640 umtxq_check_susp(struct thread *td)
646 * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
647 * eventually break the lockstep loop.
649 if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
654 if (P_SHOULDSTOP(p) ||
655 ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) {
656 if (p->p_flag & P_SINGLE_EXIT)
666 * Wake up threads waiting on an userland object.
670 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
672 struct umtxq_chain *uc;
673 struct umtxq_queue *uh;
678 uc = umtxq_getchain(key);
679 UMTXQ_LOCKED_ASSERT(uc);
680 uh = umtxq_queue_lookup(key, q);
682 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
683 umtxq_remove_queue(uq, q);
694 * Wake up specified thread.
697 umtxq_signal_thread(struct umtx_q *uq)
699 struct umtxq_chain *uc;
701 uc = umtxq_getchain(&uq->uq_key);
702 UMTXQ_LOCKED_ASSERT(uc);
708 tstohz(const struct timespec *tsp)
712 TIMESPEC_TO_TIMEVAL(&tv, tsp);
717 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
718 const struct timespec *timeout)
721 timo->clockid = clockid;
723 kern_clock_gettime(curthread, clockid, &timo->end);
724 timo->cur = timo->end;
725 timespecadd(&timo->end, timeout);
727 timo->end = *timeout;
728 kern_clock_gettime(curthread, clockid, &timo->cur);
733 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
736 abs_timeout_init(timo, umtxtime->_clockid,
737 (umtxtime->_flags & UMTX_ABSTIME) != 0,
738 &umtxtime->_timeout);
742 abs_timeout_update(struct abs_timeout *timo)
744 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
748 abs_timeout_gethz(struct abs_timeout *timo)
752 if (timespeccmp(&timo->end, &timo->cur, <=))
755 timespecsub(&tts, &timo->cur);
756 return (tstohz(&tts));
760 * Put thread into sleep state, before sleeping, check if
761 * thread was removed from umtx queue.
764 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
766 struct umtxq_chain *uc;
769 uc = umtxq_getchain(&uq->uq_key);
770 UMTXQ_LOCKED_ASSERT(uc);
772 if (!(uq->uq_flags & UQF_UMTXQ))
774 if (abstime != NULL) {
775 timo = abs_timeout_gethz(abstime);
780 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
781 if (error != EWOULDBLOCK) {
782 umtxq_lock(&uq->uq_key);
786 abs_timeout_update(abstime);
787 umtxq_lock(&uq->uq_key);
793 * Convert userspace address into unique logical address.
796 umtx_key_get(void *addr, int type, int share, struct umtx_key *key)
798 struct thread *td = curthread;
800 vm_map_entry_t entry;
806 if (share == THREAD_SHARE) {
808 key->info.private.vs = td->td_proc->p_vmspace;
809 key->info.private.addr = (uintptr_t)addr;
811 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
812 map = &td->td_proc->p_vmspace->vm_map;
813 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
814 &entry, &key->info.shared.object, &pindex, &prot,
815 &wired) != KERN_SUCCESS) {
819 if ((share == PROCESS_SHARE) ||
820 (share == AUTO_SHARE &&
821 VM_INHERIT_SHARE == entry->inheritance)) {
823 key->info.shared.offset = entry->offset + entry->start -
825 vm_object_reference(key->info.shared.object);
828 key->info.private.vs = td->td_proc->p_vmspace;
829 key->info.private.addr = (uintptr_t)addr;
831 vm_map_lookup_done(map, entry);
842 umtx_key_release(struct umtx_key *key)
845 vm_object_deallocate(key->info.shared.object);
849 * Lock a umtx object.
852 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
853 const struct timespec *timeout)
855 struct abs_timeout timo;
863 abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
866 * Care must be exercised when dealing with umtx structure. It
867 * can fault on any access.
871 * Try the uncontested case. This should be done in userland.
873 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
875 /* The acquire succeeded. */
876 if (owner == UMTX_UNOWNED)
879 /* The address was invalid. */
883 /* If no one owns it but it is contested try to acquire it. */
884 if (owner == UMTX_CONTESTED) {
885 owner = casuword(&umtx->u_owner,
886 UMTX_CONTESTED, id | UMTX_CONTESTED);
888 if (owner == UMTX_CONTESTED)
891 /* The address was invalid. */
895 error = umtxq_check_susp(td);
899 /* If this failed the lock has changed, restart. */
904 * If we caught a signal, we have retried and now
910 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
911 AUTO_SHARE, &uq->uq_key)) != 0)
914 umtxq_lock(&uq->uq_key);
915 umtxq_busy(&uq->uq_key);
917 umtxq_unbusy(&uq->uq_key);
918 umtxq_unlock(&uq->uq_key);
921 * Set the contested bit so that a release in user space
922 * knows to use the system call for unlock. If this fails
923 * either some one else has acquired the lock or it has been
926 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
928 /* The address was invalid. */
930 umtxq_lock(&uq->uq_key);
932 umtxq_unlock(&uq->uq_key);
933 umtx_key_release(&uq->uq_key);
938 * We set the contested bit, sleep. Otherwise the lock changed
939 * and we need to retry or we lost a race to the thread
940 * unlocking the umtx.
942 umtxq_lock(&uq->uq_key);
944 error = umtxq_sleep(uq, "umtx", timeout == NULL ? NULL :
947 umtxq_unlock(&uq->uq_key);
948 umtx_key_release(&uq->uq_key);
951 error = umtxq_check_susp(td);
954 if (timeout == NULL) {
955 /* Mutex locking is restarted if it is interrupted. */
959 /* Timed-locking is not restarted. */
960 if (error == ERESTART)
967 * Unlock a umtx object.
970 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
979 * Make sure we own this mtx.
981 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
985 if ((owner & ~UMTX_CONTESTED) != id)
988 /* This should be done in userland */
989 if ((owner & UMTX_CONTESTED) == 0) {
990 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
998 /* We should only ever be in here for contested locks */
999 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1005 count = umtxq_count(&key);
1009 * When unlocking the umtx, it must be marked as unowned if
1010 * there is zero or one thread only waiting for it.
1011 * Otherwise, it must be marked as contested.
1013 old = casuword(&umtx->u_owner, owner,
1014 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
1016 umtxq_signal(&key,1);
1019 umtx_key_release(&key);
1027 #ifdef COMPAT_FREEBSD32
1030 * Lock a umtx object.
1033 do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id,
1034 const struct timespec *timeout)
1036 struct abs_timeout timo;
1044 if (timeout != NULL)
1045 abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
1048 * Care must be exercised when dealing with umtx structure. It
1049 * can fault on any access.
1053 * Try the uncontested case. This should be done in userland.
1055 owner = casuword32(m, UMUTEX_UNOWNED, id);
1057 /* The acquire succeeded. */
1058 if (owner == UMUTEX_UNOWNED)
1061 /* The address was invalid. */
1065 /* If no one owns it but it is contested try to acquire it. */
1066 if (owner == UMUTEX_CONTESTED) {
1067 owner = casuword32(m,
1068 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1069 if (owner == UMUTEX_CONTESTED)
1072 /* The address was invalid. */
1076 error = umtxq_check_susp(td);
1080 /* If this failed the lock has changed, restart. */
1085 * If we caught a signal, we have retried and now
1091 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
1092 AUTO_SHARE, &uq->uq_key)) != 0)
1095 umtxq_lock(&uq->uq_key);
1096 umtxq_busy(&uq->uq_key);
1098 umtxq_unbusy(&uq->uq_key);
1099 umtxq_unlock(&uq->uq_key);
1102 * Set the contested bit so that a release in user space
1103 * knows to use the system call for unlock. If this fails
1104 * either some one else has acquired the lock or it has been
1107 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
1109 /* The address was invalid. */
1111 umtxq_lock(&uq->uq_key);
1113 umtxq_unlock(&uq->uq_key);
1114 umtx_key_release(&uq->uq_key);
1119 * We set the contested bit, sleep. Otherwise the lock changed
1120 * and we need to retry or we lost a race to the thread
1121 * unlocking the umtx.
1123 umtxq_lock(&uq->uq_key);
1125 error = umtxq_sleep(uq, "umtx", timeout == NULL ?
1128 umtxq_unlock(&uq->uq_key);
1129 umtx_key_release(&uq->uq_key);
1132 error = umtxq_check_susp(td);
1135 if (timeout == NULL) {
1136 /* Mutex locking is restarted if it is interrupted. */
1140 /* Timed-locking is not restarted. */
1141 if (error == ERESTART)
1148 * Unlock a umtx object.
1151 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
1153 struct umtx_key key;
1160 * Make sure we own this mtx.
1162 owner = fuword32(m);
1166 if ((owner & ~UMUTEX_CONTESTED) != id)
1169 /* This should be done in userland */
1170 if ((owner & UMUTEX_CONTESTED) == 0) {
1171 old = casuword32(m, owner, UMUTEX_UNOWNED);
1179 /* We should only ever be in here for contested locks */
1180 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1186 count = umtxq_count(&key);
1190 * When unlocking the umtx, it must be marked as unowned if
1191 * there is zero or one thread only waiting for it.
1192 * Otherwise, it must be marked as contested.
1194 old = casuword32(m, owner,
1195 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1197 umtxq_signal(&key,1);
1200 umtx_key_release(&key);
1210 * Fetch and compare value, sleep on the address if value is not changed.
1213 do_wait(struct thread *td, void *addr, u_long id,
1214 struct _umtx_time *timeout, int compat32, int is_private)
1216 struct abs_timeout timo;
1223 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1224 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1227 if (timeout != NULL)
1228 abs_timeout_init2(&timo, timeout);
1230 umtxq_lock(&uq->uq_key);
1232 umtxq_unlock(&uq->uq_key);
1233 if (compat32 == 0) {
1234 error = fueword(addr, &tmp);
1238 error = fueword32(addr, &tmp32);
1244 umtxq_lock(&uq->uq_key);
1247 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
1249 if ((uq->uq_flags & UQF_UMTXQ) == 0)
1253 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
1256 umtxq_unlock(&uq->uq_key);
1257 umtx_key_release(&uq->uq_key);
1258 if (error == ERESTART)
1264 * Wake up threads sleeping on the specified address.
1267 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1269 struct umtx_key key;
1272 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1273 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1276 ret = umtxq_signal(&key, n_wake);
1278 umtx_key_release(&key);
1283 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1286 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1287 struct _umtx_time *timeout, int mode)
1289 struct abs_timeout timo;
1291 uint32_t owner, old, id;
1297 if (timeout != NULL)
1298 abs_timeout_init2(&timo, timeout);
1301 * Care must be exercised when dealing with umtx structure. It
1302 * can fault on any access.
1305 rv = fueword32(&m->m_owner, &owner);
1308 if (mode == _UMUTEX_WAIT) {
1309 if (owner == UMUTEX_UNOWNED || owner == UMUTEX_CONTESTED)
1313 * Try the uncontested case. This should be done in userland.
1315 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1317 /* The address was invalid. */
1321 /* The acquire succeeded. */
1322 if (owner == UMUTEX_UNOWNED)
1325 /* If no one owns it but it is contested try to acquire it. */
1326 if (owner == UMUTEX_CONTESTED) {
1327 rv = casueword32(&m->m_owner,
1328 UMUTEX_CONTESTED, &owner,
1329 id | UMUTEX_CONTESTED);
1330 /* The address was invalid. */
1334 if (owner == UMUTEX_CONTESTED)
1337 rv = umtxq_check_susp(td);
1341 /* If this failed the lock has changed, restart. */
1346 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1347 (owner & ~UMUTEX_CONTESTED) == id)
1350 if (mode == _UMUTEX_TRY)
1354 * If we caught a signal, we have retried and now
1360 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1361 GET_SHARE(flags), &uq->uq_key)) != 0)
1364 umtxq_lock(&uq->uq_key);
1365 umtxq_busy(&uq->uq_key);
1367 umtxq_unlock(&uq->uq_key);
1370 * Set the contested bit so that a release in user space
1371 * knows to use the system call for unlock. If this fails
1372 * either some one else has acquired the lock or it has been
1375 rv = casueword32(&m->m_owner, owner, &old,
1376 owner | UMUTEX_CONTESTED);
1378 /* The address was invalid. */
1380 umtxq_lock(&uq->uq_key);
1382 umtxq_unbusy(&uq->uq_key);
1383 umtxq_unlock(&uq->uq_key);
1384 umtx_key_release(&uq->uq_key);
1389 * We set the contested bit, sleep. Otherwise the lock changed
1390 * and we need to retry or we lost a race to the thread
1391 * unlocking the umtx.
1393 umtxq_lock(&uq->uq_key);
1394 umtxq_unbusy(&uq->uq_key);
1396 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1399 umtxq_unlock(&uq->uq_key);
1400 umtx_key_release(&uq->uq_key);
1403 error = umtxq_check_susp(td);
1410 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1413 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags)
1415 struct umtx_key key;
1416 uint32_t owner, old, id;
1422 * Make sure we own this mtx.
1424 error = fueword32(&m->m_owner, &owner);
1428 if ((owner & ~UMUTEX_CONTESTED) != id)
1431 if ((owner & UMUTEX_CONTESTED) == 0) {
1432 error = casueword32(&m->m_owner, owner, &old, UMUTEX_UNOWNED);
1440 /* We should only ever be in here for contested locks */
1441 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1447 count = umtxq_count(&key);
1451 * When unlocking the umtx, it must be marked as unowned if
1452 * there is zero or one thread only waiting for it.
1453 * Otherwise, it must be marked as contested.
1455 error = casueword32(&m->m_owner, owner, &old,
1456 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1458 umtxq_signal(&key,1);
1461 umtx_key_release(&key);
1470 * Check if the mutex is available and wake up a waiter,
1471 * only for simple mutex.
1474 do_wake_umutex(struct thread *td, struct umutex *m)
1476 struct umtx_key key;
1482 error = fueword32(&m->m_owner, &owner);
1486 if ((owner & ~UMUTEX_CONTESTED) != 0)
1489 error = fueword32(&m->m_flags, &flags);
1493 /* We should only ever be in here for contested locks */
1494 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1500 count = umtxq_count(&key);
1504 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1511 if (error == 0 && count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1512 umtxq_signal(&key, 1);
1515 umtx_key_release(&key);
1520 * Check if the mutex has waiters and tries to fix contention bit.
1523 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1525 struct umtx_key key;
1526 uint32_t owner, old;
1531 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
1533 type = TYPE_NORMAL_UMUTEX;
1535 case UMUTEX_PRIO_INHERIT:
1536 type = TYPE_PI_UMUTEX;
1538 case UMUTEX_PRIO_PROTECT:
1539 type = TYPE_PP_UMUTEX;
1544 if ((error = umtx_key_get(m, type, GET_SHARE(flags),
1551 count = umtxq_count(&key);
1554 * Only repair contention bit if there is a waiter, this means the mutex
1555 * is still being referenced by userland code, otherwise don't update
1559 error = fueword32(&m->m_owner, &owner);
1562 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0) {
1563 error = casueword32(&m->m_owner, owner, &old,
1564 owner | UMUTEX_CONTESTED);
1572 error = umtxq_check_susp(td);
1576 } else if (count == 1) {
1577 error = fueword32(&m->m_owner, &owner);
1580 while (error == 0 && (owner & ~UMUTEX_CONTESTED) != 0 &&
1581 (owner & UMUTEX_CONTESTED) == 0) {
1582 error = casueword32(&m->m_owner, owner, &old,
1583 owner | UMUTEX_CONTESTED);
1591 error = umtxq_check_susp(td);
1597 if (error == EFAULT) {
1598 umtxq_signal(&key, INT_MAX);
1599 } else if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1600 umtxq_signal(&key, 1);
1603 umtx_key_release(&key);
1607 static inline struct umtx_pi *
1608 umtx_pi_alloc(int flags)
1612 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1613 TAILQ_INIT(&pi->pi_blocked);
1614 atomic_add_int(&umtx_pi_allocated, 1);
1619 umtx_pi_free(struct umtx_pi *pi)
1621 uma_zfree(umtx_pi_zone, pi);
1622 atomic_add_int(&umtx_pi_allocated, -1);
1626 * Adjust the thread's position on a pi_state after its priority has been
1630 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1632 struct umtx_q *uq, *uq1, *uq2;
1635 mtx_assert(&umtx_lock, MA_OWNED);
1642 * Check if the thread needs to be moved on the blocked chain.
1643 * It needs to be moved if either its priority is lower than
1644 * the previous thread or higher than the next thread.
1646 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1647 uq2 = TAILQ_NEXT(uq, uq_lockq);
1648 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1649 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1651 * Remove thread from blocked chain and determine where
1652 * it should be moved to.
1654 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1655 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1656 td1 = uq1->uq_thread;
1657 MPASS(td1->td_proc->p_magic == P_MAGIC);
1658 if (UPRI(td1) > UPRI(td))
1663 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1665 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1670 static struct umtx_pi *
1671 umtx_pi_next(struct umtx_pi *pi)
1673 struct umtx_q *uq_owner;
1675 if (pi->pi_owner == NULL)
1677 uq_owner = pi->pi_owner->td_umtxq;
1678 if (uq_owner == NULL)
1680 return (uq_owner->uq_pi_blocked);
1684 * Floyd's Cycle-Finding Algorithm.
1687 umtx_pi_check_loop(struct umtx_pi *pi)
1689 struct umtx_pi *pi1; /* fast iterator */
1691 mtx_assert(&umtx_lock, MA_OWNED);
1696 pi = umtx_pi_next(pi);
1699 pi1 = umtx_pi_next(pi1);
1702 pi1 = umtx_pi_next(pi1);
1712 * Propagate priority when a thread is blocked on POSIX
1716 umtx_propagate_priority(struct thread *td)
1722 mtx_assert(&umtx_lock, MA_OWNED);
1725 pi = uq->uq_pi_blocked;
1728 if (umtx_pi_check_loop(pi))
1733 if (td == NULL || td == curthread)
1736 MPASS(td->td_proc != NULL);
1737 MPASS(td->td_proc->p_magic == P_MAGIC);
1740 if (td->td_lend_user_pri > pri)
1741 sched_lend_user_prio(td, pri);
1749 * Pick up the lock that td is blocked on.
1752 pi = uq->uq_pi_blocked;
1755 /* Resort td on the list if needed. */
1756 umtx_pi_adjust_thread(pi, td);
1761 * Unpropagate priority for a PI mutex when a thread blocked on
1762 * it is interrupted by signal or resumed by others.
1765 umtx_repropagate_priority(struct umtx_pi *pi)
1767 struct umtx_q *uq, *uq_owner;
1768 struct umtx_pi *pi2;
1771 mtx_assert(&umtx_lock, MA_OWNED);
1773 if (umtx_pi_check_loop(pi))
1775 while (pi != NULL && pi->pi_owner != NULL) {
1777 uq_owner = pi->pi_owner->td_umtxq;
1779 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1780 uq = TAILQ_FIRST(&pi2->pi_blocked);
1782 if (pri > UPRI(uq->uq_thread))
1783 pri = UPRI(uq->uq_thread);
1787 if (pri > uq_owner->uq_inherited_pri)
1788 pri = uq_owner->uq_inherited_pri;
1789 thread_lock(pi->pi_owner);
1790 sched_lend_user_prio(pi->pi_owner, pri);
1791 thread_unlock(pi->pi_owner);
1792 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1793 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1798 * Insert a PI mutex into owned list.
1801 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1803 struct umtx_q *uq_owner;
1805 uq_owner = owner->td_umtxq;
1806 mtx_assert(&umtx_lock, MA_OWNED);
1807 if (pi->pi_owner != NULL)
1808 panic("pi_ower != NULL");
1809 pi->pi_owner = owner;
1810 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1815 * Disown a PI mutex, and remove it from the owned list.
1818 umtx_pi_disown(struct umtx_pi *pi)
1821 mtx_assert(&umtx_lock, MA_OWNED);
1822 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1823 pi->pi_owner = NULL;
1827 * Claim ownership of a PI mutex.
1830 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1832 struct umtx_q *uq, *uq_owner;
1834 uq_owner = owner->td_umtxq;
1835 mtx_lock(&umtx_lock);
1836 if (pi->pi_owner == owner) {
1837 mtx_unlock(&umtx_lock);
1841 if (pi->pi_owner != NULL) {
1843 * userland may have already messed the mutex, sigh.
1845 mtx_unlock(&umtx_lock);
1848 umtx_pi_setowner(pi, owner);
1849 uq = TAILQ_FIRST(&pi->pi_blocked);
1853 pri = UPRI(uq->uq_thread);
1855 if (pri < UPRI(owner))
1856 sched_lend_user_prio(owner, pri);
1857 thread_unlock(owner);
1859 mtx_unlock(&umtx_lock);
1864 * Adjust a thread's order position in its blocked PI mutex,
1865 * this may result new priority propagating process.
1868 umtx_pi_adjust(struct thread *td, u_char oldpri)
1874 mtx_lock(&umtx_lock);
1876 * Pick up the lock that td is blocked on.
1878 pi = uq->uq_pi_blocked;
1880 umtx_pi_adjust_thread(pi, td);
1881 umtx_repropagate_priority(pi);
1883 mtx_unlock(&umtx_lock);
1887 * Sleep on a PI mutex.
1890 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi,
1891 uint32_t owner, const char *wmesg, struct abs_timeout *timo)
1893 struct umtxq_chain *uc;
1894 struct thread *td, *td1;
1900 KASSERT(td == curthread, ("inconsistent uq_thread"));
1901 uc = umtxq_getchain(&uq->uq_key);
1902 UMTXQ_LOCKED_ASSERT(uc);
1903 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1905 mtx_lock(&umtx_lock);
1906 if (pi->pi_owner == NULL) {
1907 mtx_unlock(&umtx_lock);
1908 /* XXX Only look up thread in current process. */
1909 td1 = tdfind(owner, curproc->p_pid);
1910 mtx_lock(&umtx_lock);
1912 if (pi->pi_owner == NULL)
1913 umtx_pi_setowner(pi, td1);
1914 PROC_UNLOCK(td1->td_proc);
1918 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1919 pri = UPRI(uq1->uq_thread);
1925 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1927 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1929 uq->uq_pi_blocked = pi;
1931 td->td_flags |= TDF_UPIBLOCKED;
1933 umtx_propagate_priority(td);
1934 mtx_unlock(&umtx_lock);
1935 umtxq_unbusy(&uq->uq_key);
1937 error = umtxq_sleep(uq, wmesg, timo);
1940 mtx_lock(&umtx_lock);
1941 uq->uq_pi_blocked = NULL;
1943 td->td_flags &= ~TDF_UPIBLOCKED;
1945 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1946 umtx_repropagate_priority(pi);
1947 mtx_unlock(&umtx_lock);
1948 umtxq_unlock(&uq->uq_key);
1954 * Add reference count for a PI mutex.
1957 umtx_pi_ref(struct umtx_pi *pi)
1959 struct umtxq_chain *uc;
1961 uc = umtxq_getchain(&pi->pi_key);
1962 UMTXQ_LOCKED_ASSERT(uc);
1967 * Decrease reference count for a PI mutex, if the counter
1968 * is decreased to zero, its memory space is freed.
1971 umtx_pi_unref(struct umtx_pi *pi)
1973 struct umtxq_chain *uc;
1975 uc = umtxq_getchain(&pi->pi_key);
1976 UMTXQ_LOCKED_ASSERT(uc);
1977 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1978 if (--pi->pi_refcount == 0) {
1979 mtx_lock(&umtx_lock);
1980 if (pi->pi_owner != NULL) {
1981 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested,
1983 pi->pi_owner = NULL;
1985 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1986 ("blocked queue not empty"));
1987 mtx_unlock(&umtx_lock);
1988 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1994 * Find a PI mutex in hash table.
1996 static struct umtx_pi *
1997 umtx_pi_lookup(struct umtx_key *key)
1999 struct umtxq_chain *uc;
2002 uc = umtxq_getchain(key);
2003 UMTXQ_LOCKED_ASSERT(uc);
2005 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
2006 if (umtx_key_match(&pi->pi_key, key)) {
2014 * Insert a PI mutex into hash table.
2017 umtx_pi_insert(struct umtx_pi *pi)
2019 struct umtxq_chain *uc;
2021 uc = umtxq_getchain(&pi->pi_key);
2022 UMTXQ_LOCKED_ASSERT(uc);
2023 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
2030 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
2031 struct _umtx_time *timeout, int try)
2033 struct abs_timeout timo;
2035 struct umtx_pi *pi, *new_pi;
2036 uint32_t id, owner, old;
2042 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
2046 if (timeout != NULL)
2047 abs_timeout_init2(&timo, timeout);
2049 umtxq_lock(&uq->uq_key);
2050 pi = umtx_pi_lookup(&uq->uq_key);
2052 new_pi = umtx_pi_alloc(M_NOWAIT);
2053 if (new_pi == NULL) {
2054 umtxq_unlock(&uq->uq_key);
2055 new_pi = umtx_pi_alloc(M_WAITOK);
2056 umtxq_lock(&uq->uq_key);
2057 pi = umtx_pi_lookup(&uq->uq_key);
2059 umtx_pi_free(new_pi);
2063 if (new_pi != NULL) {
2064 new_pi->pi_key = uq->uq_key;
2065 umtx_pi_insert(new_pi);
2070 umtxq_unlock(&uq->uq_key);
2073 * Care must be exercised when dealing with umtx structure. It
2074 * can fault on any access.
2078 * Try the uncontested case. This should be done in userland.
2080 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
2081 /* The address was invalid. */
2087 /* The acquire succeeded. */
2088 if (owner == UMUTEX_UNOWNED) {
2093 /* If no one owns it but it is contested try to acquire it. */
2094 if (owner == UMUTEX_CONTESTED) {
2095 rv = casueword32(&m->m_owner,
2096 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
2097 /* The address was invalid. */
2103 if (owner == UMUTEX_CONTESTED) {
2104 umtxq_lock(&uq->uq_key);
2105 umtxq_busy(&uq->uq_key);
2106 error = umtx_pi_claim(pi, td);
2107 umtxq_unbusy(&uq->uq_key);
2108 umtxq_unlock(&uq->uq_key);
2111 * Since we're going to return an
2112 * error, restore the m_owner to its
2113 * previous, unowned state to avoid
2114 * compounding the problem.
2116 (void)casuword32(&m->m_owner,
2117 id | UMUTEX_CONTESTED,
2123 error = umtxq_check_susp(td);
2127 /* If this failed the lock has changed, restart. */
2131 if ((owner & ~UMUTEX_CONTESTED) == id) {
2142 * If we caught a signal, we have retried and now
2148 umtxq_lock(&uq->uq_key);
2149 umtxq_busy(&uq->uq_key);
2150 umtxq_unlock(&uq->uq_key);
2153 * Set the contested bit so that a release in user space
2154 * knows to use the system call for unlock. If this fails
2155 * either some one else has acquired the lock or it has been
2158 rv = casueword32(&m->m_owner, owner, &old,
2159 owner | UMUTEX_CONTESTED);
2161 /* The address was invalid. */
2163 umtxq_unbusy_unlocked(&uq->uq_key);
2168 umtxq_lock(&uq->uq_key);
2170 * We set the contested bit, sleep. Otherwise the lock changed
2171 * and we need to retry or we lost a race to the thread
2172 * unlocking the umtx.
2175 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2176 "umtxpi", timeout == NULL ? NULL : &timo);
2180 umtxq_unbusy(&uq->uq_key);
2181 umtxq_unlock(&uq->uq_key);
2184 error = umtxq_check_susp(td);
2189 umtxq_lock(&uq->uq_key);
2191 umtxq_unlock(&uq->uq_key);
2193 umtx_key_release(&uq->uq_key);
2198 * Unlock a PI mutex.
2201 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags)
2203 struct umtx_key key;
2204 struct umtx_q *uq_first, *uq_first2, *uq_me;
2205 struct umtx_pi *pi, *pi2;
2206 uint32_t owner, old, id;
2213 * Make sure we own this mtx.
2215 error = fueword32(&m->m_owner, &owner);
2219 if ((owner & ~UMUTEX_CONTESTED) != id)
2222 /* This should be done in userland */
2223 if ((owner & UMUTEX_CONTESTED) == 0) {
2224 error = casueword32(&m->m_owner, owner, &old, UMUTEX_UNOWNED);
2232 /* We should only ever be in here for contested locks */
2233 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
2239 count = umtxq_count_pi(&key, &uq_first);
2240 if (uq_first != NULL) {
2241 mtx_lock(&umtx_lock);
2242 pi = uq_first->uq_pi_blocked;
2243 KASSERT(pi != NULL, ("pi == NULL?"));
2244 if (pi->pi_owner != curthread) {
2245 mtx_unlock(&umtx_lock);
2248 umtx_key_release(&key);
2249 /* userland messed the mutex */
2252 uq_me = curthread->td_umtxq;
2254 /* get highest priority thread which is still sleeping. */
2255 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2256 while (uq_first != NULL &&
2257 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2258 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2261 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2262 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2263 if (uq_first2 != NULL) {
2264 if (pri > UPRI(uq_first2->uq_thread))
2265 pri = UPRI(uq_first2->uq_thread);
2268 thread_lock(curthread);
2269 sched_lend_user_prio(curthread, pri);
2270 thread_unlock(curthread);
2271 mtx_unlock(&umtx_lock);
2273 umtxq_signal_thread(uq_first);
2275 pi = umtx_pi_lookup(&key);
2277 * A umtx_pi can exist if a signal or timeout removed the
2278 * last waiter from the umtxq, but there is still
2279 * a thread in do_lock_pi() holding the umtx_pi.
2283 * The umtx_pi can be unowned, such as when a thread
2284 * has just entered do_lock_pi(), allocated the
2285 * umtx_pi, and unlocked the umtxq.
2286 * If the current thread owns it, it must disown it.
2288 mtx_lock(&umtx_lock);
2289 if (pi->pi_owner == td)
2291 mtx_unlock(&umtx_lock);
2297 * When unlocking the umtx, it must be marked as unowned if
2298 * there is zero or one thread only waiting for it.
2299 * Otherwise, it must be marked as contested.
2301 error = casueword32(&m->m_owner, owner, &old,
2302 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
2304 umtxq_unbusy_unlocked(&key);
2305 umtx_key_release(&key);
2317 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2318 struct _umtx_time *timeout, int try)
2320 struct abs_timeout timo;
2321 struct umtx_q *uq, *uq2;
2325 int error, pri, old_inherited_pri, su, rv;
2329 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2333 if (timeout != NULL)
2334 abs_timeout_init2(&timo, timeout);
2336 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2338 old_inherited_pri = uq->uq_inherited_pri;
2339 umtxq_lock(&uq->uq_key);
2340 umtxq_busy(&uq->uq_key);
2341 umtxq_unlock(&uq->uq_key);
2343 rv = fueword32(&m->m_ceilings[0], &ceiling);
2348 ceiling = RTP_PRIO_MAX - ceiling;
2349 if (ceiling > RTP_PRIO_MAX) {
2354 mtx_lock(&umtx_lock);
2355 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2356 mtx_unlock(&umtx_lock);
2360 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2361 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2363 if (uq->uq_inherited_pri < UPRI(td))
2364 sched_lend_user_prio(td, uq->uq_inherited_pri);
2367 mtx_unlock(&umtx_lock);
2369 rv = casueword32(&m->m_owner,
2370 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
2371 /* The address was invalid. */
2377 if (owner == UMUTEX_CONTESTED) {
2382 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
2383 (owner & ~UMUTEX_CONTESTED) == id) {
2394 * If we caught a signal, we have retried and now
2400 umtxq_lock(&uq->uq_key);
2402 umtxq_unbusy(&uq->uq_key);
2403 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2406 umtxq_unlock(&uq->uq_key);
2408 mtx_lock(&umtx_lock);
2409 uq->uq_inherited_pri = old_inherited_pri;
2411 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2412 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2414 if (pri > UPRI(uq2->uq_thread))
2415 pri = UPRI(uq2->uq_thread);
2418 if (pri > uq->uq_inherited_pri)
2419 pri = uq->uq_inherited_pri;
2421 sched_lend_user_prio(td, pri);
2423 mtx_unlock(&umtx_lock);
2427 mtx_lock(&umtx_lock);
2428 uq->uq_inherited_pri = old_inherited_pri;
2430 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2431 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2433 if (pri > UPRI(uq2->uq_thread))
2434 pri = UPRI(uq2->uq_thread);
2437 if (pri > uq->uq_inherited_pri)
2438 pri = uq->uq_inherited_pri;
2440 sched_lend_user_prio(td, pri);
2442 mtx_unlock(&umtx_lock);
2446 umtxq_unbusy_unlocked(&uq->uq_key);
2447 umtx_key_release(&uq->uq_key);
2452 * Unlock a PP mutex.
2455 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags)
2457 struct umtx_key key;
2458 struct umtx_q *uq, *uq2;
2462 int error, pri, new_inherited_pri, su;
2466 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2469 * Make sure we own this mtx.
2471 error = fueword32(&m->m_owner, &owner);
2475 if ((owner & ~UMUTEX_CONTESTED) != id)
2478 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2483 new_inherited_pri = PRI_MAX;
2485 rceiling = RTP_PRIO_MAX - rceiling;
2486 if (rceiling > RTP_PRIO_MAX)
2488 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2491 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2498 * For priority protected mutex, always set unlocked state
2499 * to UMUTEX_CONTESTED, so that userland always enters kernel
2500 * to lock the mutex, it is necessary because thread priority
2501 * has to be adjusted for such mutex.
2503 error = suword32(&m->m_owner, UMUTEX_CONTESTED);
2507 umtxq_signal(&key, 1);
2514 mtx_lock(&umtx_lock);
2516 uq->uq_inherited_pri = new_inherited_pri;
2518 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2519 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2521 if (pri > UPRI(uq2->uq_thread))
2522 pri = UPRI(uq2->uq_thread);
2525 if (pri > uq->uq_inherited_pri)
2526 pri = uq->uq_inherited_pri;
2528 sched_lend_user_prio(td, pri);
2530 mtx_unlock(&umtx_lock);
2532 umtx_key_release(&key);
2537 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2538 uint32_t *old_ceiling)
2541 uint32_t save_ceiling;
2546 error = fueword32(&m->m_flags, &flags);
2549 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2551 if (ceiling > RTP_PRIO_MAX)
2555 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2559 umtxq_lock(&uq->uq_key);
2560 umtxq_busy(&uq->uq_key);
2561 umtxq_unlock(&uq->uq_key);
2563 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2569 rv = casueword32(&m->m_owner,
2570 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
2576 if (owner == UMUTEX_CONTESTED) {
2577 suword32(&m->m_ceilings[0], ceiling);
2578 suword32(&m->m_owner, UMUTEX_CONTESTED);
2583 if ((owner & ~UMUTEX_CONTESTED) == id) {
2584 suword32(&m->m_ceilings[0], ceiling);
2590 * If we caught a signal, we have retried and now
2597 * We set the contested bit, sleep. Otherwise the lock changed
2598 * and we need to retry or we lost a race to the thread
2599 * unlocking the umtx.
2601 umtxq_lock(&uq->uq_key);
2603 umtxq_unbusy(&uq->uq_key);
2604 error = umtxq_sleep(uq, "umtxpp", NULL);
2606 umtxq_unlock(&uq->uq_key);
2608 umtxq_lock(&uq->uq_key);
2610 umtxq_signal(&uq->uq_key, INT_MAX);
2611 umtxq_unbusy(&uq->uq_key);
2612 umtxq_unlock(&uq->uq_key);
2613 umtx_key_release(&uq->uq_key);
2614 if (error == 0 && old_ceiling != NULL)
2615 suword32(old_ceiling, save_ceiling);
2620 * Lock a userland POSIX mutex.
2623 do_lock_umutex(struct thread *td, struct umutex *m,
2624 struct _umtx_time *timeout, int mode)
2629 error = fueword32(&m->m_flags, &flags);
2633 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2635 error = do_lock_normal(td, m, flags, timeout, mode);
2637 case UMUTEX_PRIO_INHERIT:
2638 error = do_lock_pi(td, m, flags, timeout, mode);
2640 case UMUTEX_PRIO_PROTECT:
2641 error = do_lock_pp(td, m, flags, timeout, mode);
2646 if (timeout == NULL) {
2647 if (error == EINTR && mode != _UMUTEX_WAIT)
2650 /* Timed-locking is not restarted. */
2651 if (error == ERESTART)
2658 * Unlock a userland POSIX mutex.
2661 do_unlock_umutex(struct thread *td, struct umutex *m)
2666 error = fueword32(&m->m_flags, &flags);
2670 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2672 return (do_unlock_normal(td, m, flags));
2673 case UMUTEX_PRIO_INHERIT:
2674 return (do_unlock_pi(td, m, flags));
2675 case UMUTEX_PRIO_PROTECT:
2676 return (do_unlock_pp(td, m, flags));
2683 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2684 struct timespec *timeout, u_long wflags)
2686 struct abs_timeout timo;
2688 uint32_t flags, clockid, hasw;
2692 error = fueword32(&cv->c_flags, &flags);
2695 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2699 if ((wflags & CVWAIT_CLOCKID) != 0) {
2700 error = fueword32(&cv->c_clockid, &clockid);
2702 umtx_key_release(&uq->uq_key);
2705 if (clockid < CLOCK_REALTIME ||
2706 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2707 /* hmm, only HW clock id will work. */
2708 umtx_key_release(&uq->uq_key);
2712 clockid = CLOCK_REALTIME;
2715 umtxq_lock(&uq->uq_key);
2716 umtxq_busy(&uq->uq_key);
2718 umtxq_unlock(&uq->uq_key);
2721 * Set c_has_waiters to 1 before releasing user mutex, also
2722 * don't modify cache line when unnecessary.
2724 error = fueword32(&cv->c_has_waiters, &hasw);
2725 if (error == 0 && hasw == 0)
2726 suword32(&cv->c_has_waiters, 1);
2728 umtxq_unbusy_unlocked(&uq->uq_key);
2730 error = do_unlock_umutex(td, m);
2732 if (timeout != NULL)
2733 abs_timeout_init(&timo, clockid, ((wflags & CVWAIT_ABSTIME) != 0),
2736 umtxq_lock(&uq->uq_key);
2738 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2742 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2746 * This must be timeout,interrupted by signal or
2747 * surprious wakeup, clear c_has_waiter flag when
2750 umtxq_busy(&uq->uq_key);
2751 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2752 int oldlen = uq->uq_cur_queue->length;
2755 umtxq_unlock(&uq->uq_key);
2756 suword32(&cv->c_has_waiters, 0);
2757 umtxq_lock(&uq->uq_key);
2760 umtxq_unbusy(&uq->uq_key);
2761 if (error == ERESTART)
2765 umtxq_unlock(&uq->uq_key);
2766 umtx_key_release(&uq->uq_key);
2771 * Signal a userland condition variable.
2774 do_cv_signal(struct thread *td, struct ucond *cv)
2776 struct umtx_key key;
2777 int error, cnt, nwake;
2780 error = fueword32(&cv->c_flags, &flags);
2783 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2787 cnt = umtxq_count(&key);
2788 nwake = umtxq_signal(&key, 1);
2791 error = suword32(&cv->c_has_waiters, 0);
2798 umtx_key_release(&key);
2803 do_cv_broadcast(struct thread *td, struct ucond *cv)
2805 struct umtx_key key;
2809 error = fueword32(&cv->c_flags, &flags);
2812 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2817 umtxq_signal(&key, INT_MAX);
2820 error = suword32(&cv->c_has_waiters, 0);
2824 umtxq_unbusy_unlocked(&key);
2826 umtx_key_release(&key);
2831 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, struct _umtx_time *timeout)
2833 struct abs_timeout timo;
2835 uint32_t flags, wrflags;
2836 int32_t state, oldstate;
2837 int32_t blocked_readers;
2841 error = fueword32(&rwlock->rw_flags, &flags);
2844 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2848 if (timeout != NULL)
2849 abs_timeout_init2(&timo, timeout);
2851 wrflags = URWLOCK_WRITE_OWNER;
2852 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2853 wrflags |= URWLOCK_WRITE_WAITERS;
2856 rv = fueword32(&rwlock->rw_state, &state);
2858 umtx_key_release(&uq->uq_key);
2862 /* try to lock it */
2863 while (!(state & wrflags)) {
2864 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2865 umtx_key_release(&uq->uq_key);
2868 rv = casueword32(&rwlock->rw_state, state,
2869 &oldstate, state + 1);
2871 umtx_key_release(&uq->uq_key);
2874 if (oldstate == state) {
2875 umtx_key_release(&uq->uq_key);
2878 error = umtxq_check_susp(td);
2887 /* grab monitor lock */
2888 umtxq_lock(&uq->uq_key);
2889 umtxq_busy(&uq->uq_key);
2890 umtxq_unlock(&uq->uq_key);
2893 * re-read the state, in case it changed between the try-lock above
2894 * and the check below
2896 rv = fueword32(&rwlock->rw_state, &state);
2900 /* set read contention bit */
2901 while (error == 0 && (state & wrflags) &&
2902 !(state & URWLOCK_READ_WAITERS)) {
2903 rv = casueword32(&rwlock->rw_state, state,
2904 &oldstate, state | URWLOCK_READ_WAITERS);
2909 if (oldstate == state)
2912 error = umtxq_check_susp(td);
2917 umtxq_unbusy_unlocked(&uq->uq_key);
2921 /* state is changed while setting flags, restart */
2922 if (!(state & wrflags)) {
2923 umtxq_unbusy_unlocked(&uq->uq_key);
2924 error = umtxq_check_susp(td);
2931 /* contention bit is set, before sleeping, increase read waiter count */
2932 rv = fueword32(&rwlock->rw_blocked_readers,
2935 umtxq_unbusy_unlocked(&uq->uq_key);
2939 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2941 while (state & wrflags) {
2942 umtxq_lock(&uq->uq_key);
2944 umtxq_unbusy(&uq->uq_key);
2946 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2949 umtxq_busy(&uq->uq_key);
2951 umtxq_unlock(&uq->uq_key);
2954 rv = fueword32(&rwlock->rw_state, &state);
2961 /* decrease read waiter count, and may clear read contention bit */
2962 rv = fueword32(&rwlock->rw_blocked_readers,
2965 umtxq_unbusy_unlocked(&uq->uq_key);
2969 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2970 if (blocked_readers == 1) {
2971 rv = fueword32(&rwlock->rw_state, &state);
2974 while (error == 0) {
2975 rv = casueword32(&rwlock->rw_state, state,
2976 &oldstate, state & ~URWLOCK_READ_WAITERS);
2981 if (oldstate == state)
2984 error = umtxq_check_susp(td);
2988 umtxq_unbusy_unlocked(&uq->uq_key);
2992 umtx_key_release(&uq->uq_key);
2993 if (error == ERESTART)
2999 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
3001 struct abs_timeout timo;
3004 int32_t state, oldstate;
3005 int32_t blocked_writers;
3006 int32_t blocked_readers;
3010 error = fueword32(&rwlock->rw_flags, &flags);
3013 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3017 if (timeout != NULL)
3018 abs_timeout_init2(&timo, timeout);
3020 blocked_readers = 0;
3022 rv = fueword32(&rwlock->rw_state, &state);
3024 umtx_key_release(&uq->uq_key);
3027 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
3028 rv = casueword32(&rwlock->rw_state, state,
3029 &oldstate, state | URWLOCK_WRITE_OWNER);
3031 umtx_key_release(&uq->uq_key);
3034 if (oldstate == state) {
3035 umtx_key_release(&uq->uq_key);
3039 error = umtxq_check_susp(td);
3045 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
3046 blocked_readers != 0) {
3047 umtxq_lock(&uq->uq_key);
3048 umtxq_busy(&uq->uq_key);
3049 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
3050 umtxq_unbusy(&uq->uq_key);
3051 umtxq_unlock(&uq->uq_key);
3057 /* grab monitor lock */
3058 umtxq_lock(&uq->uq_key);
3059 umtxq_busy(&uq->uq_key);
3060 umtxq_unlock(&uq->uq_key);
3063 * re-read the state, in case it changed between the try-lock above
3064 * and the check below
3066 rv = fueword32(&rwlock->rw_state, &state);
3070 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
3071 URWLOCK_READER_COUNT(state) != 0) &&
3072 (state & URWLOCK_WRITE_WAITERS) == 0) {
3073 rv = casueword32(&rwlock->rw_state, state,
3074 &oldstate, state | URWLOCK_WRITE_WAITERS);
3079 if (oldstate == state)
3082 error = umtxq_check_susp(td);
3087 umtxq_unbusy_unlocked(&uq->uq_key);
3091 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
3092 umtxq_unbusy_unlocked(&uq->uq_key);
3093 error = umtxq_check_susp(td);
3099 rv = fueword32(&rwlock->rw_blocked_writers,
3102 umtxq_unbusy_unlocked(&uq->uq_key);
3106 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
3108 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
3109 umtxq_lock(&uq->uq_key);
3110 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3111 umtxq_unbusy(&uq->uq_key);
3113 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
3116 umtxq_busy(&uq->uq_key);
3117 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3118 umtxq_unlock(&uq->uq_key);
3121 rv = fueword32(&rwlock->rw_state, &state);
3128 rv = fueword32(&rwlock->rw_blocked_writers,
3131 umtxq_unbusy_unlocked(&uq->uq_key);
3135 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3136 if (blocked_writers == 1) {
3137 rv = fueword32(&rwlock->rw_state, &state);
3139 umtxq_unbusy_unlocked(&uq->uq_key);
3144 rv = casueword32(&rwlock->rw_state, state,
3145 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3150 if (oldstate == state)
3153 error = umtxq_check_susp(td);
3155 * We are leaving the URWLOCK_WRITE_WAITERS
3156 * behind, but this should not harm the
3162 rv = fueword32(&rwlock->rw_blocked_readers,
3165 umtxq_unbusy_unlocked(&uq->uq_key);
3170 blocked_readers = 0;
3172 umtxq_unbusy_unlocked(&uq->uq_key);
3175 umtx_key_release(&uq->uq_key);
3176 if (error == ERESTART)
3182 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3186 int32_t state, oldstate;
3187 int error, rv, q, count;
3190 error = fueword32(&rwlock->rw_flags, &flags);
3193 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3197 error = fueword32(&rwlock->rw_state, &state);
3202 if (state & URWLOCK_WRITE_OWNER) {
3204 rv = casueword32(&rwlock->rw_state, state,
3205 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3210 if (oldstate != state) {
3212 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3216 error = umtxq_check_susp(td);
3222 } else if (URWLOCK_READER_COUNT(state) != 0) {
3224 rv = casueword32(&rwlock->rw_state, state,
3225 &oldstate, state - 1);
3230 if (oldstate != state) {
3232 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3236 error = umtxq_check_susp(td);
3249 if (!(flags & URWLOCK_PREFER_READER)) {
3250 if (state & URWLOCK_WRITE_WAITERS) {
3252 q = UMTX_EXCLUSIVE_QUEUE;
3253 } else if (state & URWLOCK_READ_WAITERS) {
3255 q = UMTX_SHARED_QUEUE;
3258 if (state & URWLOCK_READ_WAITERS) {
3260 q = UMTX_SHARED_QUEUE;
3261 } else if (state & URWLOCK_WRITE_WAITERS) {
3263 q = UMTX_EXCLUSIVE_QUEUE;
3268 umtxq_lock(&uq->uq_key);
3269 umtxq_busy(&uq->uq_key);
3270 umtxq_signal_queue(&uq->uq_key, count, q);
3271 umtxq_unbusy(&uq->uq_key);
3272 umtxq_unlock(&uq->uq_key);
3275 umtx_key_release(&uq->uq_key);
3280 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3282 struct abs_timeout timo;
3284 uint32_t flags, count, count1;
3288 error = fueword32(&sem->_flags, &flags);
3291 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3295 if (timeout != NULL)
3296 abs_timeout_init2(&timo, timeout);
3298 umtxq_lock(&uq->uq_key);
3299 umtxq_busy(&uq->uq_key);
3301 umtxq_unlock(&uq->uq_key);
3302 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3304 rv = fueword32(&sem->_count, &count);
3305 if (rv == -1 || count != 0) {
3306 umtxq_lock(&uq->uq_key);
3307 umtxq_unbusy(&uq->uq_key);
3309 umtxq_unlock(&uq->uq_key);
3310 umtx_key_release(&uq->uq_key);
3311 return (rv == -1 ? EFAULT : 0);
3313 umtxq_lock(&uq->uq_key);
3314 umtxq_unbusy(&uq->uq_key);
3316 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3318 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3322 /* A relative timeout cannot be restarted. */
3323 if (error == ERESTART && timeout != NULL &&
3324 (timeout->_flags & UMTX_ABSTIME) == 0)
3327 umtxq_unlock(&uq->uq_key);
3328 umtx_key_release(&uq->uq_key);
3333 * Signal a userland condition variable.
3336 do_sem_wake(struct thread *td, struct _usem *sem)
3338 struct umtx_key key;
3342 error = fueword32(&sem->_flags, &flags);
3345 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3349 cnt = umtxq_count(&key);
3351 umtxq_signal(&key, 1);
3353 * Check if count is greater than 0, this means the memory is
3354 * still being referenced by user code, so we can safely
3355 * update _has_waiters flag.
3359 error = suword32(&sem->_has_waiters, 0);
3367 umtx_key_release(&key);
3372 sys__umtx_lock(struct thread *td, struct _umtx_lock_args *uap)
3373 /* struct umtx *umtx */
3375 return do_lock_umtx(td, uap->umtx, td->td_tid, 0);
3379 sys__umtx_unlock(struct thread *td, struct _umtx_unlock_args *uap)
3380 /* struct umtx *umtx */
3382 return do_unlock_umtx(td, uap->umtx, td->td_tid);
3386 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3390 error = copyin(addr, tsp, sizeof(struct timespec));
3392 if (tsp->tv_sec < 0 ||
3393 tsp->tv_nsec >= 1000000000 ||
3401 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3405 if (size <= sizeof(struct timespec)) {
3406 tp->_clockid = CLOCK_REALTIME;
3408 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3410 error = copyin(addr, tp, sizeof(struct _umtx_time));
3413 if (tp->_timeout.tv_sec < 0 ||
3414 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3420 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap)
3422 struct timespec *ts, timeout;
3425 /* Allow a null timespec (wait forever). */
3426 if (uap->uaddr2 == NULL)
3429 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3434 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3438 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap)
3440 return (do_unlock_umtx(td, uap->obj, uap->val));
3444 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3446 struct _umtx_time timeout, *tm_p;
3449 if (uap->uaddr2 == NULL)
3452 error = umtx_copyin_umtx_time(
3453 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3458 return do_wait(td, uap->obj, uap->val, tm_p, 0, 0);
3462 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3464 struct _umtx_time timeout, *tm_p;
3467 if (uap->uaddr2 == NULL)
3470 error = umtx_copyin_umtx_time(
3471 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3476 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3480 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3482 struct _umtx_time *tm_p, timeout;
3485 if (uap->uaddr2 == NULL)
3488 error = umtx_copyin_umtx_time(
3489 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3494 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3498 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3500 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3503 #define BATCH_SIZE 128
3505 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3507 int count = uap->val;
3508 void *uaddrs[BATCH_SIZE];
3509 char **upp = (char **)uap->obj;
3516 if (tocopy > BATCH_SIZE)
3517 tocopy = BATCH_SIZE;
3518 error = copyin(upp+pos, uaddrs, tocopy * sizeof(char *));
3521 for (i = 0; i < tocopy; ++i)
3522 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3530 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3532 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3536 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3538 struct _umtx_time *tm_p, timeout;
3541 /* Allow a null timespec (wait forever). */
3542 if (uap->uaddr2 == NULL)
3545 error = umtx_copyin_umtx_time(
3546 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3551 return do_lock_umutex(td, uap->obj, tm_p, 0);
3555 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3557 return do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY);
3561 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3563 struct _umtx_time *tm_p, timeout;
3566 /* Allow a null timespec (wait forever). */
3567 if (uap->uaddr2 == NULL)
3570 error = umtx_copyin_umtx_time(
3571 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3576 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3580 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3582 return do_wake_umutex(td, uap->obj);
3586 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3588 return do_unlock_umutex(td, uap->obj);
3592 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3594 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1);
3598 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3600 struct timespec *ts, timeout;
3603 /* Allow a null timespec (wait forever). */
3604 if (uap->uaddr2 == NULL)
3607 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3612 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3616 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3618 return do_cv_signal(td, uap->obj);
3622 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3624 return do_cv_broadcast(td, uap->obj);
3628 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3630 struct _umtx_time timeout;
3633 /* Allow a null timespec (wait forever). */
3634 if (uap->uaddr2 == NULL) {
3635 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3637 error = umtx_copyin_umtx_time(uap->uaddr2,
3638 (size_t)uap->uaddr1, &timeout);
3641 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3647 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3649 struct _umtx_time timeout;
3652 /* Allow a null timespec (wait forever). */
3653 if (uap->uaddr2 == NULL) {
3654 error = do_rw_wrlock(td, uap->obj, 0);
3656 error = umtx_copyin_umtx_time(uap->uaddr2,
3657 (size_t)uap->uaddr1, &timeout);
3661 error = do_rw_wrlock(td, uap->obj, &timeout);
3667 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3669 return do_rw_unlock(td, uap->obj);
3673 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3675 struct _umtx_time *tm_p, timeout;
3678 /* Allow a null timespec (wait forever). */
3679 if (uap->uaddr2 == NULL)
3682 error = umtx_copyin_umtx_time(
3683 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3688 return (do_sem_wait(td, uap->obj, tm_p));
3692 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3694 return do_sem_wake(td, uap->obj);
3698 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3700 return do_wake2_umutex(td, uap->obj, uap->val);
3703 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3705 static _umtx_op_func op_table[] = {
3706 __umtx_op_lock_umtx, /* UMTX_OP_LOCK */
3707 __umtx_op_unlock_umtx, /* UMTX_OP_UNLOCK */
3708 __umtx_op_wait, /* UMTX_OP_WAIT */
3709 __umtx_op_wake, /* UMTX_OP_WAKE */
3710 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_TRYLOCK */
3711 __umtx_op_lock_umutex, /* UMTX_OP_MUTEX_LOCK */
3712 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3713 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3714 __umtx_op_cv_wait, /* UMTX_OP_CV_WAIT*/
3715 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3716 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3717 __umtx_op_wait_uint, /* UMTX_OP_WAIT_UINT */
3718 __umtx_op_rw_rdlock, /* UMTX_OP_RW_RDLOCK */
3719 __umtx_op_rw_wrlock, /* UMTX_OP_RW_WRLOCK */
3720 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3721 __umtx_op_wait_uint_private, /* UMTX_OP_WAIT_UINT_PRIVATE */
3722 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3723 __umtx_op_wait_umutex, /* UMTX_OP_UMUTEX_WAIT */
3724 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
3725 __umtx_op_sem_wait, /* UMTX_OP_SEM_WAIT */
3726 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3727 __umtx_op_nwake_private, /* UMTX_OP_NWAKE_PRIVATE */
3728 __umtx_op_wake2_umutex /* UMTX_OP_UMUTEX_WAKE2 */
3732 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
3734 if ((unsigned)uap->op < UMTX_OP_MAX)
3735 return (*op_table[uap->op])(td, uap);
3739 #ifdef COMPAT_FREEBSD32
3741 freebsd32_umtx_lock(struct thread *td, struct freebsd32_umtx_lock_args *uap)
3742 /* struct umtx *umtx */
3744 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
3748 freebsd32_umtx_unlock(struct thread *td, struct freebsd32_umtx_unlock_args *uap)
3749 /* struct umtx *umtx */
3751 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
3759 struct umtx_time32 {
3760 struct timespec32 timeout;
3766 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
3768 struct timespec32 ts32;
3771 error = copyin(addr, &ts32, sizeof(struct timespec32));
3773 if (ts32.tv_sec < 0 ||
3774 ts32.tv_nsec >= 1000000000 ||
3778 tsp->tv_sec = ts32.tv_sec;
3779 tsp->tv_nsec = ts32.tv_nsec;
3786 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
3788 struct umtx_time32 t32;
3791 t32.clockid = CLOCK_REALTIME;
3793 if (size <= sizeof(struct timespec32))
3794 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
3796 error = copyin(addr, &t32, sizeof(struct umtx_time32));
3799 if (t32.timeout.tv_sec < 0 ||
3800 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
3802 tp->_timeout.tv_sec = t32.timeout.tv_sec;
3803 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
3804 tp->_flags = t32.flags;
3805 tp->_clockid = t32.clockid;
3810 __umtx_op_lock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
3812 struct timespec *ts, timeout;
3815 /* Allow a null timespec (wait forever). */
3816 if (uap->uaddr2 == NULL)
3819 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3824 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3828 __umtx_op_unlock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
3830 return (do_unlock_umtx32(td, uap->obj, (uint32_t)uap->val));
3834 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3836 struct _umtx_time *tm_p, timeout;
3839 if (uap->uaddr2 == NULL)
3842 error = umtx_copyin_umtx_time32(uap->uaddr2,
3843 (size_t)uap->uaddr1, &timeout);
3848 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3852 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3854 struct _umtx_time *tm_p, timeout;
3857 /* Allow a null timespec (wait forever). */
3858 if (uap->uaddr2 == NULL)
3861 error = umtx_copyin_umtx_time(uap->uaddr2,
3862 (size_t)uap->uaddr1, &timeout);
3867 return do_lock_umutex(td, uap->obj, tm_p, 0);
3871 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3873 struct _umtx_time *tm_p, timeout;
3876 /* Allow a null timespec (wait forever). */
3877 if (uap->uaddr2 == NULL)
3880 error = umtx_copyin_umtx_time32(uap->uaddr2,
3881 (size_t)uap->uaddr1, &timeout);
3886 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3890 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3892 struct timespec *ts, timeout;
3895 /* Allow a null timespec (wait forever). */
3896 if (uap->uaddr2 == NULL)
3899 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3904 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3908 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3910 struct _umtx_time timeout;
3913 /* Allow a null timespec (wait forever). */
3914 if (uap->uaddr2 == NULL) {
3915 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3917 error = umtx_copyin_umtx_time32(uap->uaddr2,
3918 (size_t)uap->uaddr1, &timeout);
3921 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3927 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3929 struct _umtx_time timeout;
3932 /* Allow a null timespec (wait forever). */
3933 if (uap->uaddr2 == NULL) {
3934 error = do_rw_wrlock(td, uap->obj, 0);
3936 error = umtx_copyin_umtx_time32(uap->uaddr2,
3937 (size_t)uap->uaddr1, &timeout);
3940 error = do_rw_wrlock(td, uap->obj, &timeout);
3946 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3948 struct _umtx_time *tm_p, timeout;
3951 if (uap->uaddr2 == NULL)
3954 error = umtx_copyin_umtx_time32(
3955 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
3960 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3964 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3966 struct _umtx_time *tm_p, timeout;
3969 /* Allow a null timespec (wait forever). */
3970 if (uap->uaddr2 == NULL)
3973 error = umtx_copyin_umtx_time32(uap->uaddr2,
3974 (size_t)uap->uaddr1, &timeout);
3979 return (do_sem_wait(td, uap->obj, tm_p));
3983 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
3985 int count = uap->val;
3986 uint32_t uaddrs[BATCH_SIZE];
3987 uint32_t **upp = (uint32_t **)uap->obj;
3994 if (tocopy > BATCH_SIZE)
3995 tocopy = BATCH_SIZE;
3996 error = copyin(upp+pos, uaddrs, tocopy * sizeof(uint32_t));
3999 for (i = 0; i < tocopy; ++i)
4000 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
4008 static _umtx_op_func op_table_compat32[] = {
4009 __umtx_op_lock_umtx_compat32, /* UMTX_OP_LOCK */
4010 __umtx_op_unlock_umtx_compat32, /* UMTX_OP_UNLOCK */
4011 __umtx_op_wait_compat32, /* UMTX_OP_WAIT */
4012 __umtx_op_wake, /* UMTX_OP_WAKE */
4013 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_LOCK */
4014 __umtx_op_lock_umutex_compat32, /* UMTX_OP_MUTEX_TRYLOCK */
4015 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
4016 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
4017 __umtx_op_cv_wait_compat32, /* UMTX_OP_CV_WAIT*/
4018 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
4019 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
4020 __umtx_op_wait_compat32, /* UMTX_OP_WAIT_UINT */
4021 __umtx_op_rw_rdlock_compat32, /* UMTX_OP_RW_RDLOCK */
4022 __umtx_op_rw_wrlock_compat32, /* UMTX_OP_RW_WRLOCK */
4023 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
4024 __umtx_op_wait_uint_private_compat32, /* UMTX_OP_WAIT_UINT_PRIVATE */
4025 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
4026 __umtx_op_wait_umutex_compat32, /* UMTX_OP_UMUTEX_WAIT */
4027 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
4028 __umtx_op_sem_wait_compat32, /* UMTX_OP_SEM_WAIT */
4029 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
4030 __umtx_op_nwake_private32, /* UMTX_OP_NWAKE_PRIVATE */
4031 __umtx_op_wake2_umutex /* UMTX_OP_UMUTEX_WAKE2 */
4035 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
4037 if ((unsigned)uap->op < UMTX_OP_MAX)
4038 return (*op_table_compat32[uap->op])(td,
4039 (struct _umtx_op_args *)uap);
4045 umtx_thread_init(struct thread *td)
4047 td->td_umtxq = umtxq_alloc();
4048 td->td_umtxq->uq_thread = td;
4052 umtx_thread_fini(struct thread *td)
4054 umtxq_free(td->td_umtxq);
4058 * It will be called when new thread is created, e.g fork().
4061 umtx_thread_alloc(struct thread *td)
4066 uq->uq_inherited_pri = PRI_MAX;
4068 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4069 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4070 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4071 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4078 umtx_exec_hook(void *arg __unused, struct proc *p __unused,
4079 struct image_params *imgp __unused)
4081 umtx_thread_cleanup(curthread);
4085 * thread_exit() hook.
4088 umtx_thread_exit(struct thread *td)
4090 umtx_thread_cleanup(td);
4094 * clean up umtx data.
4097 umtx_thread_cleanup(struct thread *td)
4102 if ((uq = td->td_umtxq) == NULL)
4105 mtx_lock(&umtx_lock);
4106 uq->uq_inherited_pri = PRI_MAX;
4107 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4108 pi->pi_owner = NULL;
4109 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4111 mtx_unlock(&umtx_lock);
4113 sched_lend_user_prio(td, PRI_MAX);