[f761f1eb] | 1 | /*
|
---|
| 2 | * Copyright (C) 2001-2004 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 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 <proc/scheduler.h>
|
---|
| 30 | #include <proc/thread.h>
|
---|
| 31 | #include <proc/task.h>
|
---|
| 32 | #include <cpu.h>
|
---|
| 33 | #include <mm/vm.h>
|
---|
| 34 | #include <config.h>
|
---|
| 35 | #include <context.h>
|
---|
| 36 | #include <func.h>
|
---|
| 37 | #include <arch.h>
|
---|
| 38 | #include <arch/asm.h>
|
---|
| 39 | #include <list.h>
|
---|
[02a99d2] | 40 | #include <panic.h>
|
---|
[f761f1eb] | 41 | #include <typedefs.h>
|
---|
| 42 | #include <mm/page.h>
|
---|
| 43 | #include <synch/spinlock.h>
|
---|
[f2ffad4] | 44 | #include <arch/faddr.h>
|
---|
[f761f1eb] | 45 |
|
---|
| 46 | #ifdef __SMP__
|
---|
[397c77f] | 47 | #include <arch/smp/atomic.h>
|
---|
[f761f1eb] | 48 | #endif /* __SMP__ */
|
---|
| 49 |
|
---|
| 50 | /*
|
---|
| 51 | * NOTE ON ATOMIC READS:
|
---|
| 52 | * Some architectures cannot read __u32 atomically.
|
---|
| 53 | * For that reason, all accesses to nrdy and the likes must be protected by spinlock.
|
---|
| 54 | */
|
---|
| 55 |
|
---|
| 56 | spinlock_t nrdylock;
|
---|
| 57 | volatile int nrdy;
|
---|
| 58 |
|
---|
[0ca6faa] | 59 | void before_thread_runs(void)
|
---|
| 60 | {
|
---|
| 61 | before_thread_runs_arch();
|
---|
[9c926f3] | 62 | fpu_context_restore(&(THREAD->saved_fpu_context));
|
---|
[0ca6faa] | 63 | }
|
---|
| 64 |
|
---|
| 65 |
|
---|
[f761f1eb] | 66 | void scheduler_init(void)
|
---|
| 67 | {
|
---|
| 68 | spinlock_initialize(&nrdylock);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /* cpu_priority_high()'d */
|
---|
| 72 | struct thread *find_best_thread(void)
|
---|
| 73 | {
|
---|
| 74 | thread_t *t;
|
---|
| 75 | runq_t *r;
|
---|
| 76 | int i, n;
|
---|
| 77 |
|
---|
| 78 | loop:
|
---|
| 79 | cpu_priority_high();
|
---|
| 80 |
|
---|
[43114c5] | 81 | spinlock_lock(&CPU->lock);
|
---|
| 82 | n = CPU->nrdy;
|
---|
| 83 | spinlock_unlock(&CPU->lock);
|
---|
[f761f1eb] | 84 |
|
---|
| 85 | cpu_priority_low();
|
---|
| 86 |
|
---|
| 87 | if (n == 0) {
|
---|
| 88 | #ifdef __SMP__
|
---|
| 89 | /*
|
---|
| 90 | * If the load balancing thread is not running, wake it up and
|
---|
| 91 | * set CPU-private flag that the kcpulb has been started.
|
---|
| 92 | */
|
---|
[43114c5] | 93 | if (test_and_set(&CPU->kcpulbstarted) == 0) {
|
---|
| 94 | waitq_wakeup(&CPU->kcpulb_wq, 0);
|
---|
[f761f1eb] | 95 | goto loop;
|
---|
| 96 | }
|
---|
| 97 | #endif /* __SMP__ */
|
---|
| 98 |
|
---|
| 99 | /*
|
---|
| 100 | * For there was nothing to run, the CPU goes to sleep
|
---|
| 101 | * until a hardware interrupt or an IPI comes.
|
---|
| 102 | * This improves energy saving and hyperthreading.
|
---|
| 103 | * On the other hand, several hardware interrupts can be ignored.
|
---|
| 104 | */
|
---|
| 105 | cpu_sleep();
|
---|
| 106 | goto loop;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | cpu_priority_high();
|
---|
| 110 |
|
---|
| 111 | for (i = 0; i<RQ_COUNT; i++) {
|
---|
[43114c5] | 112 | r = &CPU->rq[i];
|
---|
[f761f1eb] | 113 | spinlock_lock(&r->lock);
|
---|
| 114 | if (r->n == 0) {
|
---|
| 115 | /*
|
---|
| 116 | * If this queue is empty, try a lower-priority queue.
|
---|
| 117 | */
|
---|
| 118 | spinlock_unlock(&r->lock);
|
---|
| 119 | continue;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | spinlock_lock(&nrdylock);
|
---|
| 123 | nrdy--;
|
---|
| 124 | spinlock_unlock(&nrdylock);
|
---|
| 125 |
|
---|
[43114c5] | 126 | spinlock_lock(&CPU->lock);
|
---|
| 127 | CPU->nrdy--;
|
---|
| 128 | spinlock_unlock(&CPU->lock);
|
---|
[f761f1eb] | 129 |
|
---|
| 130 | r->n--;
|
---|
| 131 |
|
---|
| 132 | /*
|
---|
| 133 | * Take the first thread from the queue.
|
---|
| 134 | */
|
---|
| 135 | t = list_get_instance(r->rq_head.next, thread_t, rq_link);
|
---|
| 136 | list_remove(&t->rq_link);
|
---|
| 137 |
|
---|
| 138 | spinlock_unlock(&r->lock);
|
---|
| 139 |
|
---|
| 140 | spinlock_lock(&t->lock);
|
---|
[43114c5] | 141 | t->cpu = CPU;
|
---|
[f761f1eb] | 142 |
|
---|
| 143 | t->ticks = us2ticks((i+1)*10000);
|
---|
| 144 | t->pri = i; /* eventually correct rq index */
|
---|
| 145 |
|
---|
| 146 | /*
|
---|
| 147 | * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge.
|
---|
| 148 | */
|
---|
| 149 | t->flags &= ~X_STOLEN;
|
---|
| 150 | spinlock_unlock(&t->lock);
|
---|
| 151 |
|
---|
| 152 | return t;
|
---|
| 153 | }
|
---|
| 154 | goto loop;
|
---|
| 155 |
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /*
|
---|
| 159 | * This function prevents low priority threads from starving in rq's.
|
---|
| 160 | * When it decides to relink rq's, it reconnects respective pointers
|
---|
| 161 | * so that in result threads with 'pri' greater or equal 'start' are
|
---|
| 162 | * moved to a higher-priority queue.
|
---|
| 163 | */
|
---|
| 164 | void relink_rq(int start)
|
---|
| 165 | {
|
---|
| 166 | link_t head;
|
---|
| 167 | runq_t *r;
|
---|
| 168 | int i, n;
|
---|
| 169 |
|
---|
| 170 | list_initialize(&head);
|
---|
[43114c5] | 171 | spinlock_lock(&CPU->lock);
|
---|
| 172 | if (CPU->needs_relink > NEEDS_RELINK_MAX) {
|
---|
[f761f1eb] | 173 | for (i = start; i<RQ_COUNT-1; i++) {
|
---|
| 174 | /* remember and empty rq[i + 1] */
|
---|
[43114c5] | 175 | r = &CPU->rq[i + 1];
|
---|
[f761f1eb] | 176 | spinlock_lock(&r->lock);
|
---|
| 177 | list_concat(&head, &r->rq_head);
|
---|
| 178 | n = r->n;
|
---|
| 179 | r->n = 0;
|
---|
| 180 | spinlock_unlock(&r->lock);
|
---|
| 181 |
|
---|
| 182 | /* append rq[i + 1] to rq[i] */
|
---|
[43114c5] | 183 | r = &CPU->rq[i];
|
---|
[f761f1eb] | 184 | spinlock_lock(&r->lock);
|
---|
| 185 | list_concat(&r->rq_head, &head);
|
---|
| 186 | r->n += n;
|
---|
| 187 | spinlock_unlock(&r->lock);
|
---|
| 188 | }
|
---|
[43114c5] | 189 | CPU->needs_relink = 0;
|
---|
[f761f1eb] | 190 | }
|
---|
[43114c5] | 191 | spinlock_unlock(&CPU->lock);
|
---|
[f761f1eb] | 192 |
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | /*
|
---|
| 196 | * The scheduler.
|
---|
| 197 | */
|
---|
| 198 | void scheduler(void)
|
---|
| 199 | {
|
---|
| 200 | volatile pri_t pri;
|
---|
| 201 |
|
---|
| 202 | pri = cpu_priority_high();
|
---|
| 203 |
|
---|
| 204 | if (haltstate)
|
---|
| 205 | halt();
|
---|
| 206 |
|
---|
[43114c5] | 207 | if (THREAD) {
|
---|
| 208 | spinlock_lock(&THREAD->lock);
|
---|
[9c926f3] | 209 | fpu_context_save(&(THREAD->saved_fpu_context));
|
---|
[43114c5] | 210 | if (!context_save(&THREAD->saved_context)) {
|
---|
[f761f1eb] | 211 | /*
|
---|
| 212 | * This is the place where threads leave scheduler();
|
---|
| 213 | */
|
---|
[cb4b61d] | 214 | before_thread_runs();
|
---|
[43114c5] | 215 | spinlock_unlock(&THREAD->lock);
|
---|
| 216 | cpu_priority_restore(THREAD->saved_context.pri);
|
---|
[f761f1eb] | 217 | return;
|
---|
| 218 | }
|
---|
[43114c5] | 219 | THREAD->saved_context.pri = pri;
|
---|
[f761f1eb] | 220 | }
|
---|
| 221 |
|
---|
| 222 | /*
|
---|
| 223 | * We may not keep the old stack.
|
---|
| 224 | * Reason: If we kept the old stack and got blocked, for instance, in
|
---|
| 225 | * find_best_thread(), the old thread could get rescheduled by another
|
---|
| 226 | * CPU and overwrite the part of its own stack that was also used by
|
---|
| 227 | * the scheduler on this CPU.
|
---|
| 228 | *
|
---|
| 229 | * Moreover, we have to bypass the compiler-generated POP sequence
|
---|
| 230 | * which is fooled by SP being set to the very top of the stack.
|
---|
| 231 | * Therefore the scheduler() function continues in
|
---|
| 232 | * scheduler_separated_stack().
|
---|
| 233 | */
|
---|
[43114c5] | 234 | context_save(&CPU->saved_context);
|
---|
| 235 | CPU->saved_context.sp = (__address) &CPU->stack[CPU_STACK_SIZE-8];
|
---|
[f2ffad4] | 236 | CPU->saved_context.pc = FADDR(scheduler_separated_stack);
|
---|
[43114c5] | 237 | context_restore(&CPU->saved_context);
|
---|
[f761f1eb] | 238 | /* not reached */
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | void scheduler_separated_stack(void)
|
---|
| 242 | {
|
---|
| 243 | int priority;
|
---|
| 244 |
|
---|
[43114c5] | 245 | if (THREAD) {
|
---|
| 246 | switch (THREAD->state) {
|
---|
[f761f1eb] | 247 | case Running:
|
---|
[43114c5] | 248 | THREAD->state = Ready;
|
---|
| 249 | spinlock_unlock(&THREAD->lock);
|
---|
| 250 | thread_ready(THREAD);
|
---|
[f761f1eb] | 251 | break;
|
---|
| 252 |
|
---|
| 253 | case Exiting:
|
---|
[43114c5] | 254 | frame_free((__address) THREAD->kstack);
|
---|
| 255 | if (THREAD->ustack) {
|
---|
| 256 | frame_free((__address) THREAD->ustack);
|
---|
[f761f1eb] | 257 | }
|
---|
| 258 |
|
---|
| 259 | /*
|
---|
| 260 | * Detach from the containing task.
|
---|
| 261 | */
|
---|
[43114c5] | 262 | spinlock_lock(&TASK->lock);
|
---|
| 263 | list_remove(&THREAD->th_link);
|
---|
| 264 | spinlock_unlock(&TASK->lock);
|
---|
[f761f1eb] | 265 |
|
---|
[43114c5] | 266 | spinlock_unlock(&THREAD->lock);
|
---|
[f761f1eb] | 267 |
|
---|
| 268 | spinlock_lock(&threads_lock);
|
---|
[43114c5] | 269 | list_remove(&THREAD->threads_link);
|
---|
[f761f1eb] | 270 | spinlock_unlock(&threads_lock);
|
---|
[6a27d63] | 271 |
|
---|
| 272 | spinlock_lock(&THREAD->cpu->lock);
|
---|
[ea3fb2e] | 273 | if(THREAD->cpu->fpu_owner==THREAD) THREAD->cpu->fpu_owner=NULL;
|
---|
[6a27d63] | 274 | spinlock_unlock(&THREAD->cpu->lock);
|
---|
| 275 |
|
---|
[f761f1eb] | 276 |
|
---|
[43114c5] | 277 | free(THREAD);
|
---|
[f761f1eb] | 278 |
|
---|
| 279 | break;
|
---|
| 280 |
|
---|
| 281 | case Sleeping:
|
---|
| 282 | /*
|
---|
| 283 | * Prefer the thread after it's woken up.
|
---|
| 284 | */
|
---|
[43114c5] | 285 | THREAD->pri = -1;
|
---|
[f761f1eb] | 286 |
|
---|
| 287 | /*
|
---|
| 288 | * We need to release wq->lock which we locked in waitq_sleep().
|
---|
[43114c5] | 289 | * Address of wq->lock is kept in THREAD->sleep_queue.
|
---|
[f761f1eb] | 290 | */
|
---|
[43114c5] | 291 | spinlock_unlock(&THREAD->sleep_queue->lock);
|
---|
[f761f1eb] | 292 |
|
---|
| 293 | /*
|
---|
| 294 | * Check for possible requests for out-of-context invocation.
|
---|
| 295 | */
|
---|
[43114c5] | 296 | if (THREAD->call_me) {
|
---|
| 297 | THREAD->call_me(THREAD->call_me_with);
|
---|
| 298 | THREAD->call_me = NULL;
|
---|
| 299 | THREAD->call_me_with = NULL;
|
---|
[f761f1eb] | 300 | }
|
---|
| 301 |
|
---|
[43114c5] | 302 | spinlock_unlock(&THREAD->lock);
|
---|
[f761f1eb] | 303 |
|
---|
| 304 | break;
|
---|
| 305 |
|
---|
| 306 | default:
|
---|
| 307 | /*
|
---|
| 308 | * Entering state is unexpected.
|
---|
| 309 | */
|
---|
[43114c5] | 310 | panic("tid%d: unexpected state %s\n", THREAD->tid, thread_states[THREAD->state]);
|
---|
[f761f1eb] | 311 | break;
|
---|
| 312 | }
|
---|
[43114c5] | 313 | THREAD = NULL;
|
---|
[f761f1eb] | 314 | }
|
---|
| 315 |
|
---|
[43114c5] | 316 | THREAD = find_best_thread();
|
---|
[f761f1eb] | 317 |
|
---|
[43114c5] | 318 | spinlock_lock(&THREAD->lock);
|
---|
| 319 | priority = THREAD->pri;
|
---|
| 320 | spinlock_unlock(&THREAD->lock);
|
---|
[f761f1eb] | 321 |
|
---|
| 322 | relink_rq(priority);
|
---|
| 323 |
|
---|
[43114c5] | 324 | spinlock_lock(&THREAD->lock);
|
---|
[f761f1eb] | 325 |
|
---|
| 326 | /*
|
---|
| 327 | * If both the old and the new task are the same, lots of work is avoided.
|
---|
| 328 | */
|
---|
[43114c5] | 329 | if (TASK != THREAD->task) {
|
---|
[f761f1eb] | 330 | vm_t *m1 = NULL;
|
---|
| 331 | vm_t *m2;
|
---|
| 332 |
|
---|
[43114c5] | 333 | if (TASK) {
|
---|
| 334 | spinlock_lock(&TASK->lock);
|
---|
| 335 | m1 = TASK->vm;
|
---|
| 336 | spinlock_unlock(&TASK->lock);
|
---|
[f761f1eb] | 337 | }
|
---|
| 338 |
|
---|
[43114c5] | 339 | spinlock_lock(&THREAD->task->lock);
|
---|
| 340 | m2 = THREAD->task->vm;
|
---|
| 341 | spinlock_unlock(&THREAD->task->lock);
|
---|
[f761f1eb] | 342 |
|
---|
| 343 | /*
|
---|
| 344 | * Note that it is possible for two tasks to share one vm mapping.
|
---|
| 345 | */
|
---|
| 346 | if (m1 != m2) {
|
---|
| 347 | /*
|
---|
| 348 | * Both tasks and vm mappings are different.
|
---|
| 349 | * Replace the old one with the new one.
|
---|
| 350 | */
|
---|
| 351 | if (m1) {
|
---|
| 352 | vm_uninstall(m1);
|
---|
| 353 | }
|
---|
| 354 | vm_install(m2);
|
---|
| 355 | }
|
---|
[43114c5] | 356 | TASK = THREAD->task;
|
---|
[f761f1eb] | 357 | }
|
---|
| 358 |
|
---|
[43114c5] | 359 | THREAD->state = Running;
|
---|
[f761f1eb] | 360 |
|
---|
| 361 | #ifdef SCHEDULER_VERBOSE
|
---|
[43114c5] | 362 | printf("cpu%d: tid %d (pri=%d,ticks=%d,nrdy=%d)\n", CPU->id, THREAD->tid, THREAD->pri, THREAD->ticks, CPU->nrdy);
|
---|
[f761f1eb] | 363 | #endif
|
---|
| 364 |
|
---|
[43114c5] | 365 | context_restore(&THREAD->saved_context);
|
---|
[f761f1eb] | 366 | /* not reached */
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | #ifdef __SMP__
|
---|
| 370 | /*
|
---|
| 371 | * This is the load balancing thread.
|
---|
| 372 | * It supervises thread supplies for the CPU it's wired to.
|
---|
| 373 | */
|
---|
| 374 | void kcpulb(void *arg)
|
---|
| 375 | {
|
---|
| 376 | thread_t *t;
|
---|
| 377 | int count, i, j, k = 0;
|
---|
| 378 | pri_t pri;
|
---|
| 379 |
|
---|
| 380 | loop:
|
---|
| 381 | /*
|
---|
| 382 | * Sleep until there's some work to do.
|
---|
| 383 | */
|
---|
[43114c5] | 384 | waitq_sleep(&CPU->kcpulb_wq);
|
---|
[f761f1eb] | 385 |
|
---|
| 386 | not_satisfied:
|
---|
| 387 | /*
|
---|
| 388 | * Calculate the number of threads that will be migrated/stolen from
|
---|
| 389 | * other CPU's. Note that situation can have changed between two
|
---|
| 390 | * passes. Each time get the most up to date counts.
|
---|
| 391 | */
|
---|
| 392 | pri = cpu_priority_high();
|
---|
[43114c5] | 393 | spinlock_lock(&CPU->lock);
|
---|
[f761f1eb] | 394 | count = nrdy / config.cpu_active;
|
---|
[43114c5] | 395 | count -= CPU->nrdy;
|
---|
| 396 | spinlock_unlock(&CPU->lock);
|
---|
[f761f1eb] | 397 | cpu_priority_restore(pri);
|
---|
| 398 |
|
---|
| 399 | if (count <= 0)
|
---|
| 400 | goto satisfied;
|
---|
| 401 |
|
---|
| 402 | /*
|
---|
| 403 | * Searching least priority queues on all CPU's first and most priority queues on all CPU's last.
|
---|
| 404 | */
|
---|
| 405 | for (j=RQ_COUNT-1; j >= 0; j--) {
|
---|
| 406 | for (i=0; i < config.cpu_active; i++) {
|
---|
| 407 | link_t *l;
|
---|
| 408 | runq_t *r;
|
---|
| 409 | cpu_t *cpu;
|
---|
| 410 |
|
---|
| 411 | cpu = &cpus[(i + k) % config.cpu_active];
|
---|
| 412 | r = &cpu->rq[j];
|
---|
| 413 |
|
---|
| 414 | /*
|
---|
| 415 | * Not interested in ourselves.
|
---|
| 416 | * Doesn't require interrupt disabling for kcpulb is X_WIRED.
|
---|
| 417 | */
|
---|
[43114c5] | 418 | if (CPU == cpu)
|
---|
[f761f1eb] | 419 | continue;
|
---|
| 420 |
|
---|
| 421 | restart: pri = cpu_priority_high();
|
---|
| 422 | spinlock_lock(&r->lock);
|
---|
| 423 | if (r->n == 0) {
|
---|
| 424 | spinlock_unlock(&r->lock);
|
---|
| 425 | cpu_priority_restore(pri);
|
---|
| 426 | continue;
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | t = NULL;
|
---|
| 430 | l = r->rq_head.prev; /* search rq from the back */
|
---|
| 431 | while (l != &r->rq_head) {
|
---|
| 432 | t = list_get_instance(l, thread_t, rq_link);
|
---|
| 433 | /*
|
---|
| 434 | * We don't want to steal CPU-wired threads neither threads already stolen.
|
---|
| 435 | * The latter prevents threads from migrating between CPU's without ever being run.
|
---|
[6a27d63] | 436 | * We don't want to steal threads whose FPU context is still in CPU
|
---|
| 437 | */
|
---|
[f761f1eb] | 438 | spinlock_lock(&t->lock);
|
---|
[6a27d63] | 439 | if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) {
|
---|
[f761f1eb] | 440 | /*
|
---|
| 441 | * Remove t from r.
|
---|
| 442 | */
|
---|
| 443 |
|
---|
| 444 | spinlock_unlock(&t->lock);
|
---|
| 445 |
|
---|
| 446 | /*
|
---|
| 447 | * Here we have to avoid deadlock with relink_rq(),
|
---|
| 448 | * because it locks cpu and r in a different order than we do.
|
---|
| 449 | */
|
---|
| 450 | if (!spinlock_trylock(&cpu->lock)) {
|
---|
| 451 | /* Release all locks and try again. */
|
---|
| 452 | spinlock_unlock(&r->lock);
|
---|
| 453 | cpu_priority_restore(pri);
|
---|
| 454 | goto restart;
|
---|
| 455 | }
|
---|
| 456 | cpu->nrdy--;
|
---|
| 457 | spinlock_unlock(&cpu->lock);
|
---|
| 458 |
|
---|
| 459 | spinlock_lock(&nrdylock);
|
---|
| 460 | nrdy--;
|
---|
| 461 | spinlock_unlock(&nrdylock);
|
---|
| 462 |
|
---|
| 463 | r->n--;
|
---|
| 464 | list_remove(&t->rq_link);
|
---|
| 465 |
|
---|
| 466 | break;
|
---|
| 467 | }
|
---|
| 468 | spinlock_unlock(&t->lock);
|
---|
| 469 | l = l->prev;
|
---|
| 470 | t = NULL;
|
---|
| 471 | }
|
---|
| 472 | spinlock_unlock(&r->lock);
|
---|
| 473 |
|
---|
| 474 | if (t) {
|
---|
| 475 | /*
|
---|
| 476 | * Ready t on local CPU
|
---|
| 477 | */
|
---|
| 478 | spinlock_lock(&t->lock);
|
---|
| 479 | #ifdef KCPULB_VERBOSE
|
---|
[43114c5] | 480 | printf("kcpulb%d: TID %d -> cpu%d, nrdy=%d, avg=%d\n", CPU->id, t->tid, CPU->id, CPU->nrdy, nrdy / config.cpu_active);
|
---|
[f761f1eb] | 481 | #endif
|
---|
| 482 | t->flags |= X_STOLEN;
|
---|
| 483 | spinlock_unlock(&t->lock);
|
---|
| 484 |
|
---|
| 485 | thread_ready(t);
|
---|
| 486 |
|
---|
| 487 | cpu_priority_restore(pri);
|
---|
| 488 |
|
---|
| 489 | if (--count == 0)
|
---|
| 490 | goto satisfied;
|
---|
| 491 |
|
---|
| 492 | /*
|
---|
| 493 | * We are not satisfied yet, focus on another CPU next time.
|
---|
| 494 | */
|
---|
| 495 | k++;
|
---|
| 496 |
|
---|
| 497 | continue;
|
---|
| 498 | }
|
---|
| 499 | cpu_priority_restore(pri);
|
---|
| 500 | }
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[43114c5] | 503 | if (CPU->nrdy) {
|
---|
[f761f1eb] | 504 | /*
|
---|
| 505 | * Be a little bit light-weight and let migrated threads run.
|
---|
| 506 | */
|
---|
| 507 | scheduler();
|
---|
| 508 | }
|
---|
| 509 | else {
|
---|
| 510 | /*
|
---|
| 511 | * We failed to migrate a single thread.
|
---|
| 512 | * Something more sophisticated should be done.
|
---|
| 513 | */
|
---|
| 514 | scheduler();
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | goto not_satisfied;
|
---|
| 518 |
|
---|
| 519 | satisfied:
|
---|
| 520 | /*
|
---|
| 521 | * Tell find_best_thread() to wake us up later again.
|
---|
| 522 | */
|
---|
[43114c5] | 523 | CPU->kcpulbstarted = 0;
|
---|
[f761f1eb] | 524 | goto loop;
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | #endif /* __SMP__ */
|
---|