xref: /freebsd/lib/libthr/thread/thr_resume_np.c (revision 83aafcdc88928c99e80b04ead23a156e235f9af4)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
4bb535300SJeff Roberson  * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
5bb535300SJeff Roberson  * All rights reserved.
6bb535300SJeff Roberson  *
7bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
8bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
9bb535300SJeff Roberson  * are met:
10bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
11bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer.
12bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
13bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
14bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
15fed32d75SWarner Losh  * 3. Neither the name of the author nor the names of any co-contributors
16bb535300SJeff Roberson  *    may be used to endorse or promote products derived from this software
17bb535300SJeff Roberson  *    without specific prior written permission.
18bb535300SJeff Roberson  *
19bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
20bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29bb535300SJeff Roberson  * SUCH DAMAGE.
30bb535300SJeff Roberson  */
31a091d823SDavid Xu 
3237a6356bSDavid Xu #include "namespace.h"
33bb535300SJeff Roberson #include <errno.h>
34bb535300SJeff Roberson #include <pthread.h>
3537a6356bSDavid Xu #include <pthread_np.h>
3637a6356bSDavid Xu #include "un-namespace.h"
37bb535300SJeff Roberson 
38a091d823SDavid Xu #include "thr_private.h"
39bb535300SJeff Roberson 
40bb535300SJeff Roberson __weak_reference(_pthread_resume_np, pthread_resume_np);
41*83aafcdcSKyle Evans __weak_reference(_thr_resume_all_np, pthread_resume_all_np);
42*83aafcdcSKyle Evans __weak_reference(_thr_resume_all_np, _pthread_resume_all_np);
43bb535300SJeff Roberson 
44a091d823SDavid Xu static void resume_common(struct pthread *thread);
45a091d823SDavid Xu 
46bb535300SJeff Roberson /* Resume a thread: */
47bb535300SJeff Roberson int
_pthread_resume_np(pthread_t thread)48bb535300SJeff Roberson _pthread_resume_np(pthread_t thread)
49bb535300SJeff Roberson {
50a091d823SDavid Xu 	struct pthread *curthread = _get_curthread();
51bb535300SJeff Roberson 	int ret;
52bb535300SJeff Roberson 
53a091d823SDavid Xu 	/* Add a reference to the thread: */
54a9b764e2SDavid Xu 	if ((ret = _thr_find_thread(curthread, thread, /*include dead*/0)) == 0) {
55a091d823SDavid Xu 		/* Lock the threads scheduling queue: */
56bb535300SJeff Roberson 		resume_common(thread);
57a091d823SDavid Xu 		THR_THREAD_UNLOCK(curthread, thread);
58bb535300SJeff Roberson 	}
59bb535300SJeff Roberson 	return (ret);
60bb535300SJeff Roberson }
61bb535300SJeff Roberson 
62bb535300SJeff Roberson void
_thr_resume_all_np(void)63*83aafcdcSKyle Evans _thr_resume_all_np(void)
64bb535300SJeff Roberson {
65a091d823SDavid Xu 	struct pthread *curthread = _get_curthread();
66bb535300SJeff Roberson 	struct pthread *thread;
67a7b84c65SDavid Xu 	int old_nocancel;
68bb535300SJeff Roberson 
69a7b84c65SDavid Xu 	old_nocancel = curthread->no_cancel;
70a7b84c65SDavid Xu 	curthread->no_cancel = 1;
71a7b84c65SDavid Xu 	_thr_suspend_all_lock(curthread);
72a091d823SDavid Xu 	/* Take the thread list lock: */
73a9b764e2SDavid Xu 	THREAD_LIST_RDLOCK(curthread);
74a091d823SDavid Xu 
75bb535300SJeff Roberson 	TAILQ_FOREACH(thread, &_thread_list, tle) {
76a091d823SDavid Xu 		if (thread != curthread) {
77a091d823SDavid Xu 			THR_THREAD_LOCK(curthread, thread);
78bb535300SJeff Roberson 			resume_common(thread);
79a091d823SDavid Xu 			THR_THREAD_UNLOCK(curthread, thread);
80f318a520SMike Makonnen 		}
81bb535300SJeff Roberson 	}
82bb535300SJeff Roberson 
83a091d823SDavid Xu 	/* Release the thread list lock: */
84a091d823SDavid Xu 	THREAD_LIST_UNLOCK(curthread);
85a7b84c65SDavid Xu 	_thr_suspend_all_unlock(curthread);
86a7b84c65SDavid Xu 	curthread->no_cancel = old_nocancel;
87a7b84c65SDavid Xu 	_thr_testcancel(curthread);
88a091d823SDavid Xu }
89a091d823SDavid Xu 
90bb535300SJeff Roberson static void
resume_common(struct pthread * thread)91bb535300SJeff Roberson resume_common(struct pthread *thread)
92bb535300SJeff Roberson {
93a091d823SDavid Xu 	/* Clear the suspend flag: */
94321e2a00SKonstantin Belousov 	thread->flags &= ~(THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED);
95a091d823SDavid Xu 	thread->cycle++;
968d6a11a0SDavid Xu 	_thr_umtx_wake(&thread->cycle, 1, 0);
97bb535300SJeff Roberson }
98