xref: /freebsd/lib/libthr/thread/thr_suspend_np.c (revision a091d823ad89a36935ba6e5568c4720a35f9a99b)
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.
13bb535300SJeff Roberson  * 3. All advertising materials mentioning features or use of this software
14bb535300SJeff Roberson  *    must display the following acknowledgement:
15bb535300SJeff Roberson  *	This product includes software developed by John Birrell.
16bb535300SJeff Roberson  * 4. Neither the name of the author nor the names of any co-contributors
17bb535300SJeff Roberson  *    may be used to endorse or promote products derived from this software
18bb535300SJeff Roberson  *    without specific prior written permission.
19bb535300SJeff Roberson  *
20bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30bb535300SJeff Roberson  * SUCH DAMAGE.
31bb535300SJeff Roberson  *
32bb535300SJeff Roberson  * $FreeBSD$
33bb535300SJeff Roberson  */
34a091d823SDavid Xu 
35bb535300SJeff Roberson #include <errno.h>
36bb535300SJeff Roberson #include <pthread.h>
37a091d823SDavid Xu 
38bb535300SJeff Roberson #include "thr_private.h"
39bb535300SJeff Roberson 
40a091d823SDavid Xu static void suspend_common(struct pthread *thread);
41a091d823SDavid Xu 
42bb535300SJeff Roberson __weak_reference(_pthread_suspend_np, pthread_suspend_np);
43bb535300SJeff Roberson __weak_reference(_pthread_suspend_all_np, pthread_suspend_all_np);
44bb535300SJeff Roberson 
45bb535300SJeff Roberson /* Suspend a thread: */
46bb535300SJeff Roberson int
47bb535300SJeff Roberson _pthread_suspend_np(pthread_t thread)
48bb535300SJeff Roberson {
49a091d823SDavid Xu 	struct pthread *curthread = _get_curthread();
50a091d823SDavid Xu 	int ret;
51a091d823SDavid Xu 
52a091d823SDavid Xu 	/* Suspending the current thread doesn't make sense. */
53a091d823SDavid Xu 	if (thread == _get_curthread())
54a091d823SDavid Xu 		ret = EDEADLK;
55a091d823SDavid Xu 
56a091d823SDavid Xu 	/* Add a reference to the thread: */
57a091d823SDavid Xu 	else if ((ret = _thr_ref_add(curthread, thread, /*include dead*/0))
58a091d823SDavid Xu 	    == 0) {
59a091d823SDavid Xu 		/* Lock the threads scheduling queue: */
60a091d823SDavid Xu 		THR_THREAD_LOCK(curthread, thread);
61a091d823SDavid Xu 		suspend_common(thread);
62a091d823SDavid Xu 		/* Unlock the threads scheduling queue: */
63a091d823SDavid Xu 		THR_THREAD_UNLOCK(curthread, thread);
64a091d823SDavid Xu 
65a091d823SDavid Xu 		/* Don't forget to remove the reference: */
66a091d823SDavid Xu 		_thr_ref_delete(curthread, thread);
67a091d823SDavid Xu 	}
68a091d823SDavid Xu 	return (ret);
69bb535300SJeff Roberson }
70bb535300SJeff Roberson 
71bb535300SJeff Roberson void
72bb535300SJeff Roberson _pthread_suspend_all_np(void)
73bb535300SJeff Roberson {
74a091d823SDavid Xu 	struct pthread	*curthread = _get_curthread();
75a091d823SDavid Xu 	struct pthread	*thread;
76a091d823SDavid Xu 
77a091d823SDavid Xu 	/* Take the thread list lock: */
78a091d823SDavid Xu 	THREAD_LIST_LOCK(curthread);
79a091d823SDavid Xu 
80a091d823SDavid Xu 	TAILQ_FOREACH(thread, &_thread_list, tle) {
81a091d823SDavid Xu 		if (thread != curthread) {
82a091d823SDavid Xu 			THR_THREAD_LOCK(curthread, thread);
83a091d823SDavid Xu 			suspend_common(thread);
84a091d823SDavid Xu 			THR_THREAD_UNLOCK(curthread, thread);
85a091d823SDavid Xu 		}
86a091d823SDavid Xu 	}
87a091d823SDavid Xu 
88a091d823SDavid Xu 	/* Release the thread list lock: */
89a091d823SDavid Xu 	THREAD_LIST_UNLOCK(curthread);
90a091d823SDavid Xu }
91a091d823SDavid Xu 
92a091d823SDavid Xu static void
93a091d823SDavid Xu suspend_common(struct pthread *thread)
94a091d823SDavid Xu {
95a091d823SDavid Xu 	if (thread->state != PS_DEAD) {
96a091d823SDavid Xu 		thread->flags |= THR_FLAGS_NEED_SUSPEND;
97a091d823SDavid Xu 		_thr_send_sig(thread, SIGCANCEL);
98a091d823SDavid Xu 	}
99bb535300SJeff Roberson }
100