171b3fa15SDavid Xu /*
271b3fa15SDavid Xu  * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
371b3fa15SDavid Xu  * All rights reserved.
471b3fa15SDavid Xu  *
571b3fa15SDavid Xu  * Redistribution and use in source and binary forms, with or without
671b3fa15SDavid Xu  * modification, are permitted provided that the following conditions
771b3fa15SDavid Xu  * are met:
871b3fa15SDavid Xu  * 1. Redistributions of source code must retain the above copyright
971b3fa15SDavid Xu  *    notice, this list of conditions and the following disclaimer.
1071b3fa15SDavid Xu  * 2. Redistributions in binary form must reproduce the above copyright
1171b3fa15SDavid Xu  *    notice, this list of conditions and the following disclaimer in the
1271b3fa15SDavid Xu  *    documentation and/or other materials provided with the distribution.
13d3b15642Szrj  * 3. Neither the name of the author nor the names of any co-contributors
1471b3fa15SDavid Xu  *    may be used to endorse or promote products derived from this software
1571b3fa15SDavid Xu  *    without specific prior written permission.
1671b3fa15SDavid Xu  *
1771b3fa15SDavid Xu  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
1871b3fa15SDavid Xu  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1971b3fa15SDavid Xu  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2071b3fa15SDavid Xu  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2171b3fa15SDavid Xu  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2271b3fa15SDavid Xu  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2371b3fa15SDavid Xu  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2471b3fa15SDavid Xu  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2571b3fa15SDavid Xu  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2671b3fa15SDavid Xu  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2771b3fa15SDavid Xu  * SUCH DAMAGE.
2871b3fa15SDavid Xu  *
2971b3fa15SDavid Xu  */
309e2ee207SJoerg Sonnenberger 
31fc71f871SDavid Xu #include "namespace.h"
329e2ee207SJoerg Sonnenberger #include <machine/tls.h>
3371b3fa15SDavid Xu #include <errno.h>
3471b3fa15SDavid Xu #include <pthread.h>
35fc71f871SDavid Xu #include <pthread_np.h>
36fc71f871SDavid Xu #include "un-namespace.h"
376be5672cSzrj 
3871b3fa15SDavid Xu #include "thr_private.h"
3971b3fa15SDavid Xu 
40*940be950Szrj static void resume_common(pthread_t thread);
4171b3fa15SDavid Xu 
4271b3fa15SDavid Xu /* Resume a thread: */
4371b3fa15SDavid Xu int
_pthread_resume_np(pthread_t thread)4471b3fa15SDavid Xu _pthread_resume_np(pthread_t thread)
4571b3fa15SDavid Xu {
46*940be950Szrj 	pthread_t curthread = tls_get_curthread();
4771b3fa15SDavid Xu 	int ret;
4871b3fa15SDavid Xu 
4971b3fa15SDavid Xu 	/* Add a reference to the thread: */
5071b3fa15SDavid Xu 	if ((ret = _thr_ref_add(curthread, thread, /*include dead*/0)) == 0) {
5171b3fa15SDavid Xu 		/* Lock the threads scheduling queue: */
5271b3fa15SDavid Xu 		THR_THREAD_LOCK(curthread, thread);
5371b3fa15SDavid Xu 		resume_common(thread);
5471b3fa15SDavid Xu 		THR_THREAD_UNLOCK(curthread, thread);
5571b3fa15SDavid Xu 		_thr_ref_delete(curthread, thread);
5671b3fa15SDavid Xu 	}
5771b3fa15SDavid Xu 	return (ret);
5871b3fa15SDavid Xu }
5971b3fa15SDavid Xu 
6071b3fa15SDavid Xu void
_pthread_resume_all_np(void)6171b3fa15SDavid Xu _pthread_resume_all_np(void)
6271b3fa15SDavid Xu {
63*940be950Szrj 	pthread_t curthread = tls_get_curthread();
64*940be950Szrj 	pthread_t thread;
6571b3fa15SDavid Xu 
6671b3fa15SDavid Xu 	/* Take the thread list lock: */
6771b3fa15SDavid Xu 	THREAD_LIST_LOCK(curthread);
6871b3fa15SDavid Xu 
6971b3fa15SDavid Xu 	TAILQ_FOREACH(thread, &_thread_list, tle) {
7071b3fa15SDavid Xu 		if (thread != curthread) {
7171b3fa15SDavid Xu 			THR_THREAD_LOCK(curthread, thread);
7271b3fa15SDavid Xu 			resume_common(thread);
7371b3fa15SDavid Xu 			THR_THREAD_UNLOCK(curthread, thread);
7471b3fa15SDavid Xu 		}
7571b3fa15SDavid Xu 	}
7671b3fa15SDavid Xu 
7771b3fa15SDavid Xu 	/* Release the thread list lock: */
7871b3fa15SDavid Xu 	THREAD_LIST_UNLOCK(curthread);
7971b3fa15SDavid Xu }
8071b3fa15SDavid Xu 
8171b3fa15SDavid Xu static void
resume_common(pthread_t thread)82*940be950Szrj resume_common(pthread_t thread)
8371b3fa15SDavid Xu {
8471b3fa15SDavid Xu 	/* Clear the suspend flag: */
8571b3fa15SDavid Xu 	thread->flags &= ~THR_FLAGS_NEED_SUSPEND;
8671b3fa15SDavid Xu 	thread->cycle++;
8771b3fa15SDavid Xu 	_thr_umtx_wake(&thread->cycle, 1);
8871b3fa15SDavid Xu }
895a1048c8SDavid Xu 
905a1048c8SDavid Xu __strong_reference(_pthread_resume_np, pthread_resume_np);
915a1048c8SDavid Xu __strong_reference(_pthread_resume_all_np, pthread_resume_all_np);
92