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$ 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 4375369Sdeischen__weak_reference(_pthread_suspend_np, pthread_suspend_np); 4497204Sdeischen__weak_reference(_pthread_suspend_all_np, pthread_suspend_all_np); 4571581Sdeischen 4635509Sjb/* Suspend a thread: */ 4717706Sjulianint 4871581Sdeischen_pthread_suspend_np(pthread_t thread) 4917706Sjulian{ 50113658Sdeischen struct pthread *curthread = _get_curthread(); 5135509Sjb int ret; 5235509Sjb 5397204Sdeischen /* Suspending the current thread doesn't make sense. */ 5497204Sdeischen if (thread == _get_curthread()) 5597204Sdeischen ret = EDEADLK; 5697204Sdeischen 57113658Sdeischen /* Add a reference to the thread: */ 58113658Sdeischen else if ((ret = _thr_ref_add(curthread, thread, /*include dead*/0)) 59113658Sdeischen == 0) { 60113658Sdeischen /* Lock the threads scheduling queue: */ 61113658Sdeischen THR_SCHED_LOCK(curthread, thread); 6297204Sdeischen suspend_common(thread); 63113658Sdeischen /* Unlock the threads scheduling queue: */ 64113658Sdeischen THR_SCHED_UNLOCK(curthread, thread); 65113658Sdeischen 66113658Sdeischen /* Don't forget to remove the reference: */ 67113658Sdeischen _thr_ref_delete(curthread, thread); 6817706Sjulian } 6997204Sdeischen return (ret); 7017706Sjulian} 7158094Sdeischen 7297204Sdeischenvoid 7397204Sdeischen_pthread_suspend_all_np(void) 7458094Sdeischen{ 7571581Sdeischen struct pthread *curthread = _get_curthread(); 7697204Sdeischen struct pthread *thread; 77113658Sdeischen kse_critical_t crit; 7871581Sdeischen 79113658Sdeischen /* Take the thread list lock: */ 80113658Sdeischen crit = _kse_critical_enter(); 81113658Sdeischen KSE_LOCK_ACQUIRE(curthread->kse, &_thread_list_lock); 8297204Sdeischen 8397204Sdeischen TAILQ_FOREACH(thread, &_thread_list, tle) { 84113661Sdeischen if (thread != curthread) { 85113658Sdeischen THR_SCHED_LOCK(curthread, thread); 8697204Sdeischen suspend_common(thread); 87113658Sdeischen THR_SCHED_UNLOCK(curthread, thread); 88113658Sdeischen } 8997204Sdeischen } 9097204Sdeischen 91113658Sdeischen /* Release the thread list lock: */ 92113658Sdeischen KSE_LOCK_RELEASE(curthread->kse, &_thread_list_lock); 93113658Sdeischen _kse_critical_leave(crit); 9458094Sdeischen} 9558094Sdeischen 9697204Sdeischenvoid 9797204Sdeischensuspend_common(struct pthread *thread) 9897204Sdeischen{ 99113661Sdeischen if ((thread->state != PS_DEAD) && 100113661Sdeischen (thread->state != PS_DEADLOCK) && 101113661Sdeischen ((thread->flags & THR_FLAGS_EXITING) == 0)) { 102113661Sdeischen thread->flags |= THR_FLAGS_SUSPENDED; 103114664Sdeischen if ((thread->flags & THR_FLAGS_IN_RUNQ) != 0) { 104113661Sdeischen THR_RUNQ_REMOVE(thread); 105114664Sdeischen THR_SET_STATE(thread, PS_SUSPENDED); 106114664Sdeischen } 107113786Sdeischen#ifdef NOT_YET 108113786Sdeischen if ((thread->attr.flags & PTHREAD_SCOPE_SYSTEM) != 0) 109113786Sdeischen /* ??? */ 110113786Sdeischen#endif 11197204Sdeischen } 11297204Sdeischen} 113