1112918Sjeff/*
2112918Sjeff * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
3112918Sjeff * All rights reserved.
4112918Sjeff *
5112918Sjeff * Redistribution and use in source and binary forms, with or without
6112918Sjeff * modification, are permitted provided that the following conditions
7112918Sjeff * are met:
8112918Sjeff * 1. Redistributions of source code must retain the above copyright
9112918Sjeff *    notice, this list of conditions and the following disclaimer.
10112918Sjeff * 2. Redistributions in binary form must reproduce the above copyright
11112918Sjeff *    notice, this list of conditions and the following disclaimer in the
12112918Sjeff *    documentation and/or other materials provided with the distribution.
13165967Simp * 3. Neither the name of the author nor the names of any co-contributors
14112918Sjeff *    may be used to endorse or promote products derived from this software
15112918Sjeff *    without specific prior written permission.
16112918Sjeff *
17112918Sjeff * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18112918Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19112918Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20112918Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21112918Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22112918Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23112918Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24112918Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25112918Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26112918Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27112918Sjeff * SUCH DAMAGE.
28112918Sjeff *
29112918Sjeff * $FreeBSD$
30112918Sjeff */
31144518Sdavidxu
32157457Sdavidxu#include "namespace.h"
33112918Sjeff#include <errno.h>
34112918Sjeff#include <pthread.h>
35157457Sdavidxu#include <pthread_np.h>
36157457Sdavidxu#include "un-namespace.h"
37144518Sdavidxu
38112918Sjeff#include "thr_private.h"
39112918Sjeff
40112918Sjeff__weak_reference(_pthread_resume_np, pthread_resume_np);
41112918Sjeff__weak_reference(_pthread_resume_all_np, pthread_resume_all_np);
42112918Sjeff
43144518Sdavidxustatic void resume_common(struct pthread *thread);
44144518Sdavidxu
45112918Sjeff/* Resume a thread: */
46112918Sjeffint
47112918Sjeff_pthread_resume_np(pthread_t thread)
48112918Sjeff{
49144518Sdavidxu	struct pthread *curthread = _get_curthread();
50112918Sjeff	int ret;
51112918Sjeff
52144518Sdavidxu	/* Add a reference to the thread: */
53212536Sdavidxu	if ((ret = _thr_find_thread(curthread, thread, /*include dead*/0)) == 0) {
54144518Sdavidxu		/* Lock the threads scheduling queue: */
55144518Sdavidxu		resume_common(thread);
56144518Sdavidxu		THR_THREAD_UNLOCK(curthread, thread);
57112918Sjeff	}
58112918Sjeff	return (ret);
59112918Sjeff}
60112918Sjeff
61112918Sjeffvoid
62112918Sjeff_pthread_resume_all_np(void)
63112918Sjeff{
64144518Sdavidxu	struct pthread *curthread = _get_curthread();
65144518Sdavidxu	struct pthread *thread;
66112918Sjeff
67144518Sdavidxu	/* Take the thread list lock: */
68212536Sdavidxu	THREAD_LIST_RDLOCK(curthread);
69144518Sdavidxu
70112918Sjeff	TAILQ_FOREACH(thread, &_thread_list, tle) {
71144518Sdavidxu		if (thread != curthread) {
72144518Sdavidxu			THR_THREAD_LOCK(curthread, thread);
73112918Sjeff			resume_common(thread);
74144518Sdavidxu			THR_THREAD_UNLOCK(curthread, thread);
75144518Sdavidxu		}
76112918Sjeff	}
77144518Sdavidxu
78144518Sdavidxu	/* Release the thread list lock: */
79144518Sdavidxu	THREAD_LIST_UNLOCK(curthread);
80112918Sjeff}
81112918Sjeff
82112918Sjeffstatic void
83112918Sjeffresume_common(struct pthread *thread)
84112918Sjeff{
85144518Sdavidxu	/* Clear the suspend flag: */
86144518Sdavidxu	thread->flags &= ~THR_FLAGS_NEED_SUSPEND;
87144518Sdavidxu	thread->cycle++;
88178647Sdavidxu	_thr_umtx_wake(&thread->cycle, 1, 0);
89112918Sjeff}
90