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 * Wake up threads waiting on an userland object.
636 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
638 struct umtxq_chain *uc;
639 struct umtxq_queue *uh;
644 uc = umtxq_getchain(key);
645 UMTXQ_LOCKED_ASSERT(uc);
646 uh = umtxq_queue_lookup(key, q);
648 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
649 umtxq_remove_queue(uq, q);
660 * Wake up specified thread.
663 umtxq_signal_thread(struct umtx_q *uq)
665 struct umtxq_chain *uc;
667 uc = umtxq_getchain(&uq->uq_key);
668 UMTXQ_LOCKED_ASSERT(uc);
674 tstohz(const struct timespec *tsp)
678 TIMESPEC_TO_TIMEVAL(&tv, tsp);
683 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
684 const struct timespec *timeout)
687 timo->clockid = clockid;
689 kern_clock_gettime(curthread, clockid, &timo->end);
690 timo->cur = timo->end;
691 timespecadd(&timo->end, timeout);
693 timo->end = *timeout;
694 kern_clock_gettime(curthread, clockid, &timo->cur);
699 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
702 abs_timeout_init(timo, umtxtime->_clockid,
703 (umtxtime->_flags & UMTX_ABSTIME) != 0,
704 &umtxtime->_timeout);
708 abs_timeout_update(struct abs_timeout *timo)
710 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
714 abs_timeout_gethz(struct abs_timeout *timo)
718 if (timespeccmp(&timo->end, &timo->cur, <=))
721 timespecsub(&tts, &timo->cur);
722 return (tstohz(&tts));
726 * Put thread into sleep state, before sleeping, check if
727 * thread was removed from umtx queue.
730 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
732 struct umtxq_chain *uc;
735 uc = umtxq_getchain(&uq->uq_key);
736 UMTXQ_LOCKED_ASSERT(uc);
738 if (!(uq->uq_flags & UQF_UMTXQ))
740 if (abstime != NULL) {
741 timo = abs_timeout_gethz(abstime);
746 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
747 if (error != EWOULDBLOCK) {
748 umtxq_lock(&uq->uq_key);
752 abs_timeout_update(abstime);
753 umtxq_lock(&uq->uq_key);
759 * Convert userspace address into unique logical address.
762 umtx_key_get(void *addr, int type, int share, struct umtx_key *key)
764 struct thread *td = curthread;
766 vm_map_entry_t entry;
772 if (share == THREAD_SHARE) {
774 key->info.private.vs = td->td_proc->p_vmspace;
775 key->info.private.addr = (uintptr_t)addr;
777 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
778 map = &td->td_proc->p_vmspace->vm_map;
779 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
780 &entry, &key->info.shared.object, &pindex, &prot,
781 &wired) != KERN_SUCCESS) {
785 if ((share == PROCESS_SHARE) ||
786 (share == AUTO_SHARE &&
787 VM_INHERIT_SHARE == entry->inheritance)) {
789 key->info.shared.offset = entry->offset + entry->start -
791 vm_object_reference(key->info.shared.object);
794 key->info.private.vs = td->td_proc->p_vmspace;
795 key->info.private.addr = (uintptr_t)addr;
797 vm_map_lookup_done(map, entry);
808 umtx_key_release(struct umtx_key *key)
811 vm_object_deallocate(key->info.shared.object);
815 * Lock a umtx object.
818 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
819 const struct timespec *timeout)
821 struct abs_timeout timo;
829 abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
832 * Care must be exercised when dealing with umtx structure. It
833 * can fault on any access.
837 * Try the uncontested case. This should be done in userland.
839 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
841 /* The acquire succeeded. */
842 if (owner == UMTX_UNOWNED)
845 /* The address was invalid. */
849 /* If no one owns it but it is contested try to acquire it. */
850 if (owner == UMTX_CONTESTED) {
851 owner = casuword(&umtx->u_owner,
852 UMTX_CONTESTED, id | UMTX_CONTESTED);
854 if (owner == UMTX_CONTESTED)
857 /* The address was invalid. */
861 /* If this failed the lock has changed, restart. */
866 * If we caught a signal, we have retried and now
872 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
873 AUTO_SHARE, &uq->uq_key)) != 0)
876 umtxq_lock(&uq->uq_key);
877 umtxq_busy(&uq->uq_key);
879 umtxq_unbusy(&uq->uq_key);
880 umtxq_unlock(&uq->uq_key);
883 * Set the contested bit so that a release in user space
884 * knows to use the system call for unlock. If this fails
885 * either some one else has acquired the lock or it has been
888 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
890 /* The address was invalid. */
892 umtxq_lock(&uq->uq_key);
894 umtxq_unlock(&uq->uq_key);
895 umtx_key_release(&uq->uq_key);
900 * We set the contested bit, sleep. Otherwise the lock changed
901 * and we need to retry or we lost a race to the thread
902 * unlocking the umtx.
904 umtxq_lock(&uq->uq_key);
906 error = umtxq_sleep(uq, "umtx", timeout == NULL ? NULL :
909 umtxq_unlock(&uq->uq_key);
910 umtx_key_release(&uq->uq_key);
913 if (timeout == NULL) {
914 /* Mutex locking is restarted if it is interrupted. */
918 /* Timed-locking is not restarted. */
919 if (error == ERESTART)
926 * Unlock a umtx object.
929 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
938 * Make sure we own this mtx.
940 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
944 if ((owner & ~UMTX_CONTESTED) != id)
947 /* This should be done in userland */
948 if ((owner & UMTX_CONTESTED) == 0) {
949 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
957 /* We should only ever be in here for contested locks */
958 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
964 count = umtxq_count(&key);
968 * When unlocking the umtx, it must be marked as unowned if
969 * there is zero or one thread only waiting for it.
970 * Otherwise, it must be marked as contested.
972 old = casuword(&umtx->u_owner, owner,
973 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
975 umtxq_signal(&key,1);
978 umtx_key_release(&key);
986 #ifdef COMPAT_FREEBSD32
989 * Lock a umtx object.
992 do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id,
993 const struct timespec *timeout)
995 struct abs_timeout timo;
1003 if (timeout != NULL)
1004 abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
1007 * Care must be exercised when dealing with umtx structure. It
1008 * can fault on any access.
1012 * Try the uncontested case. This should be done in userland.
1014 owner = casuword32(m, UMUTEX_UNOWNED, id);
1016 /* The acquire succeeded. */
1017 if (owner == UMUTEX_UNOWNED)
1020 /* The address was invalid. */
1024 /* If no one owns it but it is contested try to acquire it. */
1025 if (owner == UMUTEX_CONTESTED) {
1026 owner = casuword32(m,
1027 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1028 if (owner == UMUTEX_CONTESTED)
1031 /* The address was invalid. */
1035 /* If this failed the lock has changed, restart. */
1040 * If we caught a signal, we have retried and now
1046 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
1047 AUTO_SHARE, &uq->uq_key)) != 0)
1050 umtxq_lock(&uq->uq_key);
1051 umtxq_busy(&uq->uq_key);
1053 umtxq_unbusy(&uq->uq_key);
1054 umtxq_unlock(&uq->uq_key);
1057 * Set the contested bit so that a release in user space
1058 * knows to use the system call for unlock. If this fails
1059 * either some one else has acquired the lock or it has been
1062 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
1064 /* The address was invalid. */
1066 umtxq_lock(&uq->uq_key);
1068 umtxq_unlock(&uq->uq_key);
1069 umtx_key_release(&uq->uq_key);
1074 * We set the contested bit, sleep. Otherwise the lock changed
1075 * and we need to retry or we lost a race to the thread
1076 * unlocking the umtx.
1078 umtxq_lock(&uq->uq_key);
1080 error = umtxq_sleep(uq, "umtx", timeout == NULL ?
1083 umtxq_unlock(&uq->uq_key);
1084 umtx_key_release(&uq->uq_key);
1087 if (timeout == NULL) {
1088 /* Mutex locking is restarted if it is interrupted. */
1092 /* Timed-locking is not restarted. */
1093 if (error == ERESTART)
1100 * Unlock a umtx object.
1103 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
1105 struct umtx_key key;
1112 * Make sure we own this mtx.
1114 owner = fuword32(m);
1118 if ((owner & ~UMUTEX_CONTESTED) != id)
1121 /* This should be done in userland */
1122 if ((owner & UMUTEX_CONTESTED) == 0) {
1123 old = casuword32(m, owner, UMUTEX_UNOWNED);
1131 /* We should only ever be in here for contested locks */
1132 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1138 count = umtxq_count(&key);
1142 * When unlocking the umtx, it must be marked as unowned if
1143 * there is zero or one thread only waiting for it.
1144 * Otherwise, it must be marked as contested.
1146 old = casuword32(m, owner,
1147 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1149 umtxq_signal(&key,1);
1152 umtx_key_release(&key);
1162 * Fetch and compare value, sleep on the address if value is not changed.
1165 do_wait(struct thread *td, void *addr, u_long id,
1166 struct _umtx_time *timeout, int compat32, int is_private)
1168 struct abs_timeout timo;
1174 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1175 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1178 if (timeout != NULL)
1179 abs_timeout_init2(&timo, timeout);
1181 umtxq_lock(&uq->uq_key);
1183 umtxq_unlock(&uq->uq_key);
1187 tmp = (unsigned int)fuword32(addr);
1188 umtxq_lock(&uq->uq_key);
1190 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
1192 if ((uq->uq_flags & UQF_UMTXQ) == 0)
1196 umtxq_unlock(&uq->uq_key);
1197 umtx_key_release(&uq->uq_key);
1198 if (error == ERESTART)
1204 * Wake up threads sleeping on the specified address.
1207 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1209 struct umtx_key key;
1212 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1213 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1216 ret = umtxq_signal(&key, n_wake);
1218 umtx_key_release(&key);
1223 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1226 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1227 struct _umtx_time *timeout, int mode)
1229 struct abs_timeout timo;
1231 uint32_t owner, old, id;
1237 if (timeout != NULL)
1238 abs_timeout_init2(&timo, timeout);
1241 * Care must be exercised when dealing with umtx structure. It
1242 * can fault on any access.
1245 owner = fuword32(__DEVOLATILE(void *, &m->m_owner));
1246 if (mode == _UMUTEX_WAIT) {
1247 if (owner == UMUTEX_UNOWNED || owner == UMUTEX_CONTESTED)
1251 * Try the uncontested case. This should be done in userland.
1253 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
1255 /* The acquire succeeded. */
1256 if (owner == UMUTEX_UNOWNED)
1259 /* The address was invalid. */
1263 /* If no one owns it but it is contested try to acquire it. */
1264 if (owner == UMUTEX_CONTESTED) {
1265 owner = casuword32(&m->m_owner,
1266 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1268 if (owner == UMUTEX_CONTESTED)
1271 /* The address was invalid. */
1275 /* If this failed the lock has changed, restart. */
1280 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1281 (owner & ~UMUTEX_CONTESTED) == id)
1284 if (mode == _UMUTEX_TRY)
1288 * If we caught a signal, we have retried and now
1294 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1295 GET_SHARE(flags), &uq->uq_key)) != 0)
1298 umtxq_lock(&uq->uq_key);
1299 umtxq_busy(&uq->uq_key);
1301 umtxq_unlock(&uq->uq_key);
1304 * Set the contested bit so that a release in user space
1305 * knows to use the system call for unlock. If this fails
1306 * either some one else has acquired the lock or it has been
1309 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
1311 /* The address was invalid. */
1313 umtxq_lock(&uq->uq_key);
1315 umtxq_unbusy(&uq->uq_key);
1316 umtxq_unlock(&uq->uq_key);
1317 umtx_key_release(&uq->uq_key);
1322 * We set the contested bit, sleep. Otherwise the lock changed
1323 * and we need to retry or we lost a race to the thread
1324 * unlocking the umtx.
1326 umtxq_lock(&uq->uq_key);
1327 umtxq_unbusy(&uq->uq_key);
1329 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1332 umtxq_unlock(&uq->uq_key);
1333 umtx_key_release(&uq->uq_key);
1340 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1343 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags)
1345 struct umtx_key key;
1346 uint32_t owner, old, id;
1352 * Make sure we own this mtx.
1354 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1358 if ((owner & ~UMUTEX_CONTESTED) != id)
1361 if ((owner & UMUTEX_CONTESTED) == 0) {
1362 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
1370 /* We should only ever be in here for contested locks */
1371 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1377 count = umtxq_count(&key);
1381 * When unlocking the umtx, it must be marked as unowned if
1382 * there is zero or one thread only waiting for it.
1383 * Otherwise, it must be marked as contested.
1385 old = casuword32(&m->m_owner, owner,
1386 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1388 umtxq_signal(&key,1);
1391 umtx_key_release(&key);
1400 * Check if the mutex is available and wake up a waiter,
1401 * only for simple mutex.
1404 do_wake_umutex(struct thread *td, struct umutex *m)
1406 struct umtx_key key;
1412 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1416 if ((owner & ~UMUTEX_CONTESTED) != 0)
1419 flags = fuword32(&m->m_flags);
1421 /* We should only ever be in here for contested locks */
1422 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1428 count = umtxq_count(&key);
1432 owner = casuword32(&m->m_owner, UMUTEX_CONTESTED, UMUTEX_UNOWNED);
1435 if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1436 umtxq_signal(&key, 1);
1439 umtx_key_release(&key);
1444 * Check if the mutex has waiters and tries to fix contention bit.
1447 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1449 struct umtx_key key;
1450 uint32_t owner, old;
1455 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
1457 type = TYPE_NORMAL_UMUTEX;
1459 case UMUTEX_PRIO_INHERIT:
1460 type = TYPE_PI_UMUTEX;
1462 case UMUTEX_PRIO_PROTECT:
1463 type = TYPE_PP_UMUTEX;
1468 if ((error = umtx_key_get(m, type, GET_SHARE(flags),
1475 count = umtxq_count(&key);
1478 * Only repair contention bit if there is a waiter, this means the mutex
1479 * is still being referenced by userland code, otherwise don't update
1483 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1484 while ((owner & UMUTEX_CONTESTED) ==0) {
1485 old = casuword32(&m->m_owner, owner,
1486 owner|UMUTEX_CONTESTED);
1491 } else if (count == 1) {
1492 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1493 while ((owner & ~UMUTEX_CONTESTED) != 0 &&
1494 (owner & UMUTEX_CONTESTED) == 0) {
1495 old = casuword32(&m->m_owner, owner,
1496 owner|UMUTEX_CONTESTED);
1505 umtxq_signal(&key, INT_MAX);
1507 else if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1508 umtxq_signal(&key, 1);
1511 umtx_key_release(&key);
1515 static inline struct umtx_pi *
1516 umtx_pi_alloc(int flags)
1520 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1521 TAILQ_INIT(&pi->pi_blocked);
1522 atomic_add_int(&umtx_pi_allocated, 1);
1527 umtx_pi_free(struct umtx_pi *pi)
1529 uma_zfree(umtx_pi_zone, pi);
1530 atomic_add_int(&umtx_pi_allocated, -1);
1534 * Adjust the thread's position on a pi_state after its priority has been
1538 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1540 struct umtx_q *uq, *uq1, *uq2;
1543 mtx_assert(&umtx_lock, MA_OWNED);
1550 * Check if the thread needs to be moved on the blocked chain.
1551 * It needs to be moved if either its priority is lower than
1552 * the previous thread or higher than the next thread.
1554 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1555 uq2 = TAILQ_NEXT(uq, uq_lockq);
1556 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1557 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1559 * Remove thread from blocked chain and determine where
1560 * it should be moved to.
1562 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1563 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1564 td1 = uq1->uq_thread;
1565 MPASS(td1->td_proc->p_magic == P_MAGIC);
1566 if (UPRI(td1) > UPRI(td))
1571 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1573 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1579 * Propagate priority when a thread is blocked on POSIX
1583 umtx_propagate_priority(struct thread *td)
1589 mtx_assert(&umtx_lock, MA_OWNED);
1592 pi = uq->uq_pi_blocked;
1598 if (td == NULL || td == curthread)
1601 MPASS(td->td_proc != NULL);
1602 MPASS(td->td_proc->p_magic == P_MAGIC);
1605 if (td->td_lend_user_pri > pri)
1606 sched_lend_user_prio(td, pri);
1614 * Pick up the lock that td is blocked on.
1617 pi = uq->uq_pi_blocked;
1620 /* Resort td on the list if needed. */
1621 umtx_pi_adjust_thread(pi, td);
1626 * Unpropagate priority for a PI mutex when a thread blocked on
1627 * it is interrupted by signal or resumed by others.
1630 umtx_repropagate_priority(struct umtx_pi *pi)
1632 struct umtx_q *uq, *uq_owner;
1633 struct umtx_pi *pi2;
1636 mtx_assert(&umtx_lock, MA_OWNED);
1638 while (pi != NULL && pi->pi_owner != NULL) {
1640 uq_owner = pi->pi_owner->td_umtxq;
1642 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1643 uq = TAILQ_FIRST(&pi2->pi_blocked);
1645 if (pri > UPRI(uq->uq_thread))
1646 pri = UPRI(uq->uq_thread);
1650 if (pri > uq_owner->uq_inherited_pri)
1651 pri = uq_owner->uq_inherited_pri;
1652 thread_lock(pi->pi_owner);
1653 sched_lend_user_prio(pi->pi_owner, pri);
1654 thread_unlock(pi->pi_owner);
1655 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1656 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1661 * Insert a PI mutex into owned list.
1664 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1666 struct umtx_q *uq_owner;
1668 uq_owner = owner->td_umtxq;
1669 mtx_assert(&umtx_lock, MA_OWNED);
1670 if (pi->pi_owner != NULL)
1671 panic("pi_ower != NULL");
1672 pi->pi_owner = owner;
1673 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1677 * Claim ownership of a PI mutex.
1680 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1682 struct umtx_q *uq, *uq_owner;
1684 uq_owner = owner->td_umtxq;
1685 mtx_lock_spin(&umtx_lock);
1686 if (pi->pi_owner == owner) {
1687 mtx_unlock_spin(&umtx_lock);
1691 if (pi->pi_owner != NULL) {
1693 * userland may have already messed the mutex, sigh.
1695 mtx_unlock_spin(&umtx_lock);
1698 umtx_pi_setowner(pi, owner);
1699 uq = TAILQ_FIRST(&pi->pi_blocked);
1703 pri = UPRI(uq->uq_thread);
1705 if (pri < UPRI(owner))
1706 sched_lend_user_prio(owner, pri);
1707 thread_unlock(owner);
1709 mtx_unlock_spin(&umtx_lock);
1714 * Adjust a thread's order position in its blocked PI mutex,
1715 * this may result new priority propagating process.
1718 umtx_pi_adjust(struct thread *td, u_char oldpri)
1724 mtx_lock_spin(&umtx_lock);
1726 * Pick up the lock that td is blocked on.
1728 pi = uq->uq_pi_blocked;
1730 umtx_pi_adjust_thread(pi, td);
1731 umtx_repropagate_priority(pi);
1733 mtx_unlock_spin(&umtx_lock);
1737 * Sleep on a PI mutex.
1740 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi,
1741 uint32_t owner, const char *wmesg, struct abs_timeout *timo)
1743 struct umtxq_chain *uc;
1744 struct thread *td, *td1;
1750 KASSERT(td == curthread, ("inconsistent uq_thread"));
1751 uc = umtxq_getchain(&uq->uq_key);
1752 UMTXQ_LOCKED_ASSERT(uc);
1753 UMTXQ_BUSY_ASSERT(uc);
1755 mtx_lock_spin(&umtx_lock);
1756 if (pi->pi_owner == NULL) {
1757 mtx_unlock_spin(&umtx_lock);
1758 /* XXX Only look up thread in current process. */
1759 td1 = tdfind(owner, curproc->p_pid);
1760 mtx_lock_spin(&umtx_lock);
1762 if (pi->pi_owner == NULL)
1763 umtx_pi_setowner(pi, td1);
1764 PROC_UNLOCK(td1->td_proc);
1768 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1769 pri = UPRI(uq1->uq_thread);
1775 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1777 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1779 uq->uq_pi_blocked = pi;
1781 td->td_flags |= TDF_UPIBLOCKED;
1783 umtx_propagate_priority(td);
1784 mtx_unlock_spin(&umtx_lock);
1785 umtxq_unbusy(&uq->uq_key);
1787 error = umtxq_sleep(uq, wmesg, timo);
1790 mtx_lock_spin(&umtx_lock);
1791 uq->uq_pi_blocked = NULL;
1793 td->td_flags &= ~TDF_UPIBLOCKED;
1795 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1796 umtx_repropagate_priority(pi);
1797 mtx_unlock_spin(&umtx_lock);
1798 umtxq_unlock(&uq->uq_key);
1804 * Add reference count for a PI mutex.
1807 umtx_pi_ref(struct umtx_pi *pi)
1809 struct umtxq_chain *uc;
1811 uc = umtxq_getchain(&pi->pi_key);
1812 UMTXQ_LOCKED_ASSERT(uc);
1817 * Decrease reference count for a PI mutex, if the counter
1818 * is decreased to zero, its memory space is freed.
1821 umtx_pi_unref(struct umtx_pi *pi)
1823 struct umtxq_chain *uc;
1825 uc = umtxq_getchain(&pi->pi_key);
1826 UMTXQ_LOCKED_ASSERT(uc);
1827 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1828 if (--pi->pi_refcount == 0) {
1829 mtx_lock_spin(&umtx_lock);
1830 if (pi->pi_owner != NULL) {
1831 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested,
1833 pi->pi_owner = NULL;
1835 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1836 ("blocked queue not empty"));
1837 mtx_unlock_spin(&umtx_lock);
1838 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1844 * Find a PI mutex in hash table.
1846 static struct umtx_pi *
1847 umtx_pi_lookup(struct umtx_key *key)
1849 struct umtxq_chain *uc;
1852 uc = umtxq_getchain(key);
1853 UMTXQ_LOCKED_ASSERT(uc);
1855 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1856 if (umtx_key_match(&pi->pi_key, key)) {
1864 * Insert a PI mutex into hash table.
1867 umtx_pi_insert(struct umtx_pi *pi)
1869 struct umtxq_chain *uc;
1871 uc = umtxq_getchain(&pi->pi_key);
1872 UMTXQ_LOCKED_ASSERT(uc);
1873 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1880 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1881 struct _umtx_time *timeout, int try)
1883 struct abs_timeout timo;
1885 struct umtx_pi *pi, *new_pi;
1886 uint32_t id, owner, old;
1892 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1896 if (timeout != NULL)
1897 abs_timeout_init2(&timo, timeout);
1899 umtxq_lock(&uq->uq_key);
1900 pi = umtx_pi_lookup(&uq->uq_key);
1902 new_pi = umtx_pi_alloc(M_NOWAIT);
1903 if (new_pi == NULL) {
1904 umtxq_unlock(&uq->uq_key);
1905 new_pi = umtx_pi_alloc(M_WAITOK);
1906 umtxq_lock(&uq->uq_key);
1907 pi = umtx_pi_lookup(&uq->uq_key);
1909 umtx_pi_free(new_pi);
1913 if (new_pi != NULL) {
1914 new_pi->pi_key = uq->uq_key;
1915 umtx_pi_insert(new_pi);
1920 umtxq_unlock(&uq->uq_key);
1923 * Care must be exercised when dealing with umtx structure. It
1924 * can fault on any access.
1928 * Try the uncontested case. This should be done in userland.
1930 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
1932 /* The acquire succeeded. */
1933 if (owner == UMUTEX_UNOWNED) {
1938 /* The address was invalid. */
1944 /* If no one owns it but it is contested try to acquire it. */
1945 if (owner == UMUTEX_CONTESTED) {
1946 owner = casuword32(&m->m_owner,
1947 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1949 if (owner == UMUTEX_CONTESTED) {
1950 umtxq_lock(&uq->uq_key);
1951 umtxq_busy(&uq->uq_key);
1952 error = umtx_pi_claim(pi, td);
1953 umtxq_unbusy(&uq->uq_key);
1954 umtxq_unlock(&uq->uq_key);
1958 /* The address was invalid. */
1964 /* If this failed the lock has changed, restart. */
1968 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1969 (owner & ~UMUTEX_CONTESTED) == id) {
1980 * If we caught a signal, we have retried and now
1986 umtxq_lock(&uq->uq_key);
1987 umtxq_busy(&uq->uq_key);
1988 umtxq_unlock(&uq->uq_key);
1991 * Set the contested bit so that a release in user space
1992 * knows to use the system call for unlock. If this fails
1993 * either some one else has acquired the lock or it has been
1996 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
1998 /* The address was invalid. */
2000 umtxq_lock(&uq->uq_key);
2001 umtxq_unbusy(&uq->uq_key);
2002 umtxq_unlock(&uq->uq_key);
2007 umtxq_lock(&uq->uq_key);
2009 * We set the contested bit, sleep. Otherwise the lock changed
2010 * and we need to retry or we lost a race to the thread
2011 * unlocking the umtx.
2014 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2015 "umtxpi", timeout == NULL ? NULL : &timo);
2017 umtxq_unbusy(&uq->uq_key);
2018 umtxq_unlock(&uq->uq_key);
2022 umtxq_lock(&uq->uq_key);
2024 umtxq_unlock(&uq->uq_key);
2026 umtx_key_release(&uq->uq_key);
2031 * Unlock a PI mutex.
2034 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags)
2036 struct umtx_key key;
2037 struct umtx_q *uq_first, *uq_first2, *uq_me;
2038 struct umtx_pi *pi, *pi2;
2039 uint32_t owner, old, id;
2046 * Make sure we own this mtx.
2048 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
2052 if ((owner & ~UMUTEX_CONTESTED) != id)
2055 /* This should be done in userland */
2056 if ((owner & UMUTEX_CONTESTED) == 0) {
2057 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
2065 /* We should only ever be in here for contested locks */
2066 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
2072 count = umtxq_count_pi(&key, &uq_first);
2073 if (uq_first != NULL) {
2074 mtx_lock_spin(&umtx_lock);
2075 pi = uq_first->uq_pi_blocked;
2076 KASSERT(pi != NULL, ("pi == NULL?"));
2077 if (pi->pi_owner != curthread) {
2078 mtx_unlock_spin(&umtx_lock);
2081 umtx_key_release(&key);
2082 /* userland messed the mutex */
2085 uq_me = curthread->td_umtxq;
2086 pi->pi_owner = NULL;
2087 TAILQ_REMOVE(&uq_me->uq_pi_contested, pi, pi_link);
2088 /* get highest priority thread which is still sleeping. */
2089 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2090 while (uq_first != NULL &&
2091 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2092 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2095 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2096 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2097 if (uq_first2 != NULL) {
2098 if (pri > UPRI(uq_first2->uq_thread))
2099 pri = UPRI(uq_first2->uq_thread);
2102 thread_lock(curthread);
2103 sched_lend_user_prio(curthread, pri);
2104 thread_unlock(curthread);
2105 mtx_unlock_spin(&umtx_lock);
2107 umtxq_signal_thread(uq_first);
2112 * When unlocking the umtx, it must be marked as unowned if
2113 * there is zero or one thread only waiting for it.
2114 * Otherwise, it must be marked as contested.
2116 old = casuword32(&m->m_owner, owner,
2117 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
2122 umtx_key_release(&key);
2134 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2135 struct _umtx_time *timeout, int try)
2137 struct abs_timeout timo;
2138 struct umtx_q *uq, *uq2;
2142 int error, pri, old_inherited_pri, su;
2146 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2150 if (timeout != NULL)
2151 abs_timeout_init2(&timo, timeout);
2153 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2155 old_inherited_pri = uq->uq_inherited_pri;
2156 umtxq_lock(&uq->uq_key);
2157 umtxq_busy(&uq->uq_key);
2158 umtxq_unlock(&uq->uq_key);
2160 ceiling = RTP_PRIO_MAX - fuword32(&m->m_ceilings[0]);
2161 if (ceiling > RTP_PRIO_MAX) {
2166 mtx_lock_spin(&umtx_lock);
2167 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2168 mtx_unlock_spin(&umtx_lock);
2172 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2173 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2175 if (uq->uq_inherited_pri < UPRI(td))
2176 sched_lend_user_prio(td, uq->uq_inherited_pri);
2179 mtx_unlock_spin(&umtx_lock);
2181 owner = casuword32(&m->m_owner,
2182 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
2184 if (owner == UMUTEX_CONTESTED) {
2189 /* The address was invalid. */
2195 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
2196 (owner & ~UMUTEX_CONTESTED) == id) {
2207 * If we caught a signal, we have retried and now
2213 umtxq_lock(&uq->uq_key);
2215 umtxq_unbusy(&uq->uq_key);
2216 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2219 umtxq_unlock(&uq->uq_key);
2221 mtx_lock_spin(&umtx_lock);
2222 uq->uq_inherited_pri = old_inherited_pri;
2224 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2225 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2227 if (pri > UPRI(uq2->uq_thread))
2228 pri = UPRI(uq2->uq_thread);
2231 if (pri > uq->uq_inherited_pri)
2232 pri = uq->uq_inherited_pri;
2234 sched_lend_user_prio(td, pri);
2236 mtx_unlock_spin(&umtx_lock);
2240 mtx_lock_spin(&umtx_lock);
2241 uq->uq_inherited_pri = old_inherited_pri;
2243 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2244 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2246 if (pri > UPRI(uq2->uq_thread))
2247 pri = UPRI(uq2->uq_thread);
2250 if (pri > uq->uq_inherited_pri)
2251 pri = uq->uq_inherited_pri;
2253 sched_lend_user_prio(td, pri);
2255 mtx_unlock_spin(&umtx_lock);
2259 umtxq_lock(&uq->uq_key);
2260 umtxq_unbusy(&uq->uq_key);
2261 umtxq_unlock(&uq->uq_key);
2262 umtx_key_release(&uq->uq_key);
2267 * Unlock a PP mutex.
2270 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags)
2272 struct umtx_key key;
2273 struct umtx_q *uq, *uq2;
2277 int error, pri, new_inherited_pri, su;
2281 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2284 * Make sure we own this mtx.
2286 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
2290 if ((owner & ~UMUTEX_CONTESTED) != id)
2293 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2298 new_inherited_pri = PRI_MAX;
2300 rceiling = RTP_PRIO_MAX - rceiling;
2301 if (rceiling > RTP_PRIO_MAX)
2303 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2306 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2313 * For priority protected mutex, always set unlocked state
2314 * to UMUTEX_CONTESTED, so that userland always enters kernel
2315 * to lock the mutex, it is necessary because thread priority
2316 * has to be adjusted for such mutex.
2318 error = suword32(__DEVOLATILE(uint32_t *, &m->m_owner),
2323 umtxq_signal(&key, 1);
2330 mtx_lock_spin(&umtx_lock);
2332 uq->uq_inherited_pri = new_inherited_pri;
2334 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2335 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2337 if (pri > UPRI(uq2->uq_thread))
2338 pri = UPRI(uq2->uq_thread);
2341 if (pri > uq->uq_inherited_pri)
2342 pri = uq->uq_inherited_pri;
2344 sched_lend_user_prio(td, pri);
2346 mtx_unlock_spin(&umtx_lock);
2348 umtx_key_release(&key);
2353 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2354 uint32_t *old_ceiling)
2357 uint32_t save_ceiling;
2362 flags = fuword32(&m->m_flags);
2363 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2365 if (ceiling > RTP_PRIO_MAX)
2369 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2373 umtxq_lock(&uq->uq_key);
2374 umtxq_busy(&uq->uq_key);
2375 umtxq_unlock(&uq->uq_key);
2377 save_ceiling = fuword32(&m->m_ceilings[0]);
2379 owner = casuword32(&m->m_owner,
2380 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
2382 if (owner == UMUTEX_CONTESTED) {
2383 suword32(&m->m_ceilings[0], ceiling);
2384 suword32(__DEVOLATILE(uint32_t *, &m->m_owner),
2390 /* The address was invalid. */
2396 if ((owner & ~UMUTEX_CONTESTED) == id) {
2397 suword32(&m->m_ceilings[0], ceiling);
2403 * If we caught a signal, we have retried and now
2410 * We set the contested bit, sleep. Otherwise the lock changed
2411 * and we need to retry or we lost a race to the thread
2412 * unlocking the umtx.
2414 umtxq_lock(&uq->uq_key);
2416 umtxq_unbusy(&uq->uq_key);
2417 error = umtxq_sleep(uq, "umtxpp", NULL);
2419 umtxq_unlock(&uq->uq_key);
2421 umtxq_lock(&uq->uq_key);
2423 umtxq_signal(&uq->uq_key, INT_MAX);
2424 umtxq_unbusy(&uq->uq_key);
2425 umtxq_unlock(&uq->uq_key);
2426 umtx_key_release(&uq->uq_key);
2427 if (error == 0 && old_ceiling != NULL)
2428 suword32(old_ceiling, save_ceiling);
2433 * Lock a userland POSIX mutex.
2436 do_lock_umutex(struct thread *td, struct umutex *m,
2437 struct _umtx_time *timeout, int mode)
2442 flags = fuword32(&m->m_flags);
2446 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2448 error = do_lock_normal(td, m, flags, timeout, mode);
2450 case UMUTEX_PRIO_INHERIT:
2451 error = do_lock_pi(td, m, flags, timeout, mode);
2453 case UMUTEX_PRIO_PROTECT:
2454 error = do_lock_pp(td, m, flags, timeout, mode);
2459 if (timeout == NULL) {
2460 if (error == EINTR && mode != _UMUTEX_WAIT)
2463 /* Timed-locking is not restarted. */
2464 if (error == ERESTART)
2471 * Unlock a userland POSIX mutex.
2474 do_unlock_umutex(struct thread *td, struct umutex *m)
2478 flags = fuword32(&m->m_flags);
2482 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2484 return (do_unlock_normal(td, m, flags));
2485 case UMUTEX_PRIO_INHERIT:
2486 return (do_unlock_pi(td, m, flags));
2487 case UMUTEX_PRIO_PROTECT:
2488 return (do_unlock_pp(td, m, flags));
2495 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2496 struct timespec *timeout, u_long wflags)
2498 struct abs_timeout timo;
2505 flags = fuword32(&cv->c_flags);
2506 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2510 if ((wflags & CVWAIT_CLOCKID) != 0) {
2511 clockid = fuword32(&cv->c_clockid);
2512 if (clockid < CLOCK_REALTIME ||
2513 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2514 /* hmm, only HW clock id will work. */
2518 clockid = CLOCK_REALTIME;
2521 umtxq_lock(&uq->uq_key);
2522 umtxq_busy(&uq->uq_key);
2524 umtxq_unlock(&uq->uq_key);
2527 * Set c_has_waiters to 1 before releasing user mutex, also
2528 * don't modify cache line when unnecessary.
2530 if (fuword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters)) == 0)
2531 suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 1);
2533 umtxq_lock(&uq->uq_key);
2534 umtxq_unbusy(&uq->uq_key);
2535 umtxq_unlock(&uq->uq_key);
2537 error = do_unlock_umutex(td, m);
2539 if (timeout != NULL)
2540 abs_timeout_init(&timo, clockid, ((wflags & CVWAIT_ABSTIME) != 0),
2543 umtxq_lock(&uq->uq_key);
2545 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2549 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2553 * This must be timeout,interrupted by signal or
2554 * surprious wakeup, clear c_has_waiter flag when
2557 umtxq_busy(&uq->uq_key);
2558 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2559 int oldlen = uq->uq_cur_queue->length;
2562 umtxq_unlock(&uq->uq_key);
2564 __DEVOLATILE(uint32_t *,
2565 &cv->c_has_waiters), 0);
2566 umtxq_lock(&uq->uq_key);
2569 umtxq_unbusy(&uq->uq_key);
2570 if (error == ERESTART)
2574 umtxq_unlock(&uq->uq_key);
2575 umtx_key_release(&uq->uq_key);
2580 * Signal a userland condition variable.
2583 do_cv_signal(struct thread *td, struct ucond *cv)
2585 struct umtx_key key;
2586 int error, cnt, nwake;
2589 flags = fuword32(&cv->c_flags);
2590 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2594 cnt = umtxq_count(&key);
2595 nwake = umtxq_signal(&key, 1);
2599 __DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0);
2604 umtx_key_release(&key);
2609 do_cv_broadcast(struct thread *td, struct ucond *cv)
2611 struct umtx_key key;
2615 flags = fuword32(&cv->c_flags);
2616 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2621 umtxq_signal(&key, INT_MAX);
2624 error = suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0);
2630 umtx_key_release(&key);
2635 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, struct _umtx_time *timeout)
2637 struct abs_timeout timo;
2639 uint32_t flags, wrflags;
2640 int32_t state, oldstate;
2641 int32_t blocked_readers;
2645 flags = fuword32(&rwlock->rw_flags);
2646 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2650 if (timeout != NULL)
2651 abs_timeout_init2(&timo, timeout);
2653 wrflags = URWLOCK_WRITE_OWNER;
2654 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2655 wrflags |= URWLOCK_WRITE_WAITERS;
2658 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2659 /* try to lock it */
2660 while (!(state & wrflags)) {
2661 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2662 umtx_key_release(&uq->uq_key);
2665 oldstate = casuword32(&rwlock->rw_state, state, state + 1);
2666 if (oldstate == state) {
2667 umtx_key_release(&uq->uq_key);
2676 /* grab monitor lock */
2677 umtxq_lock(&uq->uq_key);
2678 umtxq_busy(&uq->uq_key);
2679 umtxq_unlock(&uq->uq_key);
2682 * re-read the state, in case it changed between the try-lock above
2683 * and the check below
2685 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2687 /* set read contention bit */
2688 while ((state & wrflags) && !(state & URWLOCK_READ_WAITERS)) {
2689 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_READ_WAITERS);
2690 if (oldstate == state)
2695 /* state is changed while setting flags, restart */
2696 if (!(state & wrflags)) {
2697 umtxq_lock(&uq->uq_key);
2698 umtxq_unbusy(&uq->uq_key);
2699 umtxq_unlock(&uq->uq_key);
2704 /* contention bit is set, before sleeping, increase read waiter count */
2705 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2706 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2708 while (state & wrflags) {
2709 umtxq_lock(&uq->uq_key);
2711 umtxq_unbusy(&uq->uq_key);
2713 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2716 umtxq_busy(&uq->uq_key);
2718 umtxq_unlock(&uq->uq_key);
2721 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2724 /* decrease read waiter count, and may clear read contention bit */
2725 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2726 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2727 if (blocked_readers == 1) {
2728 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2730 oldstate = casuword32(&rwlock->rw_state, state,
2731 state & ~URWLOCK_READ_WAITERS);
2732 if (oldstate == state)
2738 umtxq_lock(&uq->uq_key);
2739 umtxq_unbusy(&uq->uq_key);
2740 umtxq_unlock(&uq->uq_key);
2742 umtx_key_release(&uq->uq_key);
2743 if (error == ERESTART)
2749 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2751 struct abs_timeout timo;
2754 int32_t state, oldstate;
2755 int32_t blocked_writers;
2756 int32_t blocked_readers;
2760 flags = fuword32(&rwlock->rw_flags);
2761 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2765 if (timeout != NULL)
2766 abs_timeout_init2(&timo, timeout);
2768 blocked_readers = 0;
2770 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2771 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2772 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_WRITE_OWNER);
2773 if (oldstate == state) {
2774 umtx_key_release(&uq->uq_key);
2781 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
2782 blocked_readers != 0) {
2783 umtxq_lock(&uq->uq_key);
2784 umtxq_busy(&uq->uq_key);
2785 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
2786 umtxq_unbusy(&uq->uq_key);
2787 umtxq_unlock(&uq->uq_key);
2793 /* grab monitor lock */
2794 umtxq_lock(&uq->uq_key);
2795 umtxq_busy(&uq->uq_key);
2796 umtxq_unlock(&uq->uq_key);
2799 * re-read the state, in case it changed between the try-lock above
2800 * and the check below
2802 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2804 while (((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) &&
2805 (state & URWLOCK_WRITE_WAITERS) == 0) {
2806 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_WRITE_WAITERS);
2807 if (oldstate == state)
2812 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2813 umtxq_lock(&uq->uq_key);
2814 umtxq_unbusy(&uq->uq_key);
2815 umtxq_unlock(&uq->uq_key);
2819 blocked_writers = fuword32(&rwlock->rw_blocked_writers);
2820 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
2822 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
2823 umtxq_lock(&uq->uq_key);
2824 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2825 umtxq_unbusy(&uq->uq_key);
2827 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2830 umtxq_busy(&uq->uq_key);
2831 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2832 umtxq_unlock(&uq->uq_key);
2835 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2838 blocked_writers = fuword32(&rwlock->rw_blocked_writers);
2839 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
2840 if (blocked_writers == 1) {
2841 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2843 oldstate = casuword32(&rwlock->rw_state, state,
2844 state & ~URWLOCK_WRITE_WAITERS);
2845 if (oldstate == state)
2849 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2851 blocked_readers = 0;
2853 umtxq_lock(&uq->uq_key);
2854 umtxq_unbusy(&uq->uq_key);
2855 umtxq_unlock(&uq->uq_key);
2858 umtx_key_release(&uq->uq_key);
2859 if (error == ERESTART)
2865 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
2869 int32_t state, oldstate;
2870 int error, q, count;
2873 flags = fuword32(&rwlock->rw_flags);
2874 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2878 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2879 if (state & URWLOCK_WRITE_OWNER) {
2881 oldstate = casuword32(&rwlock->rw_state, state,
2882 state & ~URWLOCK_WRITE_OWNER);
2883 if (oldstate != state) {
2885 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
2892 } else if (URWLOCK_READER_COUNT(state) != 0) {
2894 oldstate = casuword32(&rwlock->rw_state, state,
2896 if (oldstate != state) {
2898 if (URWLOCK_READER_COUNT(oldstate) == 0) {
2913 if (!(flags & URWLOCK_PREFER_READER)) {
2914 if (state & URWLOCK_WRITE_WAITERS) {
2916 q = UMTX_EXCLUSIVE_QUEUE;
2917 } else if (state & URWLOCK_READ_WAITERS) {
2919 q = UMTX_SHARED_QUEUE;
2922 if (state & URWLOCK_READ_WAITERS) {
2924 q = UMTX_SHARED_QUEUE;
2925 } else if (state & URWLOCK_WRITE_WAITERS) {
2927 q = UMTX_EXCLUSIVE_QUEUE;
2932 umtxq_lock(&uq->uq_key);
2933 umtxq_busy(&uq->uq_key);
2934 umtxq_signal_queue(&uq->uq_key, count, q);
2935 umtxq_unbusy(&uq->uq_key);
2936 umtxq_unlock(&uq->uq_key);
2939 umtx_key_release(&uq->uq_key);
2944 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
2946 struct abs_timeout timo;
2948 uint32_t flags, count;
2952 flags = fuword32(&sem->_flags);
2953 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
2957 if (timeout != NULL)
2958 abs_timeout_init2(&timo, timeout);
2960 umtxq_lock(&uq->uq_key);
2961 umtxq_busy(&uq->uq_key);
2963 umtxq_unlock(&uq->uq_key);
2964 casuword32(__DEVOLATILE(uint32_t *, &sem->_has_waiters), 0, 1);
2965 count = fuword32(__DEVOLATILE(uint32_t *, &sem->_count));
2967 umtxq_lock(&uq->uq_key);
2968 umtxq_unbusy(&uq->uq_key);
2970 umtxq_unlock(&uq->uq_key);
2971 umtx_key_release(&uq->uq_key);
2974 umtxq_lock(&uq->uq_key);
2975 umtxq_unbusy(&uq->uq_key);
2977 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
2979 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2983 /* A relative timeout cannot be restarted. */
2984 if (error == ERESTART && timeout != NULL &&
2985 (timeout->_flags & UMTX_ABSTIME) == 0)
2988 umtxq_unlock(&uq->uq_key);
2989 umtx_key_release(&uq->uq_key);
2994 * Signal a userland condition variable.
2997 do_sem_wake(struct thread *td, struct _usem *sem)
2999 struct umtx_key key;
3003 flags = fuword32(&sem->_flags);
3004 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3008 cnt = umtxq_count(&key);
3010 umtxq_signal(&key, 1);
3012 * Check if count is greater than 0, this means the memory is
3013 * still being referenced by user code, so we can safely
3014 * update _has_waiters flag.
3019 __DEVOLATILE(uint32_t *, &sem->_has_waiters), 0);
3025 umtx_key_release(&key);
3030 sys__umtx_lock(struct thread *td, struct _umtx_lock_args *uap)
3031 /* struct umtx *umtx */
3033 return do_lock_umtx(td, uap->umtx, td->td_tid, 0);
3037 sys__umtx_unlock(struct thread *td, struct _umtx_unlock_args *uap)
3038 /* struct umtx *umtx */
3040 return do_unlock_umtx(td, uap->umtx, td->td_tid);
3044 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3048 error = copyin(addr, tsp, sizeof(struct timespec));
3050 if (tsp->tv_sec < 0 ||
3051 tsp->tv_nsec >= 1000000000 ||
3059 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3063 if (size <= sizeof(struct timespec)) {
3064 tp->_clockid = CLOCK_REALTIME;
3066 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3068 error = copyin(addr, tp, sizeof(struct _umtx_time));
3071 if (tp->_timeout.tv_sec < 0 ||
3072 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3078 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap)
3080 struct timespec *ts, timeout;
3083 /* Allow a null timespec (wait forever). */
3084 if (uap->uaddr2 == NULL)
3087 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3092 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3096 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap)
3098 return (do_unlock_umtx(td, uap->obj, uap->val));
3102 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3104 struct _umtx_time timeout, *tm_p;
3107 if (uap->uaddr2 == NULL)
3110 error = umtx_copyin_umtx_time(
3111 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3116 return do_wait(td, uap->obj, uap->val, tm_p, 0, 0);
3120 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3122 struct _umtx_time timeout, *tm_p;
3125 if (uap->uaddr2 == NULL)
3128 error = umtx_copyin_umtx_time(
3129 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3134 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3138 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3140 struct _umtx_time *tm_p, timeout;
3143 if (uap->uaddr2 == NULL)
3146 error = umtx_copyin_umtx_time(
3147 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3152 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3156 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3158 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3161 #define BATCH_SIZE 128
3163 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3165 int count = uap->val;
3166 void *uaddrs[BATCH_SIZE];
3167 char **upp = (char **)uap->obj;
3174 if (tocopy > BATCH_SIZE)
3175 tocopy = BATCH_SIZE;
3176 error = copyin(upp+pos, uaddrs, tocopy * sizeof(char *));
3179 for (i = 0; i < tocopy; ++i)
3180 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3188 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3190 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3194 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3196 struct _umtx_time *tm_p, timeout;
3199 /* Allow a null timespec (wait forever). */
3200 if (uap->uaddr2 == NULL)
3203 error = umtx_copyin_umtx_time(
3204 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3209 return do_lock_umutex(td, uap->obj, tm_p, 0);
3213 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3215 return do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY);
3219 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3221 struct _umtx_time *tm_p, timeout;
3224 /* Allow a null timespec (wait forever). */
3225 if (uap->uaddr2 == NULL)
3228 error = umtx_copyin_umtx_time(
3229 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3234 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3238 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3240 return do_wake_umutex(td, uap->obj);
3244 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3246 return do_unlock_umutex(td, uap->obj);
3250 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3252 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1);
3256 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3258 struct timespec *ts, timeout;
3261 /* Allow a null timespec (wait forever). */
3262 if (uap->uaddr2 == NULL)
3265 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3270 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3274 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3276 return do_cv_signal(td, uap->obj);
3280 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3282 return do_cv_broadcast(td, uap->obj);
3286 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3288 struct _umtx_time timeout;
3291 /* Allow a null timespec (wait forever). */
3292 if (uap->uaddr2 == NULL) {
3293 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3295 error = umtx_copyin_umtx_time(uap->uaddr2,
3296 (size_t)uap->uaddr1, &timeout);
3299 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3305 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3307 struct _umtx_time timeout;
3310 /* Allow a null timespec (wait forever). */
3311 if (uap->uaddr2 == NULL) {
3312 error = do_rw_wrlock(td, uap->obj, 0);
3314 error = umtx_copyin_umtx_time(uap->uaddr2,
3315 (size_t)uap->uaddr1, &timeout);
3319 error = do_rw_wrlock(td, uap->obj, &timeout);
3325 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3327 return do_rw_unlock(td, uap->obj);
3331 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3333 struct _umtx_time *tm_p, timeout;
3336 /* Allow a null timespec (wait forever). */
3337 if (uap->uaddr2 == NULL)
3340 error = umtx_copyin_umtx_time(
3341 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3346 return (do_sem_wait(td, uap->obj, tm_p));
3350 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3352 return do_sem_wake(td, uap->obj);
3356 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3358 return do_wake2_umutex(td, uap->obj, uap->val);
3361 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3363 static _umtx_op_func op_table[] = {
3364 __umtx_op_lock_umtx, /* UMTX_OP_LOCK */
3365 __umtx_op_unlock_umtx, /* UMTX_OP_UNLOCK */
3366 __umtx_op_wait, /* UMTX_OP_WAIT */
3367 __umtx_op_wake, /* UMTX_OP_WAKE */
3368 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_TRYLOCK */
3369 __umtx_op_lock_umutex, /* UMTX_OP_MUTEX_LOCK */
3370 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3371 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3372 __umtx_op_cv_wait, /* UMTX_OP_CV_WAIT*/
3373 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3374 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3375 __umtx_op_wait_uint, /* UMTX_OP_WAIT_UINT */
3376 __umtx_op_rw_rdlock, /* UMTX_OP_RW_RDLOCK */
3377 __umtx_op_rw_wrlock, /* UMTX_OP_RW_WRLOCK */
3378 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3379 __umtx_op_wait_uint_private, /* UMTX_OP_WAIT_UINT_PRIVATE */
3380 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3381 __umtx_op_wait_umutex, /* UMTX_OP_UMUTEX_WAIT */
3382 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
3383 __umtx_op_sem_wait, /* UMTX_OP_SEM_WAIT */
3384 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3385 __umtx_op_nwake_private, /* UMTX_OP_NWAKE_PRIVATE */
3386 __umtx_op_wake2_umutex /* UMTX_OP_UMUTEX_WAKE2 */
3390 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
3392 if ((unsigned)uap->op < UMTX_OP_MAX)
3393 return (*op_table[uap->op])(td, uap);
3397 #ifdef COMPAT_FREEBSD32
3399 freebsd32_umtx_lock(struct thread *td, struct freebsd32_umtx_lock_args *uap)
3400 /* struct umtx *umtx */
3402 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
3406 freebsd32_umtx_unlock(struct thread *td, struct freebsd32_umtx_unlock_args *uap)
3407 /* struct umtx *umtx */
3409 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
3417 struct umtx_time32 {
3418 struct timespec32 timeout;
3424 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
3426 struct timespec32 ts32;
3429 error = copyin(addr, &ts32, sizeof(struct timespec32));
3431 if (ts32.tv_sec < 0 ||
3432 ts32.tv_nsec >= 1000000000 ||
3436 tsp->tv_sec = ts32.tv_sec;
3437 tsp->tv_nsec = ts32.tv_nsec;
3444 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
3446 struct umtx_time32 t32;
3449 t32.clockid = CLOCK_REALTIME;
3451 if (size <= sizeof(struct timespec32))
3452 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
3454 error = copyin(addr, &t32, sizeof(struct umtx_time32));
3457 if (t32.timeout.tv_sec < 0 ||
3458 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
3460 tp->_timeout.tv_sec = t32.timeout.tv_sec;
3461 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
3462 tp->_flags = t32.flags;
3463 tp->_clockid = t32.clockid;
3468 __umtx_op_lock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
3470 struct timespec *ts, timeout;
3473 /* Allow a null timespec (wait forever). */
3474 if (uap->uaddr2 == NULL)
3477 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3482 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3486 __umtx_op_unlock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
3488 return (do_unlock_umtx32(td, uap->obj, (uint32_t)uap->val));
3492 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3494 struct _umtx_time *tm_p, timeout;
3497 if (uap->uaddr2 == NULL)
3500 error = umtx_copyin_umtx_time32(uap->uaddr2,
3501 (size_t)uap->uaddr1, &timeout);
3506 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3510 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3512 struct _umtx_time *tm_p, timeout;
3515 /* Allow a null timespec (wait forever). */
3516 if (uap->uaddr2 == NULL)
3519 error = umtx_copyin_umtx_time(uap->uaddr2,
3520 (size_t)uap->uaddr1, &timeout);
3525 return do_lock_umutex(td, uap->obj, tm_p, 0);
3529 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3531 struct _umtx_time *tm_p, timeout;
3534 /* Allow a null timespec (wait forever). */
3535 if (uap->uaddr2 == NULL)
3538 error = umtx_copyin_umtx_time32(uap->uaddr2,
3539 (size_t)uap->uaddr1, &timeout);
3544 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3548 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3550 struct timespec *ts, timeout;
3553 /* Allow a null timespec (wait forever). */
3554 if (uap->uaddr2 == NULL)
3557 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3562 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3566 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3568 struct _umtx_time timeout;
3571 /* Allow a null timespec (wait forever). */
3572 if (uap->uaddr2 == NULL) {
3573 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3575 error = umtx_copyin_umtx_time32(uap->uaddr2,
3576 (size_t)uap->uaddr1, &timeout);
3579 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3585 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3587 struct _umtx_time timeout;
3590 /* Allow a null timespec (wait forever). */
3591 if (uap->uaddr2 == NULL) {
3592 error = do_rw_wrlock(td, uap->obj, 0);
3594 error = umtx_copyin_umtx_time32(uap->uaddr2,
3595 (size_t)uap->uaddr1, &timeout);
3598 error = do_rw_wrlock(td, uap->obj, &timeout);
3604 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3606 struct _umtx_time *tm_p, timeout;
3609 if (uap->uaddr2 == NULL)
3612 error = umtx_copyin_umtx_time32(
3613 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
3618 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3622 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3624 struct _umtx_time *tm_p, timeout;
3627 /* Allow a null timespec (wait forever). */
3628 if (uap->uaddr2 == NULL)
3631 error = umtx_copyin_umtx_time32(uap->uaddr2,
3632 (size_t)uap->uaddr1, &timeout);
3637 return (do_sem_wait(td, uap->obj, tm_p));
3641 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
3643 int count = uap->val;
3644 uint32_t uaddrs[BATCH_SIZE];
3645 uint32_t **upp = (uint32_t **)uap->obj;
3652 if (tocopy > BATCH_SIZE)
3653 tocopy = BATCH_SIZE;
3654 error = copyin(upp+pos, uaddrs, tocopy * sizeof(uint32_t));
3657 for (i = 0; i < tocopy; ++i)
3658 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
3666 static _umtx_op_func op_table_compat32[] = {
3667 __umtx_op_lock_umtx_compat32, /* UMTX_OP_LOCK */
3668 __umtx_op_unlock_umtx_compat32, /* UMTX_OP_UNLOCK */
3669 __umtx_op_wait_compat32, /* UMTX_OP_WAIT */
3670 __umtx_op_wake, /* UMTX_OP_WAKE */
3671 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_LOCK */
3672 __umtx_op_lock_umutex_compat32, /* UMTX_OP_MUTEX_TRYLOCK */
3673 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3674 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3675 __umtx_op_cv_wait_compat32, /* UMTX_OP_CV_WAIT*/
3676 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3677 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3678 __umtx_op_wait_compat32, /* UMTX_OP_WAIT_UINT */
3679 __umtx_op_rw_rdlock_compat32, /* UMTX_OP_RW_RDLOCK */
3680 __umtx_op_rw_wrlock_compat32, /* UMTX_OP_RW_WRLOCK */
3681 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3682 __umtx_op_wait_uint_private_compat32, /* UMTX_OP_WAIT_UINT_PRIVATE */
3683 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3684 __umtx_op_wait_umutex_compat32, /* UMTX_OP_UMUTEX_WAIT */
3685 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
3686 __umtx_op_sem_wait_compat32, /* UMTX_OP_SEM_WAIT */
3687 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3688 __umtx_op_nwake_private32, /* UMTX_OP_NWAKE_PRIVATE */
3689 __umtx_op_wake2_umutex /* UMTX_OP_UMUTEX_WAKE2 */
3693 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
3695 if ((unsigned)uap->op < UMTX_OP_MAX)
3696 return (*op_table_compat32[uap->op])(td,
3697 (struct _umtx_op_args *)uap);
3703 umtx_thread_init(struct thread *td)
3705 td->td_umtxq = umtxq_alloc();
3706 td->td_umtxq->uq_thread = td;
3710 umtx_thread_fini(struct thread *td)
3712 umtxq_free(td->td_umtxq);
3716 * It will be called when new thread is created, e.g fork().
3719 umtx_thread_alloc(struct thread *td)
3724 uq->uq_inherited_pri = PRI_MAX;
3726 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
3727 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
3728 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
3729 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
3736 umtx_exec_hook(void *arg __unused, struct proc *p __unused,
3737 struct image_params *imgp __unused)
3739 umtx_thread_cleanup(curthread);
3743 * thread_exit() hook.
3746 umtx_thread_exit(struct thread *td)
3748 umtx_thread_cleanup(td);
3752 * clean up umtx data.
3755 umtx_thread_cleanup(struct thread *td)
3760 if ((uq = td->td_umtxq) == NULL)
3763 mtx_lock_spin(&umtx_lock);
3764 uq->uq_inherited_pri = PRI_MAX;
3765 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
3766 pi->pi_owner = NULL;
3767 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
3769 mtx_unlock_spin(&umtx_lock);
3771 sched_lend_user_prio(td, PRI_MAX);