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)
172 #define UMTXQ_BUSY_ASSERT(uc) KASSERT(&(uc)->uc_busy, ("umtx chain is not busy"))
175 * Don't propagate time-sharing priority, there is a security reason,
176 * a user can simply introduce PI-mutex, let thread A lock the mutex,
177 * and let another thread B block on the mutex, because B is
178 * sleeping, its priority will be boosted, this causes A's priority to
179 * be boosted via priority propagating too and will never be lowered even
180 * if it is using 100%CPU, this is unfair to other processes.
183 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
184 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
185 PRI_MAX_TIMESHARE : (td)->td_user_pri)
187 #define GOLDEN_RATIO_PRIME 2654404609U
188 #define UMTX_CHAINS 512
189 #define UMTX_SHIFTS (__WORD_BIT - 9)
191 #define GET_SHARE(flags) \
192 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
194 #define BUSY_SPINS 200
202 static uma_zone_t umtx_pi_zone;
203 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
204 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
205 static int umtx_pi_allocated;
207 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
208 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
209 &umtx_pi_allocated, 0, "Allocated umtx_pi");
211 #ifdef UMTX_PROFILING
212 static long max_length;
213 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
214 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD, 0, "umtx chain stats");
217 static void umtxq_sysinit(void *);
218 static void umtxq_hash(struct umtx_key *key);
219 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
220 static void umtxq_lock(struct umtx_key *key);
221 static void umtxq_unlock(struct umtx_key *key);
222 static void umtxq_busy(struct umtx_key *key);
223 static void umtxq_unbusy(struct umtx_key *key);
224 static void umtxq_insert_queue(struct umtx_q *uq, int q);
225 static void umtxq_remove_queue(struct umtx_q *uq, int q);
226 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
227 static int umtxq_count(struct umtx_key *key);
228 static struct umtx_pi *umtx_pi_alloc(int);
229 static void umtx_pi_free(struct umtx_pi *pi);
230 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags);
231 static void umtx_thread_cleanup(struct thread *td);
232 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
233 struct image_params *imgp __unused);
234 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
236 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
237 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
238 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
240 static struct mtx umtx_lock;
242 #ifdef UMTX_PROFILING
244 umtx_init_profiling(void)
246 struct sysctl_oid *chain_oid;
250 for (i = 0; i < UMTX_CHAINS; ++i) {
251 snprintf(chain_name, sizeof(chain_name), "%d", i);
252 chain_oid = SYSCTL_ADD_NODE(NULL,
253 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
254 chain_name, CTLFLAG_RD, NULL, "umtx hash stats");
255 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
256 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
257 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
258 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
263 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
267 struct umtxq_chain *uc;
268 u_int fract, i, j, tot, whole;
269 u_int sf0, sf1, sf2, sf3, sf4;
270 u_int si0, si1, si2, si3, si4;
271 u_int sw0, sw1, sw2, sw3, sw4;
273 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
274 for (i = 0; i < 2; i++) {
276 for (j = 0; j < UMTX_CHAINS; ++j) {
277 uc = &umtxq_chains[i][j];
278 mtx_lock(&uc->uc_lock);
279 tot += uc->max_length;
280 mtx_unlock(&uc->uc_lock);
283 sbuf_printf(&sb, "%u) Empty ", i);
285 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
286 si0 = si1 = si2 = si3 = si4 = 0;
287 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
288 for (j = 0; j < UMTX_CHAINS; j++) {
289 uc = &umtxq_chains[i][j];
290 mtx_lock(&uc->uc_lock);
291 whole = uc->max_length * 100;
292 mtx_unlock(&uc->uc_lock);
293 fract = (whole % tot) * 100;
294 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
298 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
303 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
308 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
313 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
320 sbuf_printf(&sb, "queue %u:\n", i);
321 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
323 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
325 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
327 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
329 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
335 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
341 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
343 struct umtxq_chain *uc;
348 error = sysctl_handle_int(oidp, &clear, 0, req);
349 if (error != 0 || req->newptr == NULL)
353 for (i = 0; i < 2; ++i) {
354 for (j = 0; j < UMTX_CHAINS; ++j) {
355 uc = &umtxq_chains[i][j];
356 mtx_lock(&uc->uc_lock);
359 mtx_unlock(&uc->uc_lock);
366 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
367 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
368 sysctl_debug_umtx_chains_clear, "I", "Clear umtx chains statistics");
369 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
370 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
371 sysctl_debug_umtx_chains_peaks, "A", "Highest peaks in chains max length");
375 umtxq_sysinit(void *arg __unused)
379 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
380 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
381 for (i = 0; i < 2; ++i) {
382 for (j = 0; j < UMTX_CHAINS; ++j) {
383 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
384 MTX_DEF | MTX_DUPOK);
385 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
386 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
387 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
388 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
389 umtxq_chains[i][j].uc_busy = 0;
390 umtxq_chains[i][j].uc_waiters = 0;
391 #ifdef UMTX_PROFILING
392 umtxq_chains[i][j].length = 0;
393 umtxq_chains[i][j].max_length = 0;
397 #ifdef UMTX_PROFILING
398 umtx_init_profiling();
400 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_SPIN);
401 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
402 EVENTHANDLER_PRI_ANY);
410 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
411 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX, M_WAITOK | M_ZERO);
412 TAILQ_INIT(&uq->uq_spare_queue->head);
413 TAILQ_INIT(&uq->uq_pi_contested);
414 uq->uq_inherited_pri = PRI_MAX;
419 umtxq_free(struct umtx_q *uq)
421 MPASS(uq->uq_spare_queue != NULL);
422 free(uq->uq_spare_queue, M_UMTX);
427 umtxq_hash(struct umtx_key *key)
429 unsigned n = (uintptr_t)key->info.both.a + key->info.both.b;
430 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
433 static inline struct umtxq_chain *
434 umtxq_getchain(struct umtx_key *key)
436 if (key->type <= TYPE_SEM)
437 return (&umtxq_chains[1][key->hash]);
438 return (&umtxq_chains[0][key->hash]);
445 umtxq_lock(struct umtx_key *key)
447 struct umtxq_chain *uc;
449 uc = umtxq_getchain(key);
450 mtx_lock(&uc->uc_lock);
457 umtxq_unlock(struct umtx_key *key)
459 struct umtxq_chain *uc;
461 uc = umtxq_getchain(key);
462 mtx_unlock(&uc->uc_lock);
466 * Set chain to busy state when following operation
467 * may be blocked (kernel mutex can not be used).
470 umtxq_busy(struct umtx_key *key)
472 struct umtxq_chain *uc;
474 uc = umtxq_getchain(key);
475 mtx_assert(&uc->uc_lock, MA_OWNED);
479 int count = BUSY_SPINS;
482 while (uc->uc_busy && --count > 0)
488 while (uc->uc_busy) {
490 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
501 umtxq_unbusy(struct umtx_key *key)
503 struct umtxq_chain *uc;
505 uc = umtxq_getchain(key);
506 mtx_assert(&uc->uc_lock, MA_OWNED);
507 KASSERT(uc->uc_busy != 0, ("not busy"));
513 static struct umtxq_queue *
514 umtxq_queue_lookup(struct umtx_key *key, int q)
516 struct umtxq_queue *uh;
517 struct umtxq_chain *uc;
519 uc = umtxq_getchain(key);
520 UMTXQ_LOCKED_ASSERT(uc);
521 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
522 if (umtx_key_match(&uh->key, key))
530 umtxq_insert_queue(struct umtx_q *uq, int q)
532 struct umtxq_queue *uh;
533 struct umtxq_chain *uc;
535 uc = umtxq_getchain(&uq->uq_key);
536 UMTXQ_LOCKED_ASSERT(uc);
537 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
538 uh = umtxq_queue_lookup(&uq->uq_key, q);
540 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
542 uh = uq->uq_spare_queue;
543 uh->key = uq->uq_key;
544 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
545 #ifdef UMTX_PROFILING
547 if (uc->length > uc->max_length) {
548 uc->max_length = uc->length;
549 if (uc->max_length > max_length)
550 max_length = uc->max_length;
554 uq->uq_spare_queue = NULL;
556 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
558 uq->uq_flags |= UQF_UMTXQ;
559 uq->uq_cur_queue = uh;
564 umtxq_remove_queue(struct umtx_q *uq, int q)
566 struct umtxq_chain *uc;
567 struct umtxq_queue *uh;
569 uc = umtxq_getchain(&uq->uq_key);
570 UMTXQ_LOCKED_ASSERT(uc);
571 if (uq->uq_flags & UQF_UMTXQ) {
572 uh = uq->uq_cur_queue;
573 TAILQ_REMOVE(&uh->head, uq, uq_link);
575 uq->uq_flags &= ~UQF_UMTXQ;
576 if (TAILQ_EMPTY(&uh->head)) {
577 KASSERT(uh->length == 0,
578 ("inconsistent umtxq_queue length"));
579 #ifdef UMTX_PROFILING
582 LIST_REMOVE(uh, link);
584 uh = LIST_FIRST(&uc->uc_spare_queue);
585 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
586 LIST_REMOVE(uh, link);
588 uq->uq_spare_queue = uh;
589 uq->uq_cur_queue = NULL;
594 * Check if there are multiple waiters
597 umtxq_count(struct umtx_key *key)
599 struct umtxq_chain *uc;
600 struct umtxq_queue *uh;
602 uc = umtxq_getchain(key);
603 UMTXQ_LOCKED_ASSERT(uc);
604 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
611 * Check if there are multiple PI waiters and returns first
615 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
617 struct umtxq_chain *uc;
618 struct umtxq_queue *uh;
621 uc = umtxq_getchain(key);
622 UMTXQ_LOCKED_ASSERT(uc);
623 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
625 *first = TAILQ_FIRST(&uh->head);
632 umtxq_check_susp(struct thread *td)
638 * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
639 * eventually break the lockstep loop.
641 if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
646 if (P_SHOULDSTOP(p) ||
647 ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) {
648 if (p->p_flag & P_SINGLE_EXIT)
658 * Wake up threads waiting on an userland object.
662 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
664 struct umtxq_chain *uc;
665 struct umtxq_queue *uh;
670 uc = umtxq_getchain(key);
671 UMTXQ_LOCKED_ASSERT(uc);
672 uh = umtxq_queue_lookup(key, q);
674 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
675 umtxq_remove_queue(uq, q);
686 * Wake up specified thread.
689 umtxq_signal_thread(struct umtx_q *uq)
691 struct umtxq_chain *uc;
693 uc = umtxq_getchain(&uq->uq_key);
694 UMTXQ_LOCKED_ASSERT(uc);
700 tstohz(const struct timespec *tsp)
704 TIMESPEC_TO_TIMEVAL(&tv, tsp);
709 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
710 const struct timespec *timeout)
713 timo->clockid = clockid;
715 kern_clock_gettime(curthread, clockid, &timo->end);
716 timo->cur = timo->end;
717 timespecadd(&timo->end, timeout);
719 timo->end = *timeout;
720 kern_clock_gettime(curthread, clockid, &timo->cur);
725 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
728 abs_timeout_init(timo, umtxtime->_clockid,
729 (umtxtime->_flags & UMTX_ABSTIME) != 0,
730 &umtxtime->_timeout);
734 abs_timeout_update(struct abs_timeout *timo)
736 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
740 abs_timeout_gethz(struct abs_timeout *timo)
744 if (timespeccmp(&timo->end, &timo->cur, <=))
747 timespecsub(&tts, &timo->cur);
748 return (tstohz(&tts));
752 * Put thread into sleep state, before sleeping, check if
753 * thread was removed from umtx queue.
756 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
758 struct umtxq_chain *uc;
761 uc = umtxq_getchain(&uq->uq_key);
762 UMTXQ_LOCKED_ASSERT(uc);
764 if (!(uq->uq_flags & UQF_UMTXQ))
766 if (abstime != NULL) {
767 timo = abs_timeout_gethz(abstime);
772 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
773 if (error != EWOULDBLOCK) {
774 umtxq_lock(&uq->uq_key);
778 abs_timeout_update(abstime);
779 umtxq_lock(&uq->uq_key);
785 * Convert userspace address into unique logical address.
788 umtx_key_get(void *addr, int type, int share, struct umtx_key *key)
790 struct thread *td = curthread;
792 vm_map_entry_t entry;
798 if (share == THREAD_SHARE) {
800 key->info.private.vs = td->td_proc->p_vmspace;
801 key->info.private.addr = (uintptr_t)addr;
803 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
804 map = &td->td_proc->p_vmspace->vm_map;
805 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
806 &entry, &key->info.shared.object, &pindex, &prot,
807 &wired) != KERN_SUCCESS) {
811 if ((share == PROCESS_SHARE) ||
812 (share == AUTO_SHARE &&
813 VM_INHERIT_SHARE == entry->inheritance)) {
815 key->info.shared.offset = entry->offset + entry->start -
817 vm_object_reference(key->info.shared.object);
820 key->info.private.vs = td->td_proc->p_vmspace;
821 key->info.private.addr = (uintptr_t)addr;
823 vm_map_lookup_done(map, entry);
834 umtx_key_release(struct umtx_key *key)
837 vm_object_deallocate(key->info.shared.object);
841 * Lock a umtx object.
844 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
845 const struct timespec *timeout)
847 struct abs_timeout timo;
855 abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
858 * Care must be exercised when dealing with umtx structure. It
859 * can fault on any access.
863 * Try the uncontested case. This should be done in userland.
865 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
867 /* The acquire succeeded. */
868 if (owner == UMTX_UNOWNED)
871 /* The address was invalid. */
875 /* If no one owns it but it is contested try to acquire it. */
876 if (owner == UMTX_CONTESTED) {
877 owner = casuword(&umtx->u_owner,
878 UMTX_CONTESTED, id | UMTX_CONTESTED);
880 if (owner == UMTX_CONTESTED)
883 /* The address was invalid. */
887 error = umtxq_check_susp(td);
891 /* If this failed the lock has changed, restart. */
896 * If we caught a signal, we have retried and now
902 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
903 AUTO_SHARE, &uq->uq_key)) != 0)
906 umtxq_lock(&uq->uq_key);
907 umtxq_busy(&uq->uq_key);
909 umtxq_unbusy(&uq->uq_key);
910 umtxq_unlock(&uq->uq_key);
913 * Set the contested bit so that a release in user space
914 * knows to use the system call for unlock. If this fails
915 * either some one else has acquired the lock or it has been
918 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
920 /* The address was invalid. */
922 umtxq_lock(&uq->uq_key);
924 umtxq_unlock(&uq->uq_key);
925 umtx_key_release(&uq->uq_key);
930 * We set the contested bit, sleep. Otherwise the lock changed
931 * and we need to retry or we lost a race to the thread
932 * unlocking the umtx.
934 umtxq_lock(&uq->uq_key);
936 error = umtxq_sleep(uq, "umtx", timeout == NULL ? NULL :
939 umtxq_unlock(&uq->uq_key);
940 umtx_key_release(&uq->uq_key);
943 error = umtxq_check_susp(td);
946 if (timeout == NULL) {
947 /* Mutex locking is restarted if it is interrupted. */
951 /* Timed-locking is not restarted. */
952 if (error == ERESTART)
959 * Unlock a umtx object.
962 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
971 * Make sure we own this mtx.
973 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
977 if ((owner & ~UMTX_CONTESTED) != id)
980 /* This should be done in userland */
981 if ((owner & UMTX_CONTESTED) == 0) {
982 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
990 /* We should only ever be in here for contested locks */
991 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
997 count = umtxq_count(&key);
1001 * When unlocking the umtx, it must be marked as unowned if
1002 * there is zero or one thread only waiting for it.
1003 * Otherwise, it must be marked as contested.
1005 old = casuword(&umtx->u_owner, owner,
1006 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
1008 umtxq_signal(&key,1);
1011 umtx_key_release(&key);
1019 #ifdef COMPAT_FREEBSD32
1022 * Lock a umtx object.
1025 do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id,
1026 const struct timespec *timeout)
1028 struct abs_timeout timo;
1036 if (timeout != NULL)
1037 abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
1040 * Care must be exercised when dealing with umtx structure. It
1041 * can fault on any access.
1045 * Try the uncontested case. This should be done in userland.
1047 owner = casuword32(m, UMUTEX_UNOWNED, id);
1049 /* The acquire succeeded. */
1050 if (owner == UMUTEX_UNOWNED)
1053 /* The address was invalid. */
1057 /* If no one owns it but it is contested try to acquire it. */
1058 if (owner == UMUTEX_CONTESTED) {
1059 owner = casuword32(m,
1060 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1061 if (owner == UMUTEX_CONTESTED)
1064 /* The address was invalid. */
1068 error = umtxq_check_susp(td);
1072 /* If this failed the lock has changed, restart. */
1077 * If we caught a signal, we have retried and now
1083 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
1084 AUTO_SHARE, &uq->uq_key)) != 0)
1087 umtxq_lock(&uq->uq_key);
1088 umtxq_busy(&uq->uq_key);
1090 umtxq_unbusy(&uq->uq_key);
1091 umtxq_unlock(&uq->uq_key);
1094 * Set the contested bit so that a release in user space
1095 * knows to use the system call for unlock. If this fails
1096 * either some one else has acquired the lock or it has been
1099 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
1101 /* The address was invalid. */
1103 umtxq_lock(&uq->uq_key);
1105 umtxq_unlock(&uq->uq_key);
1106 umtx_key_release(&uq->uq_key);
1111 * We set the contested bit, sleep. Otherwise the lock changed
1112 * and we need to retry or we lost a race to the thread
1113 * unlocking the umtx.
1115 umtxq_lock(&uq->uq_key);
1117 error = umtxq_sleep(uq, "umtx", timeout == NULL ?
1120 umtxq_unlock(&uq->uq_key);
1121 umtx_key_release(&uq->uq_key);
1124 error = umtxq_check_susp(td);
1127 if (timeout == NULL) {
1128 /* Mutex locking is restarted if it is interrupted. */
1132 /* Timed-locking is not restarted. */
1133 if (error == ERESTART)
1140 * Unlock a umtx object.
1143 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
1145 struct umtx_key key;
1152 * Make sure we own this mtx.
1154 owner = fuword32(m);
1158 if ((owner & ~UMUTEX_CONTESTED) != id)
1161 /* This should be done in userland */
1162 if ((owner & UMUTEX_CONTESTED) == 0) {
1163 old = casuword32(m, owner, UMUTEX_UNOWNED);
1171 /* We should only ever be in here for contested locks */
1172 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1178 count = umtxq_count(&key);
1182 * When unlocking the umtx, it must be marked as unowned if
1183 * there is zero or one thread only waiting for it.
1184 * Otherwise, it must be marked as contested.
1186 old = casuword32(m, owner,
1187 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1189 umtxq_signal(&key,1);
1192 umtx_key_release(&key);
1202 * Fetch and compare value, sleep on the address if value is not changed.
1205 do_wait(struct thread *td, void *addr, u_long id,
1206 struct _umtx_time *timeout, int compat32, int is_private)
1208 struct abs_timeout timo;
1214 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1215 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1218 if (timeout != NULL)
1219 abs_timeout_init2(&timo, timeout);
1221 umtxq_lock(&uq->uq_key);
1223 umtxq_unlock(&uq->uq_key);
1227 tmp = (unsigned int)fuword32(addr);
1228 umtxq_lock(&uq->uq_key);
1230 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
1232 if ((uq->uq_flags & UQF_UMTXQ) == 0)
1236 umtxq_unlock(&uq->uq_key);
1237 umtx_key_release(&uq->uq_key);
1238 if (error == ERESTART)
1244 * Wake up threads sleeping on the specified address.
1247 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1249 struct umtx_key key;
1252 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1253 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1256 ret = umtxq_signal(&key, n_wake);
1258 umtx_key_release(&key);
1263 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1266 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1267 struct _umtx_time *timeout, int mode)
1269 struct abs_timeout timo;
1271 uint32_t owner, old, id;
1277 if (timeout != NULL)
1278 abs_timeout_init2(&timo, timeout);
1281 * Care must be exercised when dealing with umtx structure. It
1282 * can fault on any access.
1285 owner = fuword32(__DEVOLATILE(void *, &m->m_owner));
1286 if (mode == _UMUTEX_WAIT) {
1287 if (owner == UMUTEX_UNOWNED || owner == UMUTEX_CONTESTED)
1291 * Try the uncontested case. This should be done in userland.
1293 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
1295 /* The acquire succeeded. */
1296 if (owner == UMUTEX_UNOWNED)
1299 /* The address was invalid. */
1303 /* If no one owns it but it is contested try to acquire it. */
1304 if (owner == UMUTEX_CONTESTED) {
1305 owner = casuword32(&m->m_owner,
1306 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1308 if (owner == UMUTEX_CONTESTED)
1311 /* The address was invalid. */
1315 error = umtxq_check_susp(td);
1319 /* If this failed the lock has changed, restart. */
1324 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1325 (owner & ~UMUTEX_CONTESTED) == id)
1328 if (mode == _UMUTEX_TRY)
1332 * If we caught a signal, we have retried and now
1338 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1339 GET_SHARE(flags), &uq->uq_key)) != 0)
1342 umtxq_lock(&uq->uq_key);
1343 umtxq_busy(&uq->uq_key);
1345 umtxq_unlock(&uq->uq_key);
1348 * Set the contested bit so that a release in user space
1349 * knows to use the system call for unlock. If this fails
1350 * either some one else has acquired the lock or it has been
1353 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
1355 /* The address was invalid. */
1357 umtxq_lock(&uq->uq_key);
1359 umtxq_unbusy(&uq->uq_key);
1360 umtxq_unlock(&uq->uq_key);
1361 umtx_key_release(&uq->uq_key);
1366 * We set the contested bit, sleep. Otherwise the lock changed
1367 * and we need to retry or we lost a race to the thread
1368 * unlocking the umtx.
1370 umtxq_lock(&uq->uq_key);
1371 umtxq_unbusy(&uq->uq_key);
1373 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1376 umtxq_unlock(&uq->uq_key);
1377 umtx_key_release(&uq->uq_key);
1380 error = umtxq_check_susp(td);
1387 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1390 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags)
1392 struct umtx_key key;
1393 uint32_t owner, old, id;
1399 * Make sure we own this mtx.
1401 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1405 if ((owner & ~UMUTEX_CONTESTED) != id)
1408 if ((owner & UMUTEX_CONTESTED) == 0) {
1409 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
1417 /* We should only ever be in here for contested locks */
1418 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1424 count = umtxq_count(&key);
1428 * When unlocking the umtx, it must be marked as unowned if
1429 * there is zero or one thread only waiting for it.
1430 * Otherwise, it must be marked as contested.
1432 old = casuword32(&m->m_owner, owner,
1433 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1435 umtxq_signal(&key,1);
1438 umtx_key_release(&key);
1447 * Check if the mutex is available and wake up a waiter,
1448 * only for simple mutex.
1451 do_wake_umutex(struct thread *td, struct umutex *m)
1453 struct umtx_key key;
1459 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1463 if ((owner & ~UMUTEX_CONTESTED) != 0)
1466 flags = fuword32(&m->m_flags);
1468 /* We should only ever be in here for contested locks */
1469 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1475 count = umtxq_count(&key);
1479 owner = casuword32(&m->m_owner, UMUTEX_CONTESTED, UMUTEX_UNOWNED);
1482 if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1483 umtxq_signal(&key, 1);
1486 umtx_key_release(&key);
1491 * Check if the mutex has waiters and tries to fix contention bit.
1494 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1496 struct umtx_key key;
1497 uint32_t owner, old;
1502 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
1504 type = TYPE_NORMAL_UMUTEX;
1506 case UMUTEX_PRIO_INHERIT:
1507 type = TYPE_PI_UMUTEX;
1509 case UMUTEX_PRIO_PROTECT:
1510 type = TYPE_PP_UMUTEX;
1515 if ((error = umtx_key_get(m, type, GET_SHARE(flags),
1522 count = umtxq_count(&key);
1525 * Only repair contention bit if there is a waiter, this means the mutex
1526 * is still being referenced by userland code, otherwise don't update
1530 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1531 while ((owner & UMUTEX_CONTESTED) ==0) {
1532 old = casuword32(&m->m_owner, owner,
1533 owner|UMUTEX_CONTESTED);
1539 error = umtxq_check_susp(td);
1543 } else if (count == 1) {
1544 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1545 while ((owner & ~UMUTEX_CONTESTED) != 0 &&
1546 (owner & UMUTEX_CONTESTED) == 0) {
1547 old = casuword32(&m->m_owner, owner,
1548 owner|UMUTEX_CONTESTED);
1554 error = umtxq_check_susp(td);
1562 umtxq_signal(&key, INT_MAX);
1564 else if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1565 umtxq_signal(&key, 1);
1568 umtx_key_release(&key);
1572 static inline struct umtx_pi *
1573 umtx_pi_alloc(int flags)
1577 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1578 TAILQ_INIT(&pi->pi_blocked);
1579 atomic_add_int(&umtx_pi_allocated, 1);
1584 umtx_pi_free(struct umtx_pi *pi)
1586 uma_zfree(umtx_pi_zone, pi);
1587 atomic_add_int(&umtx_pi_allocated, -1);
1591 * Adjust the thread's position on a pi_state after its priority has been
1595 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1597 struct umtx_q *uq, *uq1, *uq2;
1600 mtx_assert(&umtx_lock, MA_OWNED);
1607 * Check if the thread needs to be moved on the blocked chain.
1608 * It needs to be moved if either its priority is lower than
1609 * the previous thread or higher than the next thread.
1611 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1612 uq2 = TAILQ_NEXT(uq, uq_lockq);
1613 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1614 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1616 * Remove thread from blocked chain and determine where
1617 * it should be moved to.
1619 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1620 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1621 td1 = uq1->uq_thread;
1622 MPASS(td1->td_proc->p_magic == P_MAGIC);
1623 if (UPRI(td1) > UPRI(td))
1628 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1630 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1636 * Propagate priority when a thread is blocked on POSIX
1640 umtx_propagate_priority(struct thread *td)
1646 mtx_assert(&umtx_lock, MA_OWNED);
1649 pi = uq->uq_pi_blocked;
1655 if (td == NULL || td == curthread)
1658 MPASS(td->td_proc != NULL);
1659 MPASS(td->td_proc->p_magic == P_MAGIC);
1662 if (td->td_lend_user_pri > pri)
1663 sched_lend_user_prio(td, pri);
1671 * Pick up the lock that td is blocked on.
1674 pi = uq->uq_pi_blocked;
1677 /* Resort td on the list if needed. */
1678 umtx_pi_adjust_thread(pi, td);
1683 * Unpropagate priority for a PI mutex when a thread blocked on
1684 * it is interrupted by signal or resumed by others.
1687 umtx_repropagate_priority(struct umtx_pi *pi)
1689 struct umtx_q *uq, *uq_owner;
1690 struct umtx_pi *pi2;
1693 mtx_assert(&umtx_lock, MA_OWNED);
1695 while (pi != NULL && pi->pi_owner != NULL) {
1697 uq_owner = pi->pi_owner->td_umtxq;
1699 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1700 uq = TAILQ_FIRST(&pi2->pi_blocked);
1702 if (pri > UPRI(uq->uq_thread))
1703 pri = UPRI(uq->uq_thread);
1707 if (pri > uq_owner->uq_inherited_pri)
1708 pri = uq_owner->uq_inherited_pri;
1709 thread_lock(pi->pi_owner);
1710 sched_lend_user_prio(pi->pi_owner, pri);
1711 thread_unlock(pi->pi_owner);
1712 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1713 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1718 * Insert a PI mutex into owned list.
1721 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1723 struct umtx_q *uq_owner;
1725 uq_owner = owner->td_umtxq;
1726 mtx_assert(&umtx_lock, MA_OWNED);
1727 if (pi->pi_owner != NULL)
1728 panic("pi_ower != NULL");
1729 pi->pi_owner = owner;
1730 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1734 * Claim ownership of a PI mutex.
1737 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1739 struct umtx_q *uq, *uq_owner;
1741 uq_owner = owner->td_umtxq;
1742 mtx_lock_spin(&umtx_lock);
1743 if (pi->pi_owner == owner) {
1744 mtx_unlock_spin(&umtx_lock);
1748 if (pi->pi_owner != NULL) {
1750 * userland may have already messed the mutex, sigh.
1752 mtx_unlock_spin(&umtx_lock);
1755 umtx_pi_setowner(pi, owner);
1756 uq = TAILQ_FIRST(&pi->pi_blocked);
1760 pri = UPRI(uq->uq_thread);
1762 if (pri < UPRI(owner))
1763 sched_lend_user_prio(owner, pri);
1764 thread_unlock(owner);
1766 mtx_unlock_spin(&umtx_lock);
1771 * Adjust a thread's order position in its blocked PI mutex,
1772 * this may result new priority propagating process.
1775 umtx_pi_adjust(struct thread *td, u_char oldpri)
1781 mtx_lock_spin(&umtx_lock);
1783 * Pick up the lock that td is blocked on.
1785 pi = uq->uq_pi_blocked;
1787 umtx_pi_adjust_thread(pi, td);
1788 umtx_repropagate_priority(pi);
1790 mtx_unlock_spin(&umtx_lock);
1794 * Sleep on a PI mutex.
1797 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi,
1798 uint32_t owner, const char *wmesg, struct abs_timeout *timo)
1800 struct umtxq_chain *uc;
1801 struct thread *td, *td1;
1807 KASSERT(td == curthread, ("inconsistent uq_thread"));
1808 uc = umtxq_getchain(&uq->uq_key);
1809 UMTXQ_LOCKED_ASSERT(uc);
1810 UMTXQ_BUSY_ASSERT(uc);
1812 mtx_lock_spin(&umtx_lock);
1813 if (pi->pi_owner == NULL) {
1814 mtx_unlock_spin(&umtx_lock);
1815 /* XXX Only look up thread in current process. */
1816 td1 = tdfind(owner, curproc->p_pid);
1817 mtx_lock_spin(&umtx_lock);
1819 if (pi->pi_owner == NULL)
1820 umtx_pi_setowner(pi, td1);
1821 PROC_UNLOCK(td1->td_proc);
1825 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1826 pri = UPRI(uq1->uq_thread);
1832 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1834 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1836 uq->uq_pi_blocked = pi;
1838 td->td_flags |= TDF_UPIBLOCKED;
1840 umtx_propagate_priority(td);
1841 mtx_unlock_spin(&umtx_lock);
1842 umtxq_unbusy(&uq->uq_key);
1844 error = umtxq_sleep(uq, wmesg, timo);
1847 mtx_lock_spin(&umtx_lock);
1848 uq->uq_pi_blocked = NULL;
1850 td->td_flags &= ~TDF_UPIBLOCKED;
1852 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1853 umtx_repropagate_priority(pi);
1854 mtx_unlock_spin(&umtx_lock);
1855 umtxq_unlock(&uq->uq_key);
1861 * Add reference count for a PI mutex.
1864 umtx_pi_ref(struct umtx_pi *pi)
1866 struct umtxq_chain *uc;
1868 uc = umtxq_getchain(&pi->pi_key);
1869 UMTXQ_LOCKED_ASSERT(uc);
1874 * Decrease reference count for a PI mutex, if the counter
1875 * is decreased to zero, its memory space is freed.
1878 umtx_pi_unref(struct umtx_pi *pi)
1880 struct umtxq_chain *uc;
1882 uc = umtxq_getchain(&pi->pi_key);
1883 UMTXQ_LOCKED_ASSERT(uc);
1884 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1885 if (--pi->pi_refcount == 0) {
1886 mtx_lock_spin(&umtx_lock);
1887 if (pi->pi_owner != NULL) {
1888 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested,
1890 pi->pi_owner = NULL;
1892 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1893 ("blocked queue not empty"));
1894 mtx_unlock_spin(&umtx_lock);
1895 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1901 * Find a PI mutex in hash table.
1903 static struct umtx_pi *
1904 umtx_pi_lookup(struct umtx_key *key)
1906 struct umtxq_chain *uc;
1909 uc = umtxq_getchain(key);
1910 UMTXQ_LOCKED_ASSERT(uc);
1912 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1913 if (umtx_key_match(&pi->pi_key, key)) {
1921 * Insert a PI mutex into hash table.
1924 umtx_pi_insert(struct umtx_pi *pi)
1926 struct umtxq_chain *uc;
1928 uc = umtxq_getchain(&pi->pi_key);
1929 UMTXQ_LOCKED_ASSERT(uc);
1930 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1937 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1938 struct _umtx_time *timeout, int try)
1940 struct abs_timeout timo;
1942 struct umtx_pi *pi, *new_pi;
1943 uint32_t id, owner, old;
1949 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1953 if (timeout != NULL)
1954 abs_timeout_init2(&timo, timeout);
1956 umtxq_lock(&uq->uq_key);
1957 pi = umtx_pi_lookup(&uq->uq_key);
1959 new_pi = umtx_pi_alloc(M_NOWAIT);
1960 if (new_pi == NULL) {
1961 umtxq_unlock(&uq->uq_key);
1962 new_pi = umtx_pi_alloc(M_WAITOK);
1963 umtxq_lock(&uq->uq_key);
1964 pi = umtx_pi_lookup(&uq->uq_key);
1966 umtx_pi_free(new_pi);
1970 if (new_pi != NULL) {
1971 new_pi->pi_key = uq->uq_key;
1972 umtx_pi_insert(new_pi);
1977 umtxq_unlock(&uq->uq_key);
1980 * Care must be exercised when dealing with umtx structure. It
1981 * can fault on any access.
1985 * Try the uncontested case. This should be done in userland.
1987 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
1989 /* The acquire succeeded. */
1990 if (owner == UMUTEX_UNOWNED) {
1995 /* The address was invalid. */
2001 /* If no one owns it but it is contested try to acquire it. */
2002 if (owner == UMUTEX_CONTESTED) {
2003 owner = casuword32(&m->m_owner,
2004 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
2006 if (owner == UMUTEX_CONTESTED) {
2007 umtxq_lock(&uq->uq_key);
2008 umtxq_busy(&uq->uq_key);
2009 error = umtx_pi_claim(pi, td);
2010 umtxq_unbusy(&uq->uq_key);
2011 umtxq_unlock(&uq->uq_key);
2015 /* The address was invalid. */
2021 error = umtxq_check_susp(td);
2025 /* If this failed the lock has changed, restart. */
2029 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
2030 (owner & ~UMUTEX_CONTESTED) == id) {
2041 * If we caught a signal, we have retried and now
2047 umtxq_lock(&uq->uq_key);
2048 umtxq_busy(&uq->uq_key);
2049 umtxq_unlock(&uq->uq_key);
2052 * Set the contested bit so that a release in user space
2053 * knows to use the system call for unlock. If this fails
2054 * either some one else has acquired the lock or it has been
2057 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
2059 /* The address was invalid. */
2061 umtxq_lock(&uq->uq_key);
2062 umtxq_unbusy(&uq->uq_key);
2063 umtxq_unlock(&uq->uq_key);
2068 umtxq_lock(&uq->uq_key);
2070 * We set the contested bit, sleep. Otherwise the lock changed
2071 * and we need to retry or we lost a race to the thread
2072 * unlocking the umtx.
2075 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2076 "umtxpi", timeout == NULL ? NULL : &timo);
2078 umtxq_unbusy(&uq->uq_key);
2079 umtxq_unlock(&uq->uq_key);
2082 error = umtxq_check_susp(td);
2087 umtxq_lock(&uq->uq_key);
2089 umtxq_unlock(&uq->uq_key);
2091 umtx_key_release(&uq->uq_key);
2096 * Unlock a PI mutex.
2099 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags)
2101 struct umtx_key key;
2102 struct umtx_q *uq_first, *uq_first2, *uq_me;
2103 struct umtx_pi *pi, *pi2;
2104 uint32_t owner, old, id;
2111 * Make sure we own this mtx.
2113 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
2117 if ((owner & ~UMUTEX_CONTESTED) != id)
2120 /* This should be done in userland */
2121 if ((owner & UMUTEX_CONTESTED) == 0) {
2122 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
2130 /* We should only ever be in here for contested locks */
2131 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
2137 count = umtxq_count_pi(&key, &uq_first);
2138 if (uq_first != NULL) {
2139 mtx_lock_spin(&umtx_lock);
2140 pi = uq_first->uq_pi_blocked;
2141 KASSERT(pi != NULL, ("pi == NULL?"));
2142 if (pi->pi_owner != curthread) {
2143 mtx_unlock_spin(&umtx_lock);
2146 umtx_key_release(&key);
2147 /* userland messed the mutex */
2150 uq_me = curthread->td_umtxq;
2151 pi->pi_owner = NULL;
2152 TAILQ_REMOVE(&uq_me->uq_pi_contested, pi, pi_link);
2153 /* get highest priority thread which is still sleeping. */
2154 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2155 while (uq_first != NULL &&
2156 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2157 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2160 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2161 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2162 if (uq_first2 != NULL) {
2163 if (pri > UPRI(uq_first2->uq_thread))
2164 pri = UPRI(uq_first2->uq_thread);
2167 thread_lock(curthread);
2168 sched_lend_user_prio(curthread, pri);
2169 thread_unlock(curthread);
2170 mtx_unlock_spin(&umtx_lock);
2172 umtxq_signal_thread(uq_first);
2177 * When unlocking the umtx, it must be marked as unowned if
2178 * there is zero or one thread only waiting for it.
2179 * Otherwise, it must be marked as contested.
2181 old = casuword32(&m->m_owner, owner,
2182 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
2187 umtx_key_release(&key);
2199 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2200 struct _umtx_time *timeout, int try)
2202 struct abs_timeout timo;
2203 struct umtx_q *uq, *uq2;
2207 int error, pri, old_inherited_pri, su;
2211 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2215 if (timeout != NULL)
2216 abs_timeout_init2(&timo, timeout);
2218 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2220 old_inherited_pri = uq->uq_inherited_pri;
2221 umtxq_lock(&uq->uq_key);
2222 umtxq_busy(&uq->uq_key);
2223 umtxq_unlock(&uq->uq_key);
2225 ceiling = RTP_PRIO_MAX - fuword32(&m->m_ceilings[0]);
2226 if (ceiling > RTP_PRIO_MAX) {
2231 mtx_lock_spin(&umtx_lock);
2232 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2233 mtx_unlock_spin(&umtx_lock);
2237 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2238 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2240 if (uq->uq_inherited_pri < UPRI(td))
2241 sched_lend_user_prio(td, uq->uq_inherited_pri);
2244 mtx_unlock_spin(&umtx_lock);
2246 owner = casuword32(&m->m_owner,
2247 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
2249 if (owner == UMUTEX_CONTESTED) {
2254 /* The address was invalid. */
2260 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
2261 (owner & ~UMUTEX_CONTESTED) == id) {
2272 * If we caught a signal, we have retried and now
2278 umtxq_lock(&uq->uq_key);
2280 umtxq_unbusy(&uq->uq_key);
2281 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2284 umtxq_unlock(&uq->uq_key);
2286 mtx_lock_spin(&umtx_lock);
2287 uq->uq_inherited_pri = old_inherited_pri;
2289 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2290 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2292 if (pri > UPRI(uq2->uq_thread))
2293 pri = UPRI(uq2->uq_thread);
2296 if (pri > uq->uq_inherited_pri)
2297 pri = uq->uq_inherited_pri;
2299 sched_lend_user_prio(td, pri);
2301 mtx_unlock_spin(&umtx_lock);
2305 mtx_lock_spin(&umtx_lock);
2306 uq->uq_inherited_pri = old_inherited_pri;
2308 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2309 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2311 if (pri > UPRI(uq2->uq_thread))
2312 pri = UPRI(uq2->uq_thread);
2315 if (pri > uq->uq_inherited_pri)
2316 pri = uq->uq_inherited_pri;
2318 sched_lend_user_prio(td, pri);
2320 mtx_unlock_spin(&umtx_lock);
2324 umtxq_lock(&uq->uq_key);
2325 umtxq_unbusy(&uq->uq_key);
2326 umtxq_unlock(&uq->uq_key);
2327 umtx_key_release(&uq->uq_key);
2332 * Unlock a PP mutex.
2335 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags)
2337 struct umtx_key key;
2338 struct umtx_q *uq, *uq2;
2342 int error, pri, new_inherited_pri, su;
2346 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2349 * Make sure we own this mtx.
2351 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
2355 if ((owner & ~UMUTEX_CONTESTED) != id)
2358 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2363 new_inherited_pri = PRI_MAX;
2365 rceiling = RTP_PRIO_MAX - rceiling;
2366 if (rceiling > RTP_PRIO_MAX)
2368 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2371 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2378 * For priority protected mutex, always set unlocked state
2379 * to UMUTEX_CONTESTED, so that userland always enters kernel
2380 * to lock the mutex, it is necessary because thread priority
2381 * has to be adjusted for such mutex.
2383 error = suword32(__DEVOLATILE(uint32_t *, &m->m_owner),
2388 umtxq_signal(&key, 1);
2395 mtx_lock_spin(&umtx_lock);
2397 uq->uq_inherited_pri = new_inherited_pri;
2399 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2400 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2402 if (pri > UPRI(uq2->uq_thread))
2403 pri = UPRI(uq2->uq_thread);
2406 if (pri > uq->uq_inherited_pri)
2407 pri = uq->uq_inherited_pri;
2409 sched_lend_user_prio(td, pri);
2411 mtx_unlock_spin(&umtx_lock);
2413 umtx_key_release(&key);
2418 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2419 uint32_t *old_ceiling)
2422 uint32_t save_ceiling;
2427 flags = fuword32(&m->m_flags);
2428 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2430 if (ceiling > RTP_PRIO_MAX)
2434 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2438 umtxq_lock(&uq->uq_key);
2439 umtxq_busy(&uq->uq_key);
2440 umtxq_unlock(&uq->uq_key);
2442 save_ceiling = fuword32(&m->m_ceilings[0]);
2444 owner = casuword32(&m->m_owner,
2445 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
2447 if (owner == UMUTEX_CONTESTED) {
2448 suword32(&m->m_ceilings[0], ceiling);
2449 suword32(__DEVOLATILE(uint32_t *, &m->m_owner),
2455 /* The address was invalid. */
2461 if ((owner & ~UMUTEX_CONTESTED) == id) {
2462 suword32(&m->m_ceilings[0], ceiling);
2468 * If we caught a signal, we have retried and now
2475 * We set the contested bit, sleep. Otherwise the lock changed
2476 * and we need to retry or we lost a race to the thread
2477 * unlocking the umtx.
2479 umtxq_lock(&uq->uq_key);
2481 umtxq_unbusy(&uq->uq_key);
2482 error = umtxq_sleep(uq, "umtxpp", NULL);
2484 umtxq_unlock(&uq->uq_key);
2486 umtxq_lock(&uq->uq_key);
2488 umtxq_signal(&uq->uq_key, INT_MAX);
2489 umtxq_unbusy(&uq->uq_key);
2490 umtxq_unlock(&uq->uq_key);
2491 umtx_key_release(&uq->uq_key);
2492 if (error == 0 && old_ceiling != NULL)
2493 suword32(old_ceiling, save_ceiling);
2498 * Lock a userland POSIX mutex.
2501 do_lock_umutex(struct thread *td, struct umutex *m,
2502 struct _umtx_time *timeout, int mode)
2507 flags = fuword32(&m->m_flags);
2511 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2513 error = do_lock_normal(td, m, flags, timeout, mode);
2515 case UMUTEX_PRIO_INHERIT:
2516 error = do_lock_pi(td, m, flags, timeout, mode);
2518 case UMUTEX_PRIO_PROTECT:
2519 error = do_lock_pp(td, m, flags, timeout, mode);
2524 if (timeout == NULL) {
2525 if (error == EINTR && mode != _UMUTEX_WAIT)
2528 /* Timed-locking is not restarted. */
2529 if (error == ERESTART)
2536 * Unlock a userland POSIX mutex.
2539 do_unlock_umutex(struct thread *td, struct umutex *m)
2543 flags = fuword32(&m->m_flags);
2547 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2549 return (do_unlock_normal(td, m, flags));
2550 case UMUTEX_PRIO_INHERIT:
2551 return (do_unlock_pi(td, m, flags));
2552 case UMUTEX_PRIO_PROTECT:
2553 return (do_unlock_pp(td, m, flags));
2560 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2561 struct timespec *timeout, u_long wflags)
2563 struct abs_timeout timo;
2570 flags = fuword32(&cv->c_flags);
2571 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2575 if ((wflags & CVWAIT_CLOCKID) != 0) {
2576 clockid = fuword32(&cv->c_clockid);
2577 if (clockid < CLOCK_REALTIME ||
2578 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2579 /* hmm, only HW clock id will work. */
2583 clockid = CLOCK_REALTIME;
2586 umtxq_lock(&uq->uq_key);
2587 umtxq_busy(&uq->uq_key);
2589 umtxq_unlock(&uq->uq_key);
2592 * Set c_has_waiters to 1 before releasing user mutex, also
2593 * don't modify cache line when unnecessary.
2595 if (fuword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters)) == 0)
2596 suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 1);
2598 umtxq_lock(&uq->uq_key);
2599 umtxq_unbusy(&uq->uq_key);
2600 umtxq_unlock(&uq->uq_key);
2602 error = do_unlock_umutex(td, m);
2604 if (timeout != NULL)
2605 abs_timeout_init(&timo, clockid, ((wflags & CVWAIT_ABSTIME) != 0),
2608 umtxq_lock(&uq->uq_key);
2610 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2614 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2618 * This must be timeout,interrupted by signal or
2619 * surprious wakeup, clear c_has_waiter flag when
2622 umtxq_busy(&uq->uq_key);
2623 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2624 int oldlen = uq->uq_cur_queue->length;
2627 umtxq_unlock(&uq->uq_key);
2629 __DEVOLATILE(uint32_t *,
2630 &cv->c_has_waiters), 0);
2631 umtxq_lock(&uq->uq_key);
2634 umtxq_unbusy(&uq->uq_key);
2635 if (error == ERESTART)
2639 umtxq_unlock(&uq->uq_key);
2640 umtx_key_release(&uq->uq_key);
2645 * Signal a userland condition variable.
2648 do_cv_signal(struct thread *td, struct ucond *cv)
2650 struct umtx_key key;
2651 int error, cnt, nwake;
2654 flags = fuword32(&cv->c_flags);
2655 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2659 cnt = umtxq_count(&key);
2660 nwake = umtxq_signal(&key, 1);
2664 __DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0);
2669 umtx_key_release(&key);
2674 do_cv_broadcast(struct thread *td, struct ucond *cv)
2676 struct umtx_key key;
2680 flags = fuword32(&cv->c_flags);
2681 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2686 umtxq_signal(&key, INT_MAX);
2689 error = suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0);
2695 umtx_key_release(&key);
2700 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, struct _umtx_time *timeout)
2702 struct abs_timeout timo;
2704 uint32_t flags, wrflags;
2705 int32_t state, oldstate;
2706 int32_t blocked_readers;
2710 flags = fuword32(&rwlock->rw_flags);
2711 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2715 if (timeout != NULL)
2716 abs_timeout_init2(&timo, timeout);
2718 wrflags = URWLOCK_WRITE_OWNER;
2719 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2720 wrflags |= URWLOCK_WRITE_WAITERS;
2723 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2724 /* try to lock it */
2725 while (!(state & wrflags)) {
2726 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2727 umtx_key_release(&uq->uq_key);
2730 oldstate = casuword32(&rwlock->rw_state, state, state + 1);
2731 if (oldstate == -1) {
2732 umtx_key_release(&uq->uq_key);
2735 if (oldstate == state) {
2736 umtx_key_release(&uq->uq_key);
2739 error = umtxq_check_susp(td);
2748 /* grab monitor lock */
2749 umtxq_lock(&uq->uq_key);
2750 umtxq_busy(&uq->uq_key);
2751 umtxq_unlock(&uq->uq_key);
2754 * re-read the state, in case it changed between the try-lock above
2755 * and the check below
2757 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2759 /* set read contention bit */
2760 while ((state & wrflags) && !(state & URWLOCK_READ_WAITERS)) {
2761 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_READ_WAITERS);
2762 if (oldstate == -1) {
2766 if (oldstate == state)
2769 error = umtxq_check_susp(td);
2774 umtxq_lock(&uq->uq_key);
2775 umtxq_unbusy(&uq->uq_key);
2776 umtxq_unlock(&uq->uq_key);
2780 /* state is changed while setting flags, restart */
2781 if (!(state & wrflags)) {
2782 umtxq_lock(&uq->uq_key);
2783 umtxq_unbusy(&uq->uq_key);
2784 umtxq_unlock(&uq->uq_key);
2785 error = umtxq_check_susp(td);
2792 /* contention bit is set, before sleeping, increase read waiter count */
2793 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2794 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2796 while (state & wrflags) {
2797 umtxq_lock(&uq->uq_key);
2799 umtxq_unbusy(&uq->uq_key);
2801 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2804 umtxq_busy(&uq->uq_key);
2806 umtxq_unlock(&uq->uq_key);
2809 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2812 /* decrease read waiter count, and may clear read contention bit */
2813 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2814 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2815 if (blocked_readers == 1) {
2816 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2818 oldstate = casuword32(&rwlock->rw_state, state,
2819 state & ~URWLOCK_READ_WAITERS);
2820 if (oldstate == -1) {
2824 if (oldstate == state)
2827 error = umtxq_check_susp(td);
2833 umtxq_lock(&uq->uq_key);
2834 umtxq_unbusy(&uq->uq_key);
2835 umtxq_unlock(&uq->uq_key);
2839 umtx_key_release(&uq->uq_key);
2840 if (error == ERESTART)
2846 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2848 struct abs_timeout timo;
2851 int32_t state, oldstate;
2852 int32_t blocked_writers;
2853 int32_t blocked_readers;
2857 flags = fuword32(&rwlock->rw_flags);
2858 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2862 if (timeout != NULL)
2863 abs_timeout_init2(&timo, timeout);
2865 blocked_readers = 0;
2867 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2868 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2869 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_WRITE_OWNER);
2870 if (oldstate == -1) {
2871 umtx_key_release(&uq->uq_key);
2874 if (oldstate == state) {
2875 umtx_key_release(&uq->uq_key);
2879 error = umtxq_check_susp(td);
2885 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
2886 blocked_readers != 0) {
2887 umtxq_lock(&uq->uq_key);
2888 umtxq_busy(&uq->uq_key);
2889 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
2890 umtxq_unbusy(&uq->uq_key);
2891 umtxq_unlock(&uq->uq_key);
2897 /* grab monitor lock */
2898 umtxq_lock(&uq->uq_key);
2899 umtxq_busy(&uq->uq_key);
2900 umtxq_unlock(&uq->uq_key);
2903 * re-read the state, in case it changed between the try-lock above
2904 * and the check below
2906 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2908 while (((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) &&
2909 (state & URWLOCK_WRITE_WAITERS) == 0) {
2910 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_WRITE_WAITERS);
2911 if (oldstate == -1) {
2915 if (oldstate == state)
2918 error = umtxq_check_susp(td);
2923 umtxq_lock(&uq->uq_key);
2924 umtxq_unbusy(&uq->uq_key);
2925 umtxq_unlock(&uq->uq_key);
2929 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2930 umtxq_lock(&uq->uq_key);
2931 umtxq_unbusy(&uq->uq_key);
2932 umtxq_unlock(&uq->uq_key);
2933 error = umtxq_check_susp(td);
2939 blocked_writers = fuword32(&rwlock->rw_blocked_writers);
2940 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
2942 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
2943 umtxq_lock(&uq->uq_key);
2944 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2945 umtxq_unbusy(&uq->uq_key);
2947 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2950 umtxq_busy(&uq->uq_key);
2951 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2952 umtxq_unlock(&uq->uq_key);
2955 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2958 blocked_writers = fuword32(&rwlock->rw_blocked_writers);
2959 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
2960 if (blocked_writers == 1) {
2961 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2963 oldstate = casuword32(&rwlock->rw_state, state,
2964 state & ~URWLOCK_WRITE_WAITERS);
2965 if (oldstate == -1) {
2969 if (oldstate == state)
2972 error = umtxq_check_susp(td);
2974 * We are leaving the URWLOCK_WRITE_WAITERS
2975 * behind, but this should not harm the
2981 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2983 blocked_readers = 0;
2985 umtxq_lock(&uq->uq_key);
2986 umtxq_unbusy(&uq->uq_key);
2987 umtxq_unlock(&uq->uq_key);
2990 umtx_key_release(&uq->uq_key);
2991 if (error == ERESTART)
2997 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3001 int32_t state, oldstate;
3002 int error, q, count;
3005 flags = fuword32(&rwlock->rw_flags);
3006 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3010 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
3011 if (state & URWLOCK_WRITE_OWNER) {
3013 oldstate = casuword32(&rwlock->rw_state, state,
3014 state & ~URWLOCK_WRITE_OWNER);
3015 if (oldstate == -1) {
3019 if (oldstate != state) {
3021 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3025 error = umtxq_check_susp(td);
3031 } else if (URWLOCK_READER_COUNT(state) != 0) {
3033 oldstate = casuword32(&rwlock->rw_state, state,
3035 if (oldstate == -1) {
3039 if (oldstate != state) {
3041 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3045 error = umtxq_check_susp(td);
3058 if (!(flags & URWLOCK_PREFER_READER)) {
3059 if (state & URWLOCK_WRITE_WAITERS) {
3061 q = UMTX_EXCLUSIVE_QUEUE;
3062 } else if (state & URWLOCK_READ_WAITERS) {
3064 q = UMTX_SHARED_QUEUE;
3067 if (state & URWLOCK_READ_WAITERS) {
3069 q = UMTX_SHARED_QUEUE;
3070 } else if (state & URWLOCK_WRITE_WAITERS) {
3072 q = UMTX_EXCLUSIVE_QUEUE;
3077 umtxq_lock(&uq->uq_key);
3078 umtxq_busy(&uq->uq_key);
3079 umtxq_signal_queue(&uq->uq_key, count, q);
3080 umtxq_unbusy(&uq->uq_key);
3081 umtxq_unlock(&uq->uq_key);
3084 umtx_key_release(&uq->uq_key);
3089 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3091 struct abs_timeout timo;
3093 uint32_t flags, count;
3097 flags = fuword32(&sem->_flags);
3098 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3102 if (timeout != NULL)
3103 abs_timeout_init2(&timo, timeout);
3105 umtxq_lock(&uq->uq_key);
3106 umtxq_busy(&uq->uq_key);
3108 umtxq_unlock(&uq->uq_key);
3109 casuword32(__DEVOLATILE(uint32_t *, &sem->_has_waiters), 0, 1);
3110 count = fuword32(__DEVOLATILE(uint32_t *, &sem->_count));
3112 umtxq_lock(&uq->uq_key);
3113 umtxq_unbusy(&uq->uq_key);
3115 umtxq_unlock(&uq->uq_key);
3116 umtx_key_release(&uq->uq_key);
3119 umtxq_lock(&uq->uq_key);
3120 umtxq_unbusy(&uq->uq_key);
3122 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3124 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3128 /* A relative timeout cannot be restarted. */
3129 if (error == ERESTART && timeout != NULL &&
3130 (timeout->_flags & UMTX_ABSTIME) == 0)
3133 umtxq_unlock(&uq->uq_key);
3134 umtx_key_release(&uq->uq_key);
3139 * Signal a userland condition variable.
3142 do_sem_wake(struct thread *td, struct _usem *sem)
3144 struct umtx_key key;
3148 flags = fuword32(&sem->_flags);
3149 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3153 cnt = umtxq_count(&key);
3155 umtxq_signal(&key, 1);
3157 * Check if count is greater than 0, this means the memory is
3158 * still being referenced by user code, so we can safely
3159 * update _has_waiters flag.
3164 __DEVOLATILE(uint32_t *, &sem->_has_waiters), 0);
3170 umtx_key_release(&key);
3175 sys__umtx_lock(struct thread *td, struct _umtx_lock_args *uap)
3176 /* struct umtx *umtx */
3178 return do_lock_umtx(td, uap->umtx, td->td_tid, 0);
3182 sys__umtx_unlock(struct thread *td, struct _umtx_unlock_args *uap)
3183 /* struct umtx *umtx */
3185 return do_unlock_umtx(td, uap->umtx, td->td_tid);
3189 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3193 error = copyin(addr, tsp, sizeof(struct timespec));
3195 if (tsp->tv_sec < 0 ||
3196 tsp->tv_nsec >= 1000000000 ||
3204 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3208 if (size <= sizeof(struct timespec)) {
3209 tp->_clockid = CLOCK_REALTIME;
3211 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3213 error = copyin(addr, tp, sizeof(struct _umtx_time));
3216 if (tp->_timeout.tv_sec < 0 ||
3217 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3223 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap)
3225 struct timespec *ts, timeout;
3228 /* Allow a null timespec (wait forever). */
3229 if (uap->uaddr2 == NULL)
3232 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3237 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3241 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap)
3243 return (do_unlock_umtx(td, uap->obj, uap->val));
3247 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3249 struct _umtx_time timeout, *tm_p;
3252 if (uap->uaddr2 == NULL)
3255 error = umtx_copyin_umtx_time(
3256 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3261 return do_wait(td, uap->obj, uap->val, tm_p, 0, 0);
3265 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3267 struct _umtx_time timeout, *tm_p;
3270 if (uap->uaddr2 == NULL)
3273 error = umtx_copyin_umtx_time(
3274 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3279 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3283 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3285 struct _umtx_time *tm_p, timeout;
3288 if (uap->uaddr2 == NULL)
3291 error = umtx_copyin_umtx_time(
3292 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3297 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3301 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3303 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3306 #define BATCH_SIZE 128
3308 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3310 int count = uap->val;
3311 void *uaddrs[BATCH_SIZE];
3312 char **upp = (char **)uap->obj;
3319 if (tocopy > BATCH_SIZE)
3320 tocopy = BATCH_SIZE;
3321 error = copyin(upp+pos, uaddrs, tocopy * sizeof(char *));
3324 for (i = 0; i < tocopy; ++i)
3325 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3333 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3335 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3339 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3341 struct _umtx_time *tm_p, timeout;
3344 /* Allow a null timespec (wait forever). */
3345 if (uap->uaddr2 == NULL)
3348 error = umtx_copyin_umtx_time(
3349 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3354 return do_lock_umutex(td, uap->obj, tm_p, 0);
3358 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3360 return do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY);
3364 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3366 struct _umtx_time *tm_p, timeout;
3369 /* Allow a null timespec (wait forever). */
3370 if (uap->uaddr2 == NULL)
3373 error = umtx_copyin_umtx_time(
3374 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3379 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3383 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3385 return do_wake_umutex(td, uap->obj);
3389 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3391 return do_unlock_umutex(td, uap->obj);
3395 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3397 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1);
3401 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3403 struct timespec *ts, timeout;
3406 /* Allow a null timespec (wait forever). */
3407 if (uap->uaddr2 == NULL)
3410 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3415 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3419 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3421 return do_cv_signal(td, uap->obj);
3425 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3427 return do_cv_broadcast(td, uap->obj);
3431 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3433 struct _umtx_time timeout;
3436 /* Allow a null timespec (wait forever). */
3437 if (uap->uaddr2 == NULL) {
3438 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3440 error = umtx_copyin_umtx_time(uap->uaddr2,
3441 (size_t)uap->uaddr1, &timeout);
3444 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3450 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3452 struct _umtx_time timeout;
3455 /* Allow a null timespec (wait forever). */
3456 if (uap->uaddr2 == NULL) {
3457 error = do_rw_wrlock(td, uap->obj, 0);
3459 error = umtx_copyin_umtx_time(uap->uaddr2,
3460 (size_t)uap->uaddr1, &timeout);
3464 error = do_rw_wrlock(td, uap->obj, &timeout);
3470 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3472 return do_rw_unlock(td, uap->obj);
3476 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3478 struct _umtx_time *tm_p, timeout;
3481 /* Allow a null timespec (wait forever). */
3482 if (uap->uaddr2 == NULL)
3485 error = umtx_copyin_umtx_time(
3486 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3491 return (do_sem_wait(td, uap->obj, tm_p));
3495 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3497 return do_sem_wake(td, uap->obj);
3501 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3503 return do_wake2_umutex(td, uap->obj, uap->val);
3506 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3508 static _umtx_op_func op_table[] = {
3509 __umtx_op_lock_umtx, /* UMTX_OP_LOCK */
3510 __umtx_op_unlock_umtx, /* UMTX_OP_UNLOCK */
3511 __umtx_op_wait, /* UMTX_OP_WAIT */
3512 __umtx_op_wake, /* UMTX_OP_WAKE */
3513 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_TRYLOCK */
3514 __umtx_op_lock_umutex, /* UMTX_OP_MUTEX_LOCK */
3515 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3516 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3517 __umtx_op_cv_wait, /* UMTX_OP_CV_WAIT*/
3518 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3519 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3520 __umtx_op_wait_uint, /* UMTX_OP_WAIT_UINT */
3521 __umtx_op_rw_rdlock, /* UMTX_OP_RW_RDLOCK */
3522 __umtx_op_rw_wrlock, /* UMTX_OP_RW_WRLOCK */
3523 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3524 __umtx_op_wait_uint_private, /* UMTX_OP_WAIT_UINT_PRIVATE */
3525 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3526 __umtx_op_wait_umutex, /* UMTX_OP_UMUTEX_WAIT */
3527 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
3528 __umtx_op_sem_wait, /* UMTX_OP_SEM_WAIT */
3529 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3530 __umtx_op_nwake_private, /* UMTX_OP_NWAKE_PRIVATE */
3531 __umtx_op_wake2_umutex /* UMTX_OP_UMUTEX_WAKE2 */
3535 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
3537 if ((unsigned)uap->op < UMTX_OP_MAX)
3538 return (*op_table[uap->op])(td, uap);
3542 #ifdef COMPAT_FREEBSD32
3544 freebsd32_umtx_lock(struct thread *td, struct freebsd32_umtx_lock_args *uap)
3545 /* struct umtx *umtx */
3547 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
3551 freebsd32_umtx_unlock(struct thread *td, struct freebsd32_umtx_unlock_args *uap)
3552 /* struct umtx *umtx */
3554 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
3562 struct umtx_time32 {
3563 struct timespec32 timeout;
3569 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
3571 struct timespec32 ts32;
3574 error = copyin(addr, &ts32, sizeof(struct timespec32));
3576 if (ts32.tv_sec < 0 ||
3577 ts32.tv_nsec >= 1000000000 ||
3581 tsp->tv_sec = ts32.tv_sec;
3582 tsp->tv_nsec = ts32.tv_nsec;
3589 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
3591 struct umtx_time32 t32;
3594 t32.clockid = CLOCK_REALTIME;
3596 if (size <= sizeof(struct timespec32))
3597 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
3599 error = copyin(addr, &t32, sizeof(struct umtx_time32));
3602 if (t32.timeout.tv_sec < 0 ||
3603 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
3605 tp->_timeout.tv_sec = t32.timeout.tv_sec;
3606 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
3607 tp->_flags = t32.flags;
3608 tp->_clockid = t32.clockid;
3613 __umtx_op_lock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
3615 struct timespec *ts, timeout;
3618 /* Allow a null timespec (wait forever). */
3619 if (uap->uaddr2 == NULL)
3622 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3627 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3631 __umtx_op_unlock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
3633 return (do_unlock_umtx32(td, uap->obj, (uint32_t)uap->val));
3637 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3639 struct _umtx_time *tm_p, timeout;
3642 if (uap->uaddr2 == NULL)
3645 error = umtx_copyin_umtx_time32(uap->uaddr2,
3646 (size_t)uap->uaddr1, &timeout);
3651 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3655 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3657 struct _umtx_time *tm_p, timeout;
3660 /* Allow a null timespec (wait forever). */
3661 if (uap->uaddr2 == NULL)
3664 error = umtx_copyin_umtx_time(uap->uaddr2,
3665 (size_t)uap->uaddr1, &timeout);
3670 return do_lock_umutex(td, uap->obj, tm_p, 0);
3674 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3676 struct _umtx_time *tm_p, timeout;
3679 /* Allow a null timespec (wait forever). */
3680 if (uap->uaddr2 == NULL)
3683 error = umtx_copyin_umtx_time32(uap->uaddr2,
3684 (size_t)uap->uaddr1, &timeout);
3689 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3693 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3695 struct timespec *ts, timeout;
3698 /* Allow a null timespec (wait forever). */
3699 if (uap->uaddr2 == NULL)
3702 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3707 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3711 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3713 struct _umtx_time timeout;
3716 /* Allow a null timespec (wait forever). */
3717 if (uap->uaddr2 == NULL) {
3718 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3720 error = umtx_copyin_umtx_time32(uap->uaddr2,
3721 (size_t)uap->uaddr1, &timeout);
3724 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3730 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3732 struct _umtx_time timeout;
3735 /* Allow a null timespec (wait forever). */
3736 if (uap->uaddr2 == NULL) {
3737 error = do_rw_wrlock(td, uap->obj, 0);
3739 error = umtx_copyin_umtx_time32(uap->uaddr2,
3740 (size_t)uap->uaddr1, &timeout);
3743 error = do_rw_wrlock(td, uap->obj, &timeout);
3749 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3751 struct _umtx_time *tm_p, timeout;
3754 if (uap->uaddr2 == NULL)
3757 error = umtx_copyin_umtx_time32(
3758 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
3763 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3767 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3769 struct _umtx_time *tm_p, timeout;
3772 /* Allow a null timespec (wait forever). */
3773 if (uap->uaddr2 == NULL)
3776 error = umtx_copyin_umtx_time32(uap->uaddr2,
3777 (size_t)uap->uaddr1, &timeout);
3782 return (do_sem_wait(td, uap->obj, tm_p));
3786 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
3788 int count = uap->val;
3789 uint32_t uaddrs[BATCH_SIZE];
3790 uint32_t **upp = (uint32_t **)uap->obj;
3797 if (tocopy > BATCH_SIZE)
3798 tocopy = BATCH_SIZE;
3799 error = copyin(upp+pos, uaddrs, tocopy * sizeof(uint32_t));
3802 for (i = 0; i < tocopy; ++i)
3803 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
3811 static _umtx_op_func op_table_compat32[] = {
3812 __umtx_op_lock_umtx_compat32, /* UMTX_OP_LOCK */
3813 __umtx_op_unlock_umtx_compat32, /* UMTX_OP_UNLOCK */
3814 __umtx_op_wait_compat32, /* UMTX_OP_WAIT */
3815 __umtx_op_wake, /* UMTX_OP_WAKE */
3816 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_LOCK */
3817 __umtx_op_lock_umutex_compat32, /* UMTX_OP_MUTEX_TRYLOCK */
3818 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3819 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3820 __umtx_op_cv_wait_compat32, /* UMTX_OP_CV_WAIT*/
3821 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3822 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3823 __umtx_op_wait_compat32, /* UMTX_OP_WAIT_UINT */
3824 __umtx_op_rw_rdlock_compat32, /* UMTX_OP_RW_RDLOCK */
3825 __umtx_op_rw_wrlock_compat32, /* UMTX_OP_RW_WRLOCK */
3826 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3827 __umtx_op_wait_uint_private_compat32, /* UMTX_OP_WAIT_UINT_PRIVATE */
3828 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3829 __umtx_op_wait_umutex_compat32, /* UMTX_OP_UMUTEX_WAIT */
3830 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
3831 __umtx_op_sem_wait_compat32, /* UMTX_OP_SEM_WAIT */
3832 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3833 __umtx_op_nwake_private32, /* UMTX_OP_NWAKE_PRIVATE */
3834 __umtx_op_wake2_umutex /* UMTX_OP_UMUTEX_WAKE2 */
3838 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
3840 if ((unsigned)uap->op < UMTX_OP_MAX)
3841 return (*op_table_compat32[uap->op])(td,
3842 (struct _umtx_op_args *)uap);
3848 umtx_thread_init(struct thread *td)
3850 td->td_umtxq = umtxq_alloc();
3851 td->td_umtxq->uq_thread = td;
3855 umtx_thread_fini(struct thread *td)
3857 umtxq_free(td->td_umtxq);
3861 * It will be called when new thread is created, e.g fork().
3864 umtx_thread_alloc(struct thread *td)
3869 uq->uq_inherited_pri = PRI_MAX;
3871 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
3872 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
3873 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
3874 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
3881 umtx_exec_hook(void *arg __unused, struct proc *p __unused,
3882 struct image_params *imgp __unused)
3884 umtx_thread_cleanup(curthread);
3888 * thread_exit() hook.
3891 umtx_thread_exit(struct thread *td)
3893 umtx_thread_cleanup(td);
3897 * clean up umtx data.
3900 umtx_thread_cleanup(struct thread *td)
3905 if ((uq = td->td_umtxq) == NULL)
3908 mtx_lock_spin(&umtx_lock);
3909 uq->uq_inherited_pri = PRI_MAX;
3910 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
3911 pi->pi_owner = NULL;
3912 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
3914 mtx_unlock_spin(&umtx_lock);
3916 sched_lend_user_prio(td, PRI_MAX);