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 * 3. Neither the name of the author nor the names of any co-contributors 14122516Sjhb * may be used to endorse or promote products derived from this software 15122516Sjhb * without specific prior written permission. 16122516Sjhb * 17122516Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18122516Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19122516Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20122516Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21122516Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22122516Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23122516Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24122516Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25122516Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26122516Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27122516Sjhb * SUCH DAMAGE. 28122516Sjhb * 29122516Sjhb * $FreeBSD$ 30122516Sjhb */ 31122516Sjhb 32122516Sjhb#ifndef _SYS_TURNSTILE_H_ 33122516Sjhb#define _SYS_TURNSTILE_H_ 34122516Sjhb 35122516Sjhb/* 36122516Sjhb * Turnstile interface. Non-sleepable locks use a turnstile for the 37157845Sjhb * queue of threads blocked on them when they are contested. Each 38157845Sjhb * turnstile contains two sub-queues: one for threads waiting for a 39157845Sjhb * shared, or eread, lock, and one for threads waiting for an 40157845Sjhb * exclusive, or write, lock. 41122516Sjhb * 42216421Smckusick * A thread calls turnstile_chain_lock() to lock the turnstile chain 43216421Smckusick * associated with a given lock. A thread calls turnstile_wait() when 44216421Smckusick * the lock is contested to be put on the queue and block. If a thread 45216421Smckusick * calls turnstile_trywait() and decides to retry a lock operation instead 46216421Smckusick * of blocking, it should call turnstile_cancel() to unlock the associated 47216421Smckusick * turnstile chain lock. 48122516Sjhb * 49216421Smckusick * When a lock is released, the thread calls turnstile_lookup() to look 50136445Sjhb * up the turnstile associated with the given lock in the hash table. Then 51136445Sjhb * it calls either turnstile_signal() or turnstile_broadcast() to mark 52136445Sjhb * blocked threads for a pending wakeup. turnstile_signal() marks the 53136445Sjhb * highest priority blocked thread while turnstile_broadcast() marks all 54136445Sjhb * blocked threads. The turnstile_signal() function returns true if the 55136445Sjhb * turnstile became empty as a result. After the higher level code finishes 56136445Sjhb * releasing the lock, turnstile_unpend() must be called to wake up the 57157845Sjhb * pending thread(s) and give up ownership of the turnstile. 58122516Sjhb * 59216421Smckusick * Alternatively, if a thread wishes to relinquish ownership of a lock 60157845Sjhb * without waking up any waiters, it may call turnstile_disown(). 61157845Sjhb * 62122516Sjhb * When a lock is acquired that already has at least one thread contested 63122516Sjhb * on it, the new owner of the lock must claim ownership of the turnstile 64122516Sjhb * via turnstile_claim(). 65122516Sjhb * 66126317Sjhb * Each thread allocates a turnstile at thread creation via turnstile_alloc() 67126317Sjhb * and releases it at thread destruction via turnstile_free(). Note that 68126317Sjhb * a turnstile is not tied to a specific thread and that the turnstile 69126317Sjhb * released at thread destruction may not be the same turnstile that the 70126317Sjhb * thread allocated when it was created. 71122516Sjhb * 72157845Sjhb * The highest priority thread blocked on a specified queue of a 73157845Sjhb * turnstile can be obtained via turnstile_head(). A given queue can 74157845Sjhb * also be queried to see if it is empty via turnstile_empty(). 75122516Sjhb */ 76122516Sjhb 77122516Sjhbstruct lock_object; 78122516Sjhbstruct thread; 79122516Sjhbstruct turnstile; 80122516Sjhb 81122516Sjhb#ifdef _KERNEL 82122516Sjhb 83154937Sjhb/* Which queue to block on or which queue to wakeup one or more threads from. */ 84154937Sjhb#define TS_EXCLUSIVE_QUEUE 0 85154937Sjhb#define TS_SHARED_QUEUE 1 86154937Sjhb 87154937Sjhb/* The type of lock currently held. */ 88154937Sjhb#define TS_EXCLUSIVE_LOCK TS_EXCLUSIVE_QUEUE 89154937Sjhb#define TS_SHARED_LOCK TS_SHARED_QUEUE 90154937Sjhb 91122516Sjhbvoid init_turnstiles(void); 92139453Sjhbvoid turnstile_adjust(struct thread *, u_char); 93122516Sjhbstruct turnstile *turnstile_alloc(void); 94154937Sjhbvoid turnstile_broadcast(struct turnstile *, int); 95170295Sjeffvoid turnstile_cancel(struct turnstile *); 96170295Sjeffvoid turnstile_chain_lock(struct lock_object *); 97170295Sjeffvoid turnstile_chain_unlock(struct lock_object *); 98170295Sjeffvoid turnstile_claim(struct turnstile *); 99157844Sjhbvoid turnstile_disown(struct turnstile *); 100157844Sjhbint turnstile_empty(struct turnstile *ts, int queue); 101122516Sjhbvoid turnstile_free(struct turnstile *); 102154937Sjhbstruct thread *turnstile_head(struct turnstile *, int); 103122516Sjhbstruct turnstile *turnstile_lookup(struct lock_object *); 104154937Sjhbint turnstile_signal(struct turnstile *, int); 105170295Sjeffstruct turnstile *turnstile_trywait(struct lock_object *); 106154937Sjhbvoid turnstile_unpend(struct turnstile *, int); 107170295Sjeffvoid turnstile_wait(struct turnstile *, struct thread *, int); 108122516Sjhb 109122516Sjhb#endif /* _KERNEL */ 110122516Sjhb#endif /* _SYS_TURNSTILE_H_ */ 111