thr_suspend_np.c revision 174112
117706Sjulian/*
235509Sjb * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>.
317706Sjulian * All rights reserved.
417706Sjulian *
517706Sjulian * Redistribution and use in source and binary forms, with or without
617706Sjulian * modification, are permitted provided that the following conditions
717706Sjulian * are met:
817706Sjulian * 1. Redistributions of source code must retain the above copyright
917706Sjulian *    notice, this list of conditions and the following disclaimer.
1017706Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1117706Sjulian *    notice, this list of conditions and the following disclaimer in the
1217706Sjulian *    documentation and/or other materials provided with the distribution.
13165967Simp * 3. Neither the name of the author nor the names of any co-contributors
1417706Sjulian *    may be used to endorse or promote products derived from this software
1517706Sjulian *    without specific prior written permission.
1617706Sjulian *
1717706Sjulian * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
1817706Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1917706Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2049439Sdeischen * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2117706Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2217706Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2317706Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2417706Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2517706Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2617706Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2717706Sjulian * SUCH DAMAGE.
2817706Sjulian *
2950476Speter * $FreeBSD: head/lib/libkse/thread/thr_suspend_np.c 174112 2007-11-30 17:20:29Z deischen $
3017706Sjulian */
31174112Sdeischen
32174112Sdeischen#include "namespace.h"
3317706Sjulian#include <errno.h>
3417706Sjulian#include <pthread.h>
35174112Sdeischen#include "un-namespace.h"
36103388Smini#include "thr_private.h"
3717706Sjulian
38174112Sdeischenint	_pthread_suspend_np(pthread_t thread);
39174112Sdeischenvoid	_pthread_suspend_all_np(void);
40174112Sdeischen
41113658Sdeischenstatic void suspend_common(struct pthread *thread);
4258094Sdeischen
43156611SdeischenLT10_COMPAT_PRIVATE(_pthread_suspend_np);
44156611SdeischenLT10_COMPAT_DEFAULT(pthread_suspend_np);
45156611SdeischenLT10_COMPAT_PRIVATE(_pthread_suspend_all_np);
46156611SdeischenLT10_COMPAT_DEFAULT(pthread_suspend_all_np);
47156611Sdeischen
4875369Sdeischen__weak_reference(_pthread_suspend_np, pthread_suspend_np);
4997204Sdeischen__weak_reference(_pthread_suspend_all_np, pthread_suspend_all_np);
5071581Sdeischen
5135509Sjb/* Suspend a thread: */
5217706Sjulianint
5371581Sdeischen_pthread_suspend_np(pthread_t thread)
5417706Sjulian{
55113658Sdeischen	struct pthread *curthread = _get_curthread();
5635509Sjb	int ret;
5735509Sjb
5897204Sdeischen	/* Suspending the current thread doesn't make sense. */
5997204Sdeischen	if (thread == _get_curthread())
6097204Sdeischen		ret = EDEADLK;
6197204Sdeischen
62113658Sdeischen	/* Add a reference to the thread: */
63113658Sdeischen	else if ((ret = _thr_ref_add(curthread, thread, /*include dead*/0))
64113658Sdeischen	    == 0) {
65113658Sdeischen		/* Lock the threads scheduling queue: */
66113658Sdeischen		THR_SCHED_LOCK(curthread, thread);
6797204Sdeischen		suspend_common(thread);
68113658Sdeischen		/* Unlock the threads scheduling queue: */
69113658Sdeischen		THR_SCHED_UNLOCK(curthread, thread);
70113658Sdeischen
71113658Sdeischen		/* Don't forget to remove the reference: */
72113658Sdeischen		_thr_ref_delete(curthread, thread);
7317706Sjulian	}
7497204Sdeischen	return (ret);
7517706Sjulian}
7658094Sdeischen
7797204Sdeischenvoid
7897204Sdeischen_pthread_suspend_all_np(void)
7958094Sdeischen{
8071581Sdeischen	struct pthread	*curthread = _get_curthread();
8197204Sdeischen	struct pthread	*thread;
82113658Sdeischen	kse_critical_t crit;
8371581Sdeischen
84113658Sdeischen	/* Take the thread list lock: */
85113658Sdeischen	crit = _kse_critical_enter();
86113658Sdeischen	KSE_LOCK_ACQUIRE(curthread->kse, &_thread_list_lock);
8797204Sdeischen
8897204Sdeischen	TAILQ_FOREACH(thread, &_thread_list, tle) {
89113661Sdeischen		if (thread != curthread) {
90113658Sdeischen			THR_SCHED_LOCK(curthread, thread);
9197204Sdeischen			suspend_common(thread);
92113658Sdeischen			THR_SCHED_UNLOCK(curthread, thread);
93113658Sdeischen		}
9497204Sdeischen	}
9597204Sdeischen
96113658Sdeischen	/* Release the thread list lock: */
97113658Sdeischen	KSE_LOCK_RELEASE(curthread->kse, &_thread_list_lock);
98113658Sdeischen	_kse_critical_leave(crit);
9958094Sdeischen}
10058094Sdeischen
10197204Sdeischenvoid
10297204Sdeischensuspend_common(struct pthread *thread)
10397204Sdeischen{
104113661Sdeischen	if ((thread->state != PS_DEAD) &&
105113661Sdeischen	    (thread->state != PS_DEADLOCK) &&
106113661Sdeischen	    ((thread->flags & THR_FLAGS_EXITING) == 0)) {
107113661Sdeischen		thread->flags |= THR_FLAGS_SUSPENDED;
108114664Sdeischen		if ((thread->flags & THR_FLAGS_IN_RUNQ) != 0) {
109113661Sdeischen			THR_RUNQ_REMOVE(thread);
110114664Sdeischen			THR_SET_STATE(thread, PS_SUSPENDED);
111114664Sdeischen		}
112113786Sdeischen#ifdef NOT_YET
113113786Sdeischen		if ((thread->attr.flags & PTHREAD_SCOPE_SYSTEM) != 0)
114113786Sdeischen			/* ??? */
115113786Sdeischen#endif
11697204Sdeischen	}
11797204Sdeischen}
118