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