* Find thread sleeping on a wait channel and resume it. */
| 895 | * Find thread sleeping on a wait channel and resume it. |
| 896 | */ |
| 897 | int |
| 898 | sleepq_signal(const void *wchan, int flags, int pri, int queue) |
| 899 | { |
| 900 | struct sleepqueue_chain *sc; |
| 901 | struct sleepqueue *sq; |
| 902 | struct threadqueue *head; |
| 903 | struct thread *td, *besttd; |
| 904 | int wakeup_swapper; |
| 905 | |
| 906 | CTR2(KTR_PROC, "sleepq_signal(%p, %d)", wchan, flags); |
| 907 | KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__)); |
| 908 | MPASS((queue >= 0) && (queue < NR_SLEEPQS)); |
| 909 | sq = sleepq_lookup(wchan); |
| 910 | if (sq == NULL) |
| 911 | return (0); |
| 912 | KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE), |
| 913 | ("%s: mismatch between sleep/wakeup and cv_*", __func__)); |
| 914 | |
| 915 | head = &sq->sq_blocked[queue]; |
| 916 | if (flags & SLEEPQ_UNFAIR) { |
| 917 | /* |
| 918 | * Find the most recently sleeping thread, but try to |
| 919 | * skip threads still in process of context switch to |
| 920 | * avoid spinning on the thread lock. |
| 921 | */ |
| 922 | sc = SC_LOOKUP(wchan); |
| 923 | besttd = TAILQ_LAST_FAST(head, thread, td_slpq); |
| 924 | while (besttd->td_lock != &sc->sc_lock) { |
| 925 | td = TAILQ_PREV_FAST(besttd, head, thread, td_slpq); |
| 926 | if (td == NULL) |
| 927 | break; |
| 928 | besttd = td; |
| 929 | } |
| 930 | } else { |
| 931 | /* |
| 932 | * Find the highest priority thread on the queue. If there |
| 933 | * is a tie, use the thread that first appears in the queue |
| 934 | * as it has been sleeping the longest since threads are |
| 935 | * always added to the tail of sleep queues. |
| 936 | */ |
| 937 | besttd = td = TAILQ_FIRST(head); |
| 938 | while ((td = TAILQ_NEXT(td, td_slpq)) != NULL) { |
| 939 | if (td->td_priority < besttd->td_priority) |
| 940 | besttd = td; |
| 941 | } |
| 942 | } |
| 943 | MPASS(besttd != NULL); |
| 944 | wakeup_swapper = sleepq_resume_thread(sq, besttd, pri, SRQ_HOLD); |
| 945 | return (wakeup_swapper); |
| 946 | } |
| 947 | |
| 948 | static bool |
| 949 | match_any(struct thread *td __unused) |
no test coverage detected