thr_sleepq.c revision 235218
1216642Sdavidxu/* 2216642Sdavidxu * Copyright (c) 2010 David Xu <davidxu@freebsd.org> 3216642Sdavidxu * All rights reserved. 4216642Sdavidxu * 5216642Sdavidxu * Redistribution and use in source and binary forms, with or without 6216642Sdavidxu * modification, are permitted provided that the following conditions 7216642Sdavidxu * are met: 8216642Sdavidxu * 1. Redistributions of source code must retain the above copyright 9216642Sdavidxu * notice unmodified, this list of conditions, and the following 10216642Sdavidxu * disclaimer. 11216642Sdavidxu * 2. Redistributions in binary form must reproduce the above copyright 12216642Sdavidxu * notice, this list of conditions and the following disclaimer in the 13216642Sdavidxu * documentation and/or other materials provided with the distribution. 14216642Sdavidxu * 15216642Sdavidxu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16216642Sdavidxu * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17216642Sdavidxu * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18216642Sdavidxu * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19216642Sdavidxu * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20216642Sdavidxu * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21216642Sdavidxu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22216642Sdavidxu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23216642Sdavidxu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24216642Sdavidxu * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25216642Sdavidxu * 26216642Sdavidxu * $FreeBSD: head/lib/libthr/thread/thr_sleepq.c 235218 2012-05-10 09:30:37Z davidxu $ 27216642Sdavidxu */ 28216642Sdavidxu 29216642Sdavidxu#include <stdlib.h> 30216642Sdavidxu#include "thr_private.h" 31216642Sdavidxu 32216642Sdavidxu#define HASHSHIFT 9 33216642Sdavidxu#define HASHSIZE (1 << HASHSHIFT) 34216642Sdavidxu#define SC_HASH(wchan) ((unsigned) \ 35216642Sdavidxu ((((uintptr_t)(wchan) >> 3) \ 36216642Sdavidxu ^ ((uintptr_t)(wchan) >> (HASHSHIFT + 3))) \ 37216642Sdavidxu & (HASHSIZE - 1))) 38216642Sdavidxu#define SC_LOOKUP(wc) &sc_table[SC_HASH(wc)] 39216642Sdavidxu 40216642Sdavidxustruct sleepqueue_chain { 41216642Sdavidxu struct umutex sc_lock; 42234947Sdavidxu int sc_enqcnt; 43216642Sdavidxu LIST_HEAD(, sleepqueue) sc_queues; 44216642Sdavidxu int sc_type; 45216642Sdavidxu}; 46216642Sdavidxu 47216642Sdavidxustatic struct sleepqueue_chain sc_table[HASHSIZE]; 48216642Sdavidxu 49216642Sdavidxuvoid 50216642Sdavidxu_sleepq_init(void) 51216642Sdavidxu{ 52216642Sdavidxu int i; 53216642Sdavidxu 54216642Sdavidxu for (i = 0; i < HASHSIZE; ++i) { 55216642Sdavidxu LIST_INIT(&sc_table[i].sc_queues); 56216642Sdavidxu _thr_umutex_init(&sc_table[i].sc_lock); 57216642Sdavidxu } 58216642Sdavidxu} 59216642Sdavidxu 60216642Sdavidxustruct sleepqueue * 61216642Sdavidxu_sleepq_alloc(void) 62216642Sdavidxu{ 63216642Sdavidxu struct sleepqueue *sq; 64216642Sdavidxu 65216642Sdavidxu sq = calloc(1, sizeof(struct sleepqueue)); 66216642Sdavidxu TAILQ_INIT(&sq->sq_blocked); 67216642Sdavidxu SLIST_INIT(&sq->sq_freeq); 68216642Sdavidxu return (sq); 69216642Sdavidxu} 70216642Sdavidxu 71216642Sdavidxuvoid 72216642Sdavidxu_sleepq_free(struct sleepqueue *sq) 73216642Sdavidxu{ 74216642Sdavidxu free(sq); 75216642Sdavidxu} 76216642Sdavidxu 77216642Sdavidxuvoid 78216642Sdavidxu_sleepq_lock(void *wchan) 79216642Sdavidxu{ 80216642Sdavidxu struct pthread *curthread = _get_curthread(); 81216642Sdavidxu struct sleepqueue_chain *sc; 82216642Sdavidxu 83216642Sdavidxu sc = SC_LOOKUP(wchan); 84216642Sdavidxu THR_LOCK_ACQUIRE_SPIN(curthread, &sc->sc_lock); 85216642Sdavidxu} 86216642Sdavidxu 87216642Sdavidxuvoid 88216642Sdavidxu_sleepq_unlock(void *wchan) 89216642Sdavidxu{ 90216642Sdavidxu struct sleepqueue_chain *sc; 91216642Sdavidxu struct pthread *curthread = _get_curthread(); 92216642Sdavidxu 93216642Sdavidxu sc = SC_LOOKUP(wchan); 94216642Sdavidxu THR_LOCK_RELEASE(curthread, &sc->sc_lock); 95216642Sdavidxu} 96216642Sdavidxu 97235218Sdavidxustatic inline struct sleepqueue * 98235218Sdavidxulookup(struct sleepqueue_chain *sc, void *wchan) 99216642Sdavidxu{ 100216642Sdavidxu struct sleepqueue *sq; 101216642Sdavidxu 102216642Sdavidxu LIST_FOREACH(sq, &sc->sc_queues, sq_hash) 103216642Sdavidxu if (sq->sq_wchan == wchan) 104216642Sdavidxu return (sq); 105216642Sdavidxu return (NULL); 106216642Sdavidxu} 107216642Sdavidxu 108235218Sdavidxustruct sleepqueue * 109235218Sdavidxu_sleepq_lookup(void *wchan) 110235218Sdavidxu{ 111235218Sdavidxu return (lookup(SC_LOOKUP(wchan), wchan)); 112235218Sdavidxu} 113235218Sdavidxu 114216642Sdavidxuvoid 115216642Sdavidxu_sleepq_add(void *wchan, struct pthread *td) 116216642Sdavidxu{ 117216642Sdavidxu struct sleepqueue_chain *sc; 118216642Sdavidxu struct sleepqueue *sq; 119216642Sdavidxu 120235068Sdavidxu sc = SC_LOOKUP(wchan); 121235218Sdavidxu sq = lookup(sc, wchan); 122216642Sdavidxu if (sq != NULL) { 123216642Sdavidxu SLIST_INSERT_HEAD(&sq->sq_freeq, td->sleepqueue, sq_flink); 124216642Sdavidxu } else { 125216642Sdavidxu sq = td->sleepqueue; 126216642Sdavidxu LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash); 127216642Sdavidxu sq->sq_wchan = wchan; 128216642Sdavidxu /* sq->sq_type = type; */ 129216642Sdavidxu } 130216642Sdavidxu td->sleepqueue = NULL; 131216642Sdavidxu td->wchan = wchan; 132234947Sdavidxu if (((++sc->sc_enqcnt << _thr_queuefifo) & 0xff) != 0) 133234947Sdavidxu TAILQ_INSERT_HEAD(&sq->sq_blocked, td, wle); 134234947Sdavidxu else 135234947Sdavidxu TAILQ_INSERT_TAIL(&sq->sq_blocked, td, wle); 136216642Sdavidxu} 137216642Sdavidxu 138216642Sdavidxuint 139216642Sdavidxu_sleepq_remove(struct sleepqueue *sq, struct pthread *td) 140216642Sdavidxu{ 141216642Sdavidxu int rc; 142216642Sdavidxu 143216642Sdavidxu TAILQ_REMOVE(&sq->sq_blocked, td, wle); 144216642Sdavidxu if (TAILQ_EMPTY(&sq->sq_blocked)) { 145216642Sdavidxu LIST_REMOVE(sq, sq_hash); 146216642Sdavidxu td->sleepqueue = sq; 147216642Sdavidxu rc = 0; 148216642Sdavidxu } else { 149216642Sdavidxu td->sleepqueue = SLIST_FIRST(&sq->sq_freeq); 150216642Sdavidxu SLIST_REMOVE_HEAD(&sq->sq_freeq, sq_flink); 151216642Sdavidxu rc = 1; 152216642Sdavidxu } 153216642Sdavidxu td->wchan = NULL; 154216642Sdavidxu return (rc); 155216642Sdavidxu} 156216642Sdavidxu 157216642Sdavidxuvoid 158216642Sdavidxu_sleepq_drop(struct sleepqueue *sq, 159216642Sdavidxu void (*cb)(struct pthread *, void *arg), void *arg) 160216642Sdavidxu{ 161216642Sdavidxu struct pthread *td; 162216642Sdavidxu struct sleepqueue *sq2; 163216642Sdavidxu 164216642Sdavidxu td = TAILQ_FIRST(&sq->sq_blocked); 165216642Sdavidxu if (td == NULL) 166216642Sdavidxu return; 167216642Sdavidxu LIST_REMOVE(sq, sq_hash); 168216642Sdavidxu TAILQ_REMOVE(&sq->sq_blocked, td, wle); 169216642Sdavidxu if (cb != NULL) 170216642Sdavidxu cb(td, arg); 171216642Sdavidxu td->sleepqueue = sq; 172216642Sdavidxu td->wchan = NULL; 173216642Sdavidxu sq2 = SLIST_FIRST(&sq->sq_freeq); 174216642Sdavidxu TAILQ_FOREACH(td, &sq->sq_blocked, wle) { 175216642Sdavidxu if (cb != NULL) 176216642Sdavidxu cb(td, arg); 177216642Sdavidxu td->sleepqueue = sq2; 178216642Sdavidxu td->wchan = NULL; 179216642Sdavidxu sq2 = SLIST_NEXT(sq2, sq_flink); 180216642Sdavidxu } 181216642Sdavidxu TAILQ_INIT(&sq->sq_blocked); 182216642Sdavidxu SLIST_INIT(&sq->sq_freeq); 183216642Sdavidxu} 184