1139825Simp/*- 2122516Sjhb * Copyright (c) 2002 John Baldwin <jhb@FreeBSD.org> 3122516Sjhb * 4122516Sjhb * Redistribution and use in source and binary forms, with or without 5122516Sjhb * modification, are permitted provided that the following conditions 6122516Sjhb * are met: 7122516Sjhb * 1. Redistributions of source code must retain the above copyright 8122516Sjhb * notice, this list of conditions and the following disclaimer. 9122516Sjhb * 2. Redistributions in binary form must reproduce the above copyright 10122516Sjhb * notice, this list of conditions and the following disclaimer in the 11122516Sjhb * documentation and/or other materials provided with the distribution. 12122516Sjhb * 13122516Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14122516Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15122516Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16122516Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17122516Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18122516Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19122516Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20122516Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21122516Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22122516Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23122516Sjhb * SUCH DAMAGE. 24122516Sjhb * 25122516Sjhb * $FreeBSD: stable/11/sys/sys/turnstile.h 367457 2020-11-07 18:10:59Z dim $ 26122516Sjhb */ 27122516Sjhb 28122516Sjhb#ifndef _SYS_TURNSTILE_H_ 29122516Sjhb#define _SYS_TURNSTILE_H_ 30122516Sjhb 31122516Sjhb/* 32122516Sjhb * Turnstile interface. Non-sleepable locks use a turnstile for the 33157845Sjhb * queue of threads blocked on them when they are contested. Each 34157845Sjhb * turnstile contains two sub-queues: one for threads waiting for a 35299519Skib * shared, or read, lock, and one for threads waiting for an 36157845Sjhb * exclusive, or write, lock. 37122516Sjhb * 38216421Smckusick * A thread calls turnstile_chain_lock() to lock the turnstile chain 39216421Smckusick * associated with a given lock. A thread calls turnstile_wait() when 40216421Smckusick * the lock is contested to be put on the queue and block. If a thread 41216421Smckusick * calls turnstile_trywait() and decides to retry a lock operation instead 42216421Smckusick * of blocking, it should call turnstile_cancel() to unlock the associated 43216421Smckusick * turnstile chain lock. 44122516Sjhb * 45216421Smckusick * When a lock is released, the thread calls turnstile_lookup() to look 46136445Sjhb * up the turnstile associated with the given lock in the hash table. Then 47136445Sjhb * it calls either turnstile_signal() or turnstile_broadcast() to mark 48136445Sjhb * blocked threads for a pending wakeup. turnstile_signal() marks the 49136445Sjhb * highest priority blocked thread while turnstile_broadcast() marks all 50136445Sjhb * blocked threads. The turnstile_signal() function returns true if the 51136445Sjhb * turnstile became empty as a result. After the higher level code finishes 52136445Sjhb * releasing the lock, turnstile_unpend() must be called to wake up the 53157845Sjhb * pending thread(s) and give up ownership of the turnstile. 54122516Sjhb * 55216421Smckusick * Alternatively, if a thread wishes to relinquish ownership of a lock 56157845Sjhb * without waking up any waiters, it may call turnstile_disown(). 57157845Sjhb * 58122516Sjhb * When a lock is acquired that already has at least one thread contested 59122516Sjhb * on it, the new owner of the lock must claim ownership of the turnstile 60122516Sjhb * via turnstile_claim(). 61122516Sjhb * 62126317Sjhb * Each thread allocates a turnstile at thread creation via turnstile_alloc() 63126317Sjhb * and releases it at thread destruction via turnstile_free(). Note that 64126317Sjhb * a turnstile is not tied to a specific thread and that the turnstile 65126317Sjhb * released at thread destruction may not be the same turnstile that the 66126317Sjhb * thread allocated when it was created. 67122516Sjhb * 68157845Sjhb * The highest priority thread blocked on a specified queue of a 69157845Sjhb * turnstile can be obtained via turnstile_head(). A given queue can 70157845Sjhb * also be queried to see if it is empty via turnstile_empty(). 71122516Sjhb */ 72122516Sjhb 73122516Sjhbstruct lock_object; 74122516Sjhbstruct thread; 75122516Sjhbstruct turnstile; 76122516Sjhb 77122516Sjhb#ifdef _KERNEL 78122516Sjhb 79154937Sjhb/* Which queue to block on or which queue to wakeup one or more threads from. */ 80154937Sjhb#define TS_EXCLUSIVE_QUEUE 0 81154937Sjhb#define TS_SHARED_QUEUE 1 82154937Sjhb 83154937Sjhb/* The type of lock currently held. */ 84154937Sjhb#define TS_EXCLUSIVE_LOCK TS_EXCLUSIVE_QUEUE 85154937Sjhb#define TS_SHARED_LOCK TS_SHARED_QUEUE 86154937Sjhb 87122516Sjhbvoid init_turnstiles(void); 88139453Sjhbvoid turnstile_adjust(struct thread *, u_char); 89122516Sjhbstruct turnstile *turnstile_alloc(void); 90154937Sjhbvoid turnstile_broadcast(struct turnstile *, int); 91170295Sjeffvoid turnstile_cancel(struct turnstile *); 92170295Sjeffvoid turnstile_chain_lock(struct lock_object *); 93170295Sjeffvoid turnstile_chain_unlock(struct lock_object *); 94170295Sjeffvoid turnstile_claim(struct turnstile *); 95157844Sjhbvoid turnstile_disown(struct turnstile *); 96157844Sjhbint turnstile_empty(struct turnstile *ts, int queue); 97122516Sjhbvoid turnstile_free(struct turnstile *); 98154937Sjhbstruct thread *turnstile_head(struct turnstile *, int); 99122516Sjhbstruct turnstile *turnstile_lookup(struct lock_object *); 100154937Sjhbint turnstile_signal(struct turnstile *, int); 101170295Sjeffstruct turnstile *turnstile_trywait(struct lock_object *); 102154937Sjhbvoid turnstile_unpend(struct turnstile *, int); 103170295Sjeffvoid turnstile_wait(struct turnstile *, struct thread *, int); 104122516Sjhb 105122516Sjhb#endif /* _KERNEL */ 106122516Sjhb#endif /* _SYS_TURNSTILE_H_ */ 107