xref: /freebsd/lib/libthr/thread/thr_suspend_np.c (revision fed32d75449d7d05afdd7ddb7703bfa69a3027f3)
1bb535300SJeff Roberson /*
2bb535300SJeff Roberson  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>.
3bb535300SJeff Roberson  * All rights reserved.
4bb535300SJeff Roberson  *
5bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
6bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
7bb535300SJeff Roberson  * are met:
8bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
9bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer.
10bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
11bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
12bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
13fed32d75SWarner Losh  * 3. Neither the name of the author nor the names of any co-contributors
14bb535300SJeff Roberson  *    may be used to endorse or promote products derived from this software
15bb535300SJeff Roberson  *    without specific prior written permission.
16bb535300SJeff Roberson  *
17bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27bb535300SJeff Roberson  * SUCH DAMAGE.
28bb535300SJeff Roberson  *
29bb535300SJeff Roberson  * $FreeBSD$
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"
37a091d823SDavid Xu 
38bb535300SJeff Roberson #include "thr_private.h"
39bb535300SJeff Roberson 
40bc414752SDavid Xu static int suspend_common(struct pthread *, struct pthread *,
41bc414752SDavid Xu 		int);
42a091d823SDavid Xu 
43bb535300SJeff Roberson __weak_reference(_pthread_suspend_np, pthread_suspend_np);
44bb535300SJeff Roberson __weak_reference(_pthread_suspend_all_np, pthread_suspend_all_np);
45bb535300SJeff Roberson 
46bb535300SJeff Roberson /* Suspend a thread: */
47bb535300SJeff Roberson int
48bb535300SJeff Roberson _pthread_suspend_np(pthread_t thread)
49bb535300SJeff Roberson {
50a091d823SDavid Xu 	struct pthread *curthread = _get_curthread();
51a091d823SDavid Xu 	int ret;
52a091d823SDavid Xu 
53a091d823SDavid Xu 	/* Suspending the current thread doesn't make sense. */
54a091d823SDavid Xu 	if (thread == _get_curthread())
55a091d823SDavid Xu 		ret = EDEADLK;
56a091d823SDavid Xu 
57a091d823SDavid Xu 	/* Add a reference to the thread: */
58a091d823SDavid Xu 	else if ((ret = _thr_ref_add(curthread, thread, /*include dead*/0))
59a091d823SDavid Xu 	    == 0) {
60a091d823SDavid Xu 		/* Lock the threads scheduling queue: */
61a091d823SDavid Xu 		THR_THREAD_LOCK(curthread, thread);
62bc414752SDavid Xu 		suspend_common(curthread, thread, 1);
63a091d823SDavid Xu 		/* Unlock the threads scheduling queue: */
64a091d823SDavid Xu 		THR_THREAD_UNLOCK(curthread, thread);
65a091d823SDavid Xu 
66a091d823SDavid Xu 		/* Don't forget to remove the reference: */
67a091d823SDavid Xu 		_thr_ref_delete(curthread, thread);
68a091d823SDavid Xu 	}
69a091d823SDavid Xu 	return (ret);
70bb535300SJeff Roberson }
71bb535300SJeff Roberson 
72bb535300SJeff Roberson void
73bb535300SJeff Roberson _pthread_suspend_all_np(void)
74bb535300SJeff Roberson {
75a091d823SDavid Xu 	struct pthread *curthread = _get_curthread();
76a091d823SDavid Xu 	struct pthread *thread;
77bc414752SDavid Xu 	int ret;
78a091d823SDavid Xu 
79a091d823SDavid Xu 	THREAD_LIST_LOCK(curthread);
80a091d823SDavid Xu 
81a091d823SDavid Xu 	TAILQ_FOREACH(thread, &_thread_list, tle) {
82a091d823SDavid Xu 		if (thread != curthread) {
83a091d823SDavid Xu 			THR_THREAD_LOCK(curthread, thread);
840d29c148SDavid Xu 			if (thread->state != PS_DEAD &&
850d29c148SDavid Xu 	      		   !(thread->flags & THR_FLAGS_SUSPENDED))
860d29c148SDavid Xu 			    thread->flags |= THR_FLAGS_NEED_SUSPEND;
870d29c148SDavid Xu 			THR_THREAD_UNLOCK(curthread, thread);
880d29c148SDavid Xu 		}
890d29c148SDavid Xu 	}
900d29c148SDavid Xu 	thr_kill(-1, SIGCANCEL);
910d29c148SDavid Xu 
920d29c148SDavid Xu restart:
930d29c148SDavid Xu 	TAILQ_FOREACH(thread, &_thread_list, tle) {
940d29c148SDavid Xu 		if (thread != curthread) {
95bc414752SDavid Xu 			/* First try to suspend the thread without waiting */
960d29c148SDavid Xu 			THR_THREAD_LOCK(curthread, thread);
97bc414752SDavid Xu 			ret = suspend_common(curthread, thread, 0);
98bc414752SDavid Xu 			if (ret == 0) {
990d29c148SDavid Xu 				/* Can not suspend, try to wait */
100bc414752SDavid Xu 				thread->refcount++;
101bc414752SDavid Xu 				THREAD_LIST_UNLOCK(curthread);
102bc414752SDavid Xu 				suspend_common(curthread, thread, 1);
103bc414752SDavid Xu 				THR_THREAD_UNLOCK(curthread, thread);
1040d29c148SDavid Xu 				THREAD_LIST_LOCK(curthread);
1050d29c148SDavid Xu 				_thr_ref_delete_unlocked(curthread, thread);
106bc414752SDavid Xu 				/*
107bc414752SDavid Xu 				 * Because we were blocked, things may have
108bc414752SDavid Xu 				 * been changed, we have to restart the
109bc414752SDavid Xu 				 * process.
110bc414752SDavid Xu 				 */
111bc414752SDavid Xu 				goto restart;
112bc414752SDavid Xu 			}
113a091d823SDavid Xu 			THR_THREAD_UNLOCK(curthread, thread);
114a091d823SDavid Xu 		}
115a091d823SDavid Xu 	}
116a091d823SDavid Xu 
117a091d823SDavid Xu 	THREAD_LIST_UNLOCK(curthread);
118a091d823SDavid Xu }
119a091d823SDavid Xu 
120bc414752SDavid Xu static int
121bc414752SDavid Xu suspend_common(struct pthread *curthread, struct pthread *thread,
122bc414752SDavid Xu 	int waitok)
123a091d823SDavid Xu {
124bc414752SDavid Xu 	umtx_t tmp;
125bc414752SDavid Xu 
126bc414752SDavid Xu 	while (thread->state != PS_DEAD &&
127bc414752SDavid Xu 	      !(thread->flags & THR_FLAGS_SUSPENDED)) {
128a091d823SDavid Xu 		thread->flags |= THR_FLAGS_NEED_SUSPEND;
129bab012a9SDavid Xu 		tmp = thread->cycle;
1300d29c148SDavid Xu 		THR_THREAD_UNLOCK(curthread, thread);
131a091d823SDavid Xu 		_thr_send_sig(thread, SIGCANCEL);
132bc414752SDavid Xu 		if (waitok) {
133bc414752SDavid Xu 			_thr_umtx_wait(&thread->cycle, tmp, NULL);
134bc414752SDavid Xu 			THR_THREAD_LOCK(curthread, thread);
135bc414752SDavid Xu 		} else {
1360d29c148SDavid Xu 			THR_THREAD_LOCK(curthread, thread);
137bc414752SDavid Xu 			return (0);
138a091d823SDavid Xu 		}
139bb535300SJeff Roberson 	}
140bc414752SDavid Xu 
141bc414752SDavid Xu 	return (1);
142bc414752SDavid Xu }
143