turnstile.h revision 262192
1333019Ssbruno/*- 2333019Ssbruno * Copyright (c) 2002 John Baldwin <jhb@FreeBSD.org> 3333019Ssbruno * All rights reserved. 4333019Ssbruno * 5333019Ssbruno * Redistribution and use in source and binary forms, with or without 6333019Ssbruno * modification, are permitted provided that the following conditions 7333019Ssbruno * are met: 8333019Ssbruno * 1. Redistributions of source code must retain the above copyright 9333019Ssbruno * notice, this list of conditions and the following disclaimer. 10333019Ssbruno * 2. Redistributions in binary form must reproduce the above copyright 11333019Ssbruno * notice, this list of conditions and the following disclaimer in the 12333019Ssbruno * documentation and/or other materials provided with the distribution. 13333019Ssbruno * 14333019Ssbruno * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15333019Ssbruno * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16333019Ssbruno * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17333019Ssbruno * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18333019Ssbruno * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19333019Ssbruno * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20333019Ssbruno * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21333019Ssbruno * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22333019Ssbruno * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23333019Ssbruno * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24333019Ssbruno * SUCH DAMAGE. 25333019Ssbruno * 26333019Ssbruno * $FreeBSD: stable/10/sys/sys/turnstile.h 262192 2014-02-18 20:27:17Z jhb $ 27333019Ssbruno */ 28333019Ssbruno 29333019Ssbruno#ifndef _SYS_TURNSTILE_H_ 30333019Ssbruno#define _SYS_TURNSTILE_H_ 31333019Ssbruno 32333019Ssbruno/* 33333019Ssbruno * Turnstile interface. Non-sleepable locks use a turnstile for the 34333019Ssbruno * queue of threads blocked on them when they are contested. Each 35333019Ssbruno * turnstile contains two sub-queues: one for threads waiting for a 36333019Ssbruno * shared, or eread, lock, and one for threads waiting for an 37333019Ssbruno * exclusive, or write, lock. 38333019Ssbruno * 39333019Ssbruno * A thread calls turnstile_chain_lock() to lock the turnstile chain 40333019Ssbruno * associated with a given lock. A thread calls turnstile_wait() when 41333019Ssbruno * the lock is contested to be put on the queue and block. If a thread 42333019Ssbruno * calls turnstile_trywait() and decides to retry a lock operation instead 43333019Ssbruno * of blocking, it should call turnstile_cancel() to unlock the associated 44333019Ssbruno * turnstile chain lock. 45333019Ssbruno * 46333019Ssbruno * When a lock is released, the thread calls turnstile_lookup() to look 47333019Ssbruno * up the turnstile associated with the given lock in the hash table. Then 48333019Ssbruno * it calls either turnstile_signal() or turnstile_broadcast() to mark 49333019Ssbruno * blocked threads for a pending wakeup. turnstile_signal() marks the 50333019Ssbruno * highest priority blocked thread while turnstile_broadcast() marks all 51333019Ssbruno * blocked threads. The turnstile_signal() function returns true if the 52333019Ssbruno * turnstile became empty as a result. After the higher level code finishes 53333019Ssbruno * releasing the lock, turnstile_unpend() must be called to wake up the 54333019Ssbruno * pending thread(s) and give up ownership of the turnstile. 55333019Ssbruno * 56333019Ssbruno * Alternatively, if a thread wishes to relinquish ownership of a lock 57333019Ssbruno * without waking up any waiters, it may call turnstile_disown(). 58333019Ssbruno * 59333019Ssbruno * When a lock is acquired that already has at least one thread contested 60333019Ssbruno * on it, the new owner of the lock must claim ownership of the turnstile 61333019Ssbruno * via turnstile_claim(). 62333019Ssbruno * 63333019Ssbruno * Each thread allocates a turnstile at thread creation via turnstile_alloc() 64333019Ssbruno * and releases it at thread destruction via turnstile_free(). Note that 65333019Ssbruno * a turnstile is not tied to a specific thread and that the turnstile 66333019Ssbruno * released at thread destruction may not be the same turnstile that the 67333019Ssbruno * thread allocated when it was created. 68333019Ssbruno * 69333019Ssbruno * The highest priority thread blocked on a specified queue of a 70333019Ssbruno * turnstile can be obtained via turnstile_head(). A given queue can 71333019Ssbruno * also be queried to see if it is empty via turnstile_empty(). 72333019Ssbruno */ 73333019Ssbruno 74333019Ssbrunostruct lock_object; 75333019Ssbrunostruct thread; 76333019Ssbrunostruct turnstile; 77333019Ssbruno 78333019Ssbruno#ifdef _KERNEL 79333019Ssbruno 80333019Ssbruno/* Which queue to block on or which queue to wakeup one or more threads from. */ 81333019Ssbruno#define TS_EXCLUSIVE_QUEUE 0 82333019Ssbruno#define TS_SHARED_QUEUE 1 83333019Ssbruno 84333019Ssbruno/* The type of lock currently held. */ 85333019Ssbruno#define TS_EXCLUSIVE_LOCK TS_EXCLUSIVE_QUEUE 86333019Ssbruno#define TS_SHARED_LOCK TS_SHARED_QUEUE 87333019Ssbruno 88333019Ssbrunovoid init_turnstiles(void); 89333019Ssbrunovoid turnstile_adjust(struct thread *, u_char); 90333019Ssbrunostruct turnstile *turnstile_alloc(void); 91333019Ssbrunovoid turnstile_broadcast(struct turnstile *, int); 92333019Ssbrunovoid turnstile_cancel(struct turnstile *); 93333019Ssbrunovoid turnstile_chain_lock(struct lock_object *); 94333019Ssbrunovoid turnstile_chain_unlock(struct lock_object *); 95333019Ssbrunovoid turnstile_claim(struct turnstile *); 96333019Ssbrunovoid turnstile_disown(struct turnstile *); 97333019Ssbrunoint turnstile_empty(struct turnstile *ts, int queue); 98333019Ssbrunovoid turnstile_free(struct turnstile *); 99333019Ssbrunostruct thread *turnstile_head(struct turnstile *, int); 100333019Ssbrunostruct turnstile *turnstile_lookup(struct lock_object *); 101333019Ssbrunoint turnstile_signal(struct turnstile *, int); 102333019Ssbrunostruct turnstile *turnstile_trywait(struct lock_object *); 103333019Ssbrunovoid turnstile_unpend(struct turnstile *, int); 104333019Ssbrunovoid turnstile_wait(struct turnstile *, struct thread *, int); 105333019Ssbruno 106333019Ssbruno#endif /* _KERNEL */ 107333019Ssbruno#endif /* _SYS_TURNSTILE_H_ */ 108333019Ssbruno