1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2010 David Xu <davidxu@freebsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #include <stdlib.h> 31 #include "thr_private.h" 32 33 #define HASHSHIFT 9 34 #define HASHSIZE (1 << HASHSHIFT) 35 #define SC_HASH(wchan) ((unsigned) \ 36 ((((uintptr_t)(wchan) >> 3) \ 37 ^ ((uintptr_t)(wchan) >> (HASHSHIFT + 3))) \ 38 & (HASHSIZE - 1))) 39 #define SC_LOOKUP(wc) &sc_table[SC_HASH(wc)] 40 41 struct sleepqueue_chain { 42 struct umutex sc_lock; 43 int sc_enqcnt; 44 LIST_HEAD(, sleepqueue) sc_queues; 45 int sc_type; 46 }; 47 48 static struct sleepqueue_chain sc_table[HASHSIZE]; 49 50 void 51 _sleepq_init(void) 52 { 53 int i; 54 55 for (i = 0; i < HASHSIZE; ++i) { 56 LIST_INIT(&sc_table[i].sc_queues); 57 _thr_umutex_init(&sc_table[i].sc_lock); 58 } 59 } 60 61 struct sleepqueue * 62 _sleepq_alloc(void) 63 { 64 struct sleepqueue *sq; 65 66 sq = calloc(1, sizeof(struct sleepqueue)); 67 TAILQ_INIT(&sq->sq_blocked); 68 SLIST_INIT(&sq->sq_freeq); 69 return (sq); 70 } 71 72 void 73 _sleepq_free(struct sleepqueue *sq) 74 { 75 free(sq); 76 } 77 78 void 79 _sleepq_lock(void *wchan) 80 { 81 struct pthread *curthread = _get_curthread(); 82 struct sleepqueue_chain *sc; 83 84 sc = SC_LOOKUP(wchan); 85 THR_LOCK_ACQUIRE_SPIN(curthread, &sc->sc_lock); 86 } 87 88 void 89 _sleepq_unlock(void *wchan) 90 { 91 struct sleepqueue_chain *sc; 92 struct pthread *curthread = _get_curthread(); 93 94 sc = SC_LOOKUP(wchan); 95 THR_LOCK_RELEASE(curthread, &sc->sc_lock); 96 } 97 98 static inline struct sleepqueue * 99 lookup(struct sleepqueue_chain *sc, void *wchan) 100 { 101 struct sleepqueue *sq; 102 103 LIST_FOREACH(sq, &sc->sc_queues, sq_hash) 104 if (sq->sq_wchan == wchan) 105 return (sq); 106 return (NULL); 107 } 108 109 struct sleepqueue * 110 _sleepq_lookup(void *wchan) 111 { 112 return (lookup(SC_LOOKUP(wchan), wchan)); 113 } 114 115 void 116 _sleepq_add(void *wchan, struct pthread *td) 117 { 118 struct sleepqueue_chain *sc; 119 struct sleepqueue *sq; 120 121 sc = SC_LOOKUP(wchan); 122 sq = lookup(sc, wchan); 123 if (sq != NULL) { 124 SLIST_INSERT_HEAD(&sq->sq_freeq, td->sleepqueue, sq_flink); 125 } else { 126 sq = td->sleepqueue; 127 LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash); 128 sq->sq_wchan = wchan; 129 /* sq->sq_type = type; */ 130 } 131 td->sleepqueue = NULL; 132 td->wchan = wchan; 133 if (((++sc->sc_enqcnt << _thr_queuefifo) & 0xff) != 0) 134 TAILQ_INSERT_HEAD(&sq->sq_blocked, td, wle); 135 else 136 TAILQ_INSERT_TAIL(&sq->sq_blocked, td, wle); 137 } 138 139 int 140 _sleepq_remove(struct sleepqueue *sq, struct pthread *td) 141 { 142 int rc; 143 144 TAILQ_REMOVE(&sq->sq_blocked, td, wle); 145 if (TAILQ_EMPTY(&sq->sq_blocked)) { 146 LIST_REMOVE(sq, sq_hash); 147 td->sleepqueue = sq; 148 rc = 0; 149 } else { 150 td->sleepqueue = SLIST_FIRST(&sq->sq_freeq); 151 SLIST_REMOVE_HEAD(&sq->sq_freeq, sq_flink); 152 rc = 1; 153 } 154 td->wchan = NULL; 155 return (rc); 156 } 157 158 void 159 _sleepq_drop(struct sleepqueue *sq, 160 void (*cb)(struct pthread *, void *arg), void *arg) 161 { 162 struct pthread *td; 163 struct sleepqueue *sq2; 164 165 td = TAILQ_FIRST(&sq->sq_blocked); 166 if (td == NULL) 167 return; 168 LIST_REMOVE(sq, sq_hash); 169 TAILQ_REMOVE(&sq->sq_blocked, td, wle); 170 if (cb != NULL) 171 cb(td, arg); 172 td->sleepqueue = sq; 173 td->wchan = NULL; 174 sq2 = SLIST_FIRST(&sq->sq_freeq); 175 TAILQ_FOREACH(td, &sq->sq_blocked, wle) { 176 if (cb != NULL) 177 cb(td, arg); 178 td->sleepqueue = sq2; 179 td->wchan = NULL; 180 sq2 = SLIST_NEXT(sq2, sq_flink); 181 } 182 TAILQ_INIT(&sq->sq_blocked); 183 SLIST_INIT(&sq->sq_freeq); 184 } 185