xref: /freebsd/lib/libthr/thread/thr_suspend_np.c (revision a9b764e218bfdbf5f746d64ff503348a2363db6e)
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 
79*a9b764e2SDavid Xu 	THREAD_LIST_RDLOCK(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) {
99bc414752SDavid Xu 				THREAD_LIST_UNLOCK(curthread);
100*a9b764e2SDavid Xu 				/* Can not suspend, try to wait */
101*a9b764e2SDavid Xu 				THR_REF_ADD(curthread, thread);
102bc414752SDavid Xu 				suspend_common(curthread, thread, 1);
103*a9b764e2SDavid Xu 				THR_REF_DEL(curthread, thread);
104*a9b764e2SDavid Xu 				_thr_try_gc(curthread, thread);
105*a9b764e2SDavid Xu 				/* thread lock released */
106*a9b764e2SDavid Xu 
107*a9b764e2SDavid Xu 				THREAD_LIST_RDLOCK(curthread);
108bc414752SDavid Xu 				/*
109bc414752SDavid Xu 				 * Because we were blocked, things may have
110bc414752SDavid Xu 				 * been changed, we have to restart the
111bc414752SDavid Xu 				 * process.
112bc414752SDavid Xu 				 */
113bc414752SDavid Xu 				goto restart;
114bc414752SDavid Xu 			}
115a091d823SDavid Xu 			THR_THREAD_UNLOCK(curthread, thread);
116a091d823SDavid Xu 		}
117a091d823SDavid Xu 	}
118a091d823SDavid Xu 
119a091d823SDavid Xu 	THREAD_LIST_UNLOCK(curthread);
120a091d823SDavid Xu }
121a091d823SDavid Xu 
122bc414752SDavid Xu static int
123bc414752SDavid Xu suspend_common(struct pthread *curthread, struct pthread *thread,
124bc414752SDavid Xu 	int waitok)
125a091d823SDavid Xu {
1266fdfcacbSDavid Xu 	long tmp;
127bc414752SDavid Xu 
128bc414752SDavid Xu 	while (thread->state != PS_DEAD &&
129bc414752SDavid Xu 	      !(thread->flags & THR_FLAGS_SUSPENDED)) {
130a091d823SDavid Xu 		thread->flags |= THR_FLAGS_NEED_SUSPEND;
131bab012a9SDavid Xu 		tmp = thread->cycle;
132a091d823SDavid Xu 		_thr_send_sig(thread, SIGCANCEL);
133*a9b764e2SDavid Xu 		THR_THREAD_UNLOCK(curthread, thread);
134bc414752SDavid Xu 		if (waitok) {
1358d6a11a0SDavid Xu 			_thr_umtx_wait_uint(&thread->cycle, tmp, NULL, 0);
136bc414752SDavid Xu 			THR_THREAD_LOCK(curthread, thread);
137bc414752SDavid Xu 		} else {
1380d29c148SDavid Xu 			THR_THREAD_LOCK(curthread, thread);
139bc414752SDavid Xu 			return (0);
140a091d823SDavid Xu 		}
141bb535300SJeff Roberson 	}
142bc414752SDavid Xu 
143bc414752SDavid Xu 	return (1);
144bc414752SDavid Xu }
145