kern_thread.c revision 99072
1/* 2 * Copyright (C) 2001 Julian Elischer <julian@freebsd.org>. 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 * 1. Redistributions of source code must retain the above copyright 9 * notice(s), this list of conditions and the following disclaimer as 10 * the first lines of this file unmodified other than the possible 11 * addition of one or more copyright notices. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice(s), this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 26 * DAMAGE. 27 * 28 * $FreeBSD: head/sys/kern/kern_thread.c 99026 2002-06-29 07:04:59Z julian $ 29 */ 30 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/kernel.h> 34#include <sys/lock.h> 35#include <sys/malloc.h> 36#include <sys/mutex.h> 37#include <sys/proc.h> 38#include <sys/sysctl.h> 39#include <sys/filedesc.h> 40#include <sys/tty.h> 41#include <sys/signalvar.h> 42#include <sys/sx.h> 43#include <sys/user.h> 44#include <sys/jail.h> 45#include <sys/kse.h> 46#include <sys/ktr.h> 47 48#include <vm/vm.h> 49#include <vm/vm_object.h> 50#include <vm/pmap.h> 51#include <vm/uma.h> 52#include <vm/vm_map.h> 53 54/* 55 * Thread related storage. 56 */ 57static uma_zone_t thread_zone; 58static int allocated_threads; 59static int active_threads; 60static int cached_threads; 61 62SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, "thread allocation"); 63 64SYSCTL_INT(_kern_threads, OID_AUTO, active, CTLFLAG_RD, 65 &active_threads, 0, "Number of active threads in system."); 66 67SYSCTL_INT(_kern_threads, OID_AUTO, cached, CTLFLAG_RD, 68 &cached_threads, 0, "Number of threads in thread cache."); 69 70SYSCTL_INT(_kern_threads, OID_AUTO, allocated, CTLFLAG_RD, 71 &allocated_threads, 0, "Number of threads in zone."); 72 73static int oiks_debug = 1; /* 0 disable, 1 printf, 2 enter debugger */ 74SYSCTL_INT(_kern_threads, OID_AUTO, oiks, CTLFLAG_RW, 75 &oiks_debug, 0, "OIKS thread debug"); 76 77#define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start)) 78 79struct threadqueue zombie_threads = TAILQ_HEAD_INITIALIZER(zombie_threads); 80struct mtx zombie_thread_lock; 81MTX_SYSINIT(zombie_thread_lock, &zombie_thread_lock, 82 "zombie_thread_lock", MTX_SPIN); 83 84/* 85 * Pepare a thread for use. 86 */ 87static void 88thread_ctor(void *mem, int size, void *arg) 89{ 90 struct thread *td; 91 92 KASSERT((size == sizeof(struct thread)), 93 ("size mismatch: %d != %d\n", size, sizeof(struct thread))); 94 95 td = (struct thread *)mem; 96 bzero(&td->td_startzero, 97 (unsigned)RANGEOF(struct thread, td_startzero, td_endzero)); 98 td->td_state = TDS_NEW; 99 td->td_flags |= TDF_UNBOUND; 100 cached_threads--; /* XXXSMP */ 101 active_threads++; /* XXXSMP */ 102} 103 104/* 105 * Reclaim a thread after use. 106 */ 107static void 108thread_dtor(void *mem, int size, void *arg) 109{ 110 struct thread *td; 111 112 KASSERT((size == sizeof(struct thread)), 113 ("size mismatch: %d != %d\n", size, sizeof(struct thread))); 114 115 td = (struct thread *)mem; 116 117#ifdef INVARIANTS 118 /* Verify that this thread is in a safe state to free. */ 119 switch (td->td_state) { 120 case TDS_SLP: 121 case TDS_MTX: 122 case TDS_RUNQ: 123 /* 124 * We must never unlink a thread that is in one of 125 * these states, because it is currently active. 126 */ 127 panic("bad state for thread unlinking"); 128 /* NOTREACHED */ 129 case TDS_UNQUEUED: 130 case TDS_NEW: 131 case TDS_RUNNING: 132 case TDS_SURPLUS: 133 break; 134 default: 135 panic("bad thread state"); 136 /* NOTREACHED */ 137 } 138#endif 139 140 /* Update counters. */ 141 active_threads--; /* XXXSMP */ 142 cached_threads++; /* XXXSMP */ 143} 144 145/* 146 * Initialize type-stable parts of a thread (when newly created). 147 */ 148static void 149thread_init(void *mem, int size) 150{ 151 struct thread *td; 152 153 KASSERT((size == sizeof(struct thread)), 154 ("size mismatch: %d != %d\n", size, sizeof(struct thread))); 155 156 td = (struct thread *)mem; 157 pmap_new_thread(td); 158 cpu_thread_setup(td); 159 cached_threads++; /* XXXSMP */ 160 allocated_threads++; /* XXXSMP */ 161} 162 163/* 164 * Tear down type-stable parts of a thread (just before being discarded). 165 */ 166static void 167thread_fini(void *mem, int size) 168{ 169 struct thread *td; 170 171 KASSERT((size == sizeof(struct thread)), 172 ("size mismatch: %d != %d\n", size, sizeof(struct thread))); 173 174 td = (struct thread *)mem; 175 pmap_dispose_thread(td); 176 cached_threads--; /* XXXSMP */ 177 allocated_threads--; /* XXXSMP */ 178} 179 180/* 181 * Initialize global thread allocation resources. 182 */ 183void 184threadinit(void) 185{ 186 187 thread_zone = uma_zcreate("THREAD", sizeof (struct thread), 188 thread_ctor, thread_dtor, thread_init, thread_fini, 189 UMA_ALIGN_CACHE, 0); 190} 191 192/* 193 * Stash an embarasingly esxtra thread into the zombie thread queue. 194 */ 195void 196thread_stash(struct thread *td) 197{ 198 mtx_lock_spin(&zombie_thread_lock); 199 TAILQ_INSERT_HEAD(&zombie_threads, td, td_runq); 200 mtx_unlock_spin(&zombie_thread_lock); 201} 202 203/* 204 * reap any zombie threads for this Processor. 205 */ 206void 207thread_reap(void) 208{ 209 struct thread *td_reaped; 210 211 /* 212 * don't even bother to lock if none at this instant 213 * We really don't care about the next instant.. 214 */ 215 if (!TAILQ_EMPTY(&zombie_threads)) { 216 mtx_lock_spin(&zombie_thread_lock); 217 while (!TAILQ_EMPTY(&zombie_threads)) { 218 td_reaped = TAILQ_FIRST(&zombie_threads); 219 TAILQ_REMOVE(&zombie_threads, td_reaped, td_runq); 220 mtx_unlock_spin(&zombie_thread_lock); 221 thread_free(td_reaped); 222 mtx_lock_spin(&zombie_thread_lock); 223 } 224 mtx_unlock_spin(&zombie_thread_lock); 225 } 226} 227 228/* 229 * Allocate a thread. 230 */ 231struct thread * 232thread_alloc(void) 233{ 234 thread_reap(); /* check if any zombies to get */ 235 return (uma_zalloc(thread_zone, M_WAITOK)); 236} 237 238/* 239 * Deallocate a thread. 240 */ 241void 242thread_free(struct thread *td) 243{ 244 uma_zfree(thread_zone, td); 245} 246 247/* 248 * Store the thread context in the UTS's mailbox. 249 */ 250int 251thread_export_context(struct thread *td) 252{ 253 struct kse *ke; 254 uintptr_t td2_mbx; 255 void *addr1; 256 void *addr2; 257 int error; 258 259 /* Export the register contents. */ 260 error = cpu_export_context(td); 261 262 ke = td->td_kse; 263 addr1 = (caddr_t)ke->ke_mailbox 264 + offsetof(struct kse_mailbox, kmbx_completed_threads); 265 addr2 = (caddr_t)td->td_mailbox 266 + offsetof(struct thread_mailbox , next_completed); 267 /* Then link it into it's KSE's list of completed threads. */ 268 if (!error) { 269 error = td2_mbx = fuword(addr1); 270 if (error == -1) 271 error = EFAULT; 272 else 273 error = 0; 274 } 275 if (!error) 276 error = suword(addr2, td2_mbx); 277 if (!error) 278 error = suword(addr1, (u_long)td->td_mailbox); 279 if (error == -1) 280 error = EFAULT; 281 return (error); 282} 283 284 285/* 286 * Discard the current thread and exit from its context. 287 * 288 * Because we can't free a thread while we're operating under its context, 289 * push the current thread into our KSE's ke_tdspare slot, freeing the 290 * thread that might be there currently. Because we know that only this 291 * processor will run our KSE, we needn't worry about someone else grabbing 292 * our context before we do a cpu_throw. 293 */ 294void 295thread_exit(void) 296{ 297 struct thread *td; 298 struct kse *ke; 299 struct proc *p; 300 struct ksegrp *kg; 301 302 td = curthread; 303 kg = td->td_ksegrp; 304 p = td->td_proc; 305 ke = td->td_kse; 306 307 mtx_assert(&sched_lock, MA_OWNED); 308 PROC_LOCK_ASSERT(p, MA_OWNED); 309 CTR1(KTR_PROC, "thread_exit: thread %p", td); 310 KASSERT(!mtx_owned(&Giant), ("dying thread owns giant")); 311 312 if (ke->ke_tdspare != NULL) { 313 thread_stash(ke->ke_tdspare); 314 ke->ke_tdspare = NULL; 315 } 316 cpu_thread_exit(td); /* XXXSMP */ 317 318 /* Reassign this thread's KSE. */ 319 if (ke != NULL) { 320KASSERT((ke->ke_state == KES_RUNNING), ("zapping kse not running")); 321KASSERT((ke->ke_thread == td ), ("kse ke_thread mismatch against curthread")); 322KASSERT((ke->ke_thread->td_state == TDS_RUNNING), ("zapping thread not running")); 323 ke->ke_thread = NULL; 324 td->td_kse = NULL; 325 ke->ke_state = KES_UNQUEUED; 326 kse_reassign(ke); 327 } 328 329 /* Unlink this thread from its proc. and the kseg */ 330 if (p != NULL) { 331 TAILQ_REMOVE(&p->p_threads, td, td_plist); 332 p->p_numthreads--; 333 if (kg != NULL) { 334 TAILQ_REMOVE(&kg->kg_threads, td, td_kglist); 335 kg->kg_numthreads--; 336 } 337 /* 338 * The test below is NOT true if we are the 339 * sole exiting thread. P_STOPPED_SNGL is unset 340 * in exit1() after it is the only survivor. 341 */ 342 if (P_SHOULDSTOP(p) == P_STOPPED_SNGL) { 343 if (p->p_numthreads == p->p_suspcount) { 344 TAILQ_REMOVE(&p->p_suspended, 345 p->p_singlethread, td_runq); 346 setrunqueue(p->p_singlethread); 347 p->p_suspcount--; 348 } 349 } 350 } 351 td->td_state = TDS_SURPLUS; 352 td->td_proc = NULL; 353 td->td_ksegrp = NULL; 354 td->td_last_kse = NULL; 355 ke->ke_tdspare = td; 356 PROC_UNLOCK(p); 357 cpu_throw(); 358 /* NOTREACHED */ 359} 360 361/* 362 * Link a thread to a process. 363 * 364 * Note that we do not link to the proc's ucred here. 365 * The thread is linked as if running but no KSE assigned. 366 */ 367void 368thread_link(struct thread *td, struct ksegrp *kg) 369{ 370 struct proc *p; 371 372 p = kg->kg_proc; 373 td->td_state = TDS_NEW; 374 td->td_proc = p; 375 td->td_ksegrp = kg; 376 td->td_last_kse = NULL; 377 378 TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist); 379 TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist); 380 p->p_numthreads++; 381 kg->kg_numthreads++; 382 if (oiks_debug && p->p_numthreads > 4) { 383 printf("OIKS %d\n", p->p_numthreads); 384 if (oiks_debug > 1) 385 Debugger("OIKS"); 386 } 387 td->td_critnest = 0; 388 td->td_kse = NULL; 389} 390 391/* 392 * Set up the upcall pcb in either a given thread or a new one 393 * if none given. Use the upcall for the given KSE 394 * XXXKSE possibly fix cpu_set_upcall() to not need td->td_kse set. 395 */ 396struct thread * 397thread_schedule_upcall(struct thread *td, struct kse *ke) 398{ 399 struct thread *td2; 400 401 mtx_assert(&sched_lock, MA_OWNED); 402 if (ke->ke_tdspare != NULL) { 403 td2 = ke->ke_tdspare; 404 ke->ke_tdspare = NULL; 405 } else { 406 mtx_unlock_spin(&sched_lock); 407 td2 = thread_alloc(); 408 mtx_lock_spin(&sched_lock); 409 } 410 CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)", 411 td, td->td_proc->p_pid, td->td_proc->p_comm); 412 thread_link(td2, ke->ke_ksegrp); 413 cpu_set_upcall(td2, ke->ke_pcb); 414 td2->td_ucred = crhold(td->td_ucred); 415 td2->td_flags = TDF_UNBOUND|TDF_UPCALLING; 416 td2->td_priority = td->td_priority; 417 setrunqueue(td2); 418 return (td2); 419} 420 421/* 422 * The extra work we go through if we are a threaded process when we 423 * return to userland 424 * 425 * If we are a KSE process and returning to user mode, check for 426 * extra work to do before we return (e.g. for more syscalls 427 * to complete first). If we were in a critical section, we should 428 * just return to let it finish. Same if we were in the UTS (in 429 * which case we will have no thread mailbox registered). The only 430 * traps we suport will have set the mailbox. We will clear it here. 431 */ 432int 433thread_userret(struct proc *p, struct ksegrp *kg, struct kse *ke, 434 struct thread *td, struct trapframe *frame) 435{ 436 int error = 0; 437 438 if (ke->ke_tdspare == NULL) { 439 mtx_lock(&Giant); 440 ke->ke_tdspare = thread_alloc(); 441 mtx_unlock(&Giant); 442 } 443 if (td->td_flags & TDF_UNBOUND) { 444 /* 445 * Are we returning from a thread that had a mailbox? 446 * 447 * XXX Maybe this should be in a separate function. 448 */ 449 if (((td->td_flags & TDF_UPCALLING) == 0) && td->td_mailbox) { 450 /* 451 * [XXXKSE Future enhancement] 452 * We could also go straight back to the syscall 453 * if we never had to do an upcall since then. 454 * If the KSE's copy is == the thread's copy.. 455 * AND there are no other completed threads. 456 */ 457 /* 458 * We will go back as an upcall or go do another thread. 459 * Either way we need to save the context back to 460 * the user thread mailbox. 461 * So the UTS can restart it later. 462 */ 463 error = thread_export_context(td); 464 td->td_mailbox = NULL; 465 if (error) { 466 /* 467 * Failing to do the KSE 468 * operation just defaults operation 469 * back to synchonous operation. 470 */ 471 goto cont; 472 } 473 474 if (TAILQ_FIRST(&kg->kg_runq)) { 475 /* 476 * Uh-oh.. don't return to the user. 477 * Instead, switch to the thread that 478 * needs to run. The question is: 479 * What do we do with the thread we have now? 480 * We have put the completion block 481 * on the kse mailbox. If we had more energy, 482 * we could lazily do so, assuming someone 483 * else might get to userland earlier 484 * and deliver it earlier than we could. 485 * To do that we could save it off the KSEG. 486 * An upcalling KSE would 'reap' all completed 487 * threads. 488 * Being in a hurry, we'll do nothing and 489 * leave it on the current KSE for now. 490 * 491 * As for the other threads to run; 492 * we COULD rush through all the threads 493 * in this KSEG at this priority, or we 494 * could throw the ball back into the court 495 * and just run the highest prio kse available. 496 * What is OUR priority? 497 * the priority of the highest sycall waiting 498 * to be returned? 499 * For now, just let another KSE run (easiest). 500 */ 501 PROC_LOCK(p); 502 mtx_lock_spin(&sched_lock); 503 thread_exit(); /* Abandon current thread. */ 504 /* NOTREACHED */ 505 } else { /* if (number of returning syscalls = 1) */ 506 /* 507 * Swap our frame for the upcall frame. 508 * 509 * XXXKSE Assumes we are going to user land 510 * and not nested in the kernel 511 */ 512 td->td_flags |= TDF_UPCALLING; 513 } 514 } 515 /* 516 * This is NOT just an 'else' clause for the above test... 517 */ 518 if (td->td_flags & TDF_UPCALLING) { 519 CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)", 520 td, p->p_pid, p->p_comm); 521 /* 522 * Make sure that it has the correct frame loaded. 523 * While we know that we are on the same KSEGRP 524 * as we were created on, we could very easily 525 * have come in on another KSE. We therefore need 526 * to do the copy of the frame after the last 527 * possible switch() (the one above). 528 */ 529 bcopy(ke->ke_frame, frame, sizeof(struct trapframe)); 530 531 /* 532 * Decide what we are sending to the user 533 * upcall sets one argument. The address of the mbox. 534 */ 535 cpu_set_args(td, ke); 536 537 /* 538 * There is no more work to do and we are going to ride 539 * this thead/KSE up to userland. Make sure the user's 540 * pointer to the thread mailbox is cleared before we 541 * re-enter the kernel next time for any reason.. 542 * We might as well do it here. 543 */ 544 td->td_flags &= ~TDF_UPCALLING; /* Hmmmm. */ 545 error = suword((caddr_t)td->td_kse->ke_mailbox + 546 offsetof(struct kse_mailbox, kmbx_current_thread), 547 0); 548 } 549 /* 550 * Stop any chance that we may be separated from 551 * the KSE we are currently on. This is "biting the bullet", 552 * we are committing to go to user space as as THIS KSE here. 553 */ 554cont: 555 td->td_flags &= ~TDF_UNBOUND; 556 } 557 return (error); 558} 559 560/* 561 * Enforce single-threading. 562 * 563 * Returns 1 if the caller must abort (another thread is waiting to 564 * exit the process or similar). Process is locked! 565 * Returns 0 when you are successfully the only thread running. 566 * A process has successfully single threaded in the suspend mode when 567 * There are no threads in user mode. Threads in the kernel must be 568 * allowed to continue until they get to the user boundary. They may even 569 * copy out their return values and data before suspending. They may however be 570 * accellerated in reaching the user boundary as we will wake up 571 * any sleeping threads that are interruptable. (PCATCH). 572 */ 573int 574thread_single(int force_exit) 575{ 576 struct thread *td; 577 struct thread *td2; 578 struct proc *p; 579 580 td = curthread; 581 p = td->td_proc; 582 PROC_LOCK_ASSERT(p, MA_OWNED); 583 KASSERT((td != NULL), ("curthread is NULL")); 584 585 if ((p->p_flag & P_KSES) == 0) 586 return (0); 587 588 if (p->p_singlethread) { 589 /* 590 * Someone is already single threading! 591 */ 592 return (1); 593 } 594 595 if (force_exit == SNGLE_EXIT) 596 p->p_flag |= P_SINGLE_EXIT; 597 else 598 p->p_flag &= ~P_SINGLE_EXIT; 599 p->p_flag |= P_STOPPED_SNGL; 600 p->p_singlethread = td; 601 while ((p->p_numthreads - p->p_suspcount) != 1) { 602 FOREACH_THREAD_IN_PROC(p, td2) { 603 if (td2 == td) 604 continue; 605 switch(td2->td_state) { 606 case TDS_SUSPENDED: 607 if (force_exit == SNGLE_EXIT) { 608 TAILQ_REMOVE(&p->p_suspended, 609 td, td_runq); 610 setrunqueue(td); /* Should suicide. */ 611 } 612 case TDS_SLP: 613 if (td2->td_flags & TDF_CVWAITQ) { 614 cv_abort(td2); 615 } else { 616 abortsleep(td2); 617 } 618 break; 619 /* etc. XXXKSE */ 620 default: 621 ; 622 } 623 } 624 /* 625 * XXXKSE-- idea 626 * It's possible that we can just wake up when 627 * there are no runnable KSEs, because that would 628 * indicate that only this thread is runnable and 629 * there are no running KSEs in userland. 630 * -- 631 * Wake us up when everyone else has suspended. 632 * (or died) 633 */ 634 mtx_lock_spin(&sched_lock); 635 TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq); 636 td->td_state = TDS_SUSPENDED; 637 p->p_suspcount++; 638 mtx_unlock(&Giant); 639 PROC_UNLOCK(p); 640 mi_switch(); 641 mtx_unlock_spin(&sched_lock); 642 mtx_lock(&Giant); 643 PROC_LOCK(p); 644 } 645 return (0); 646} 647 648/* 649 * Called in from locations that can safely check to see 650 * whether we have to suspend or at least throttle for a 651 * single-thread event (e.g. fork). 652 * 653 * Such locations include userret(). 654 * If the "return_instead" argument is non zero, the thread must be able to 655 * accept 0 (caller may continue), or 1 (caller must abort) as a result. 656 * 657 * The 'return_instead' argument tells the function if it may do a 658 * thread_exit() or suspend, or whether the caller must abort and back 659 * out instead. 660 * 661 * If the thread that set the single_threading request has set the 662 * P_SINGLE_EXIT bit in the process flags then this call will never return 663 * if 'return_instead' is false, but will exit. 664 * 665 * P_SINGLE_EXIT | return_instead == 0| return_instead != 0 666 *---------------+--------------------+--------------------- 667 * 0 | returns 0 | returns 0 or 1 668 * | when ST ends | immediatly 669 *---------------+--------------------+--------------------- 670 * 1 | thread exits | returns 1 671 * | | immediatly 672 * 0 = thread_exit() or suspension ok, 673 * other = return error instead of stopping the thread. 674 * 675 * While a full suspension is under effect, even a single threading 676 * thread would be suspended if it made this call (but it shouldn't). 677 * This call should only be made from places where 678 * thread_exit() would be safe as that may be the outcome unless 679 * return_instead is set. 680 */ 681int 682thread_suspend_check(int return_instead) 683{ 684 struct thread *td = curthread; 685 struct proc *p = td->td_proc; 686 687 td = curthread; 688 p = td->td_proc; 689 PROC_LOCK_ASSERT(p, MA_OWNED); 690 while (P_SHOULDSTOP(p)) { 691 if (P_SHOULDSTOP(p) == P_STOPPED_SNGL) { 692 KASSERT(p->p_singlethread != NULL, 693 ("singlethread not set")); 694 695 /* 696 * The only suspension in action is 697 * a single-threading. Treat it ever 698 * so slightly different if it is 699 * in a special situation. 700 */ 701 if (p->p_singlethread == td) { 702 return (0); /* Exempt from stopping. */ 703 } 704 705 } 706 707 if (return_instead) { 708 return (1); 709 } 710 711 /* 712 * If the process is waiting for us to exit, 713 * this thread should just suicide. 714 * Assumes that P_SINGLE_EXIT implies P_STOPPED_SNGL. 715 */ 716 if ((p->p_flag & P_SINGLE_EXIT) && (p->p_singlethread != td)) { 717 mtx_lock_spin(&sched_lock); 718 while (mtx_owned(&Giant)) 719 mtx_unlock(&Giant); 720 thread_exit(); 721 } 722 723 /* 724 * When a thread suspends, it just 725 * moves to the processes's suspend queue 726 * and stays there. 727 * 728 * XXXKSE if TDF_BOUND is true 729 * it will not release it's KSE which might 730 * lead to deadlock if there are not enough KSEs 731 * to complete all waiting threads. 732 * Maybe be able to 'lend' it out again. 733 * (lent kse's can not go back to userland?) 734 * and can only be lent in STOPPED state. 735 */ 736 mtx_assert(&Giant, MA_NOTOWNED); 737 mtx_lock_spin(&sched_lock); 738 p->p_suspcount++; 739 td->td_state = TDS_SUSPENDED; 740 TAILQ_INSERT_TAIL(&p->p_suspended, td, td_runq); 741 PROC_UNLOCK(p); 742 mi_switch(); 743 mtx_unlock_spin(&sched_lock); 744 PROC_LOCK(p); 745 } 746 return (0); 747} 748 749/* 750 * Allow all threads blocked by single threading to continue running. 751 */ 752void 753thread_unsuspend(struct proc *p) 754{ 755 struct thread *td; 756 757 PROC_LOCK_ASSERT(p, MA_OWNED); 758 if (!P_SHOULDSTOP(p)) { 759 while (( td = TAILQ_FIRST(&p->p_suspended))) { 760 TAILQ_REMOVE(&p->p_suspended, td, td_runq); 761 p->p_suspcount--; 762 setrunqueue(td); 763 } 764 } else if ((P_SHOULDSTOP(p) == P_STOPPED_SNGL) && 765 (p->p_numthreads == p->p_suspcount)) { 766 /* 767 * Stopping everything also did the job for the single 768 * threading request. Now we've downgraded to single-threaded, 769 * let it continue. 770 */ 771 TAILQ_REMOVE(&p->p_suspended, p->p_singlethread, td_runq); 772 p->p_suspcount--; 773 setrunqueue(p->p_singlethread); 774 } 775} 776 777void 778thread_single_end(void) 779{ 780 struct thread *td; 781 struct proc *p; 782 783 td = curthread; 784 p = td->td_proc; 785 PROC_LOCK_ASSERT(p, MA_OWNED); 786 p->p_flag &= ~P_STOPPED_SNGL; 787 p->p_singlethread = NULL; 788 thread_unsuspend(p); 789} 790 791