2005-01-06 23:35:40 +00:00
|
|
|
/*-
|
2003-04-01 00:30:30 +00:00
|
|
|
* Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice unmodified, this list of conditions, and the following
|
|
|
|
* disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/resourcevar.h>
|
2003-04-11 19:24:37 +00:00
|
|
|
#include <sys/sched.h>
|
2004-09-07 06:33:39 +00:00
|
|
|
#include <sys/sysctl.h>
|
2004-09-05 02:09:54 +00:00
|
|
|
#include <sys/smp.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/sysent.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/sysproto.h>
|
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/ucontext.h>
|
|
|
|
#include <sys/thr.h>
|
2005-10-26 06:55:46 +00:00
|
|
|
#include <sys/umtx.h>
|
|
|
|
#include <sys/limits.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
|
|
|
|
#include <machine/frame.h>
|
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
extern int max_threads_per_proc;
|
|
|
|
extern int max_groups_per_proc;
|
|
|
|
|
2004-09-07 06:33:39 +00:00
|
|
|
SYSCTL_DECL(_kern_threads);
|
2005-04-23 02:36:07 +00:00
|
|
|
static int thr_scope = 0;
|
|
|
|
SYSCTL_INT(_kern_threads, OID_AUTO, thr_scope, CTLFLAG_RW,
|
|
|
|
&thr_scope, 0, "sys or proc scope scheduling");
|
2004-09-07 06:33:39 +00:00
|
|
|
|
2004-09-07 07:04:47 +00:00
|
|
|
static int thr_concurrency = 0;
|
2004-09-07 06:33:39 +00:00
|
|
|
SYSCTL_INT(_kern_threads, OID_AUTO, thr_concurrency, CTLFLAG_RW,
|
|
|
|
&thr_concurrency, 0, "a concurrency value if not default");
|
|
|
|
|
2005-04-23 02:36:07 +00:00
|
|
|
static int create_thread(struct thread *td, mcontext_t *ctx,
|
|
|
|
void (*start_func)(void *), void *arg,
|
|
|
|
char *stack_base, size_t stack_size,
|
|
|
|
char *tls_base,
|
|
|
|
long *child_tid, long *parent_tid,
|
|
|
|
int flags);
|
|
|
|
|
2003-04-01 00:30:30 +00:00
|
|
|
/*
|
|
|
|
* System call interface.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
thr_create(struct thread *td, struct thr_create_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* ucontext_t *ctx, long *id, int flags */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
|
|
|
ucontext_t ctx;
|
|
|
|
int error;
|
2005-04-23 02:36:07 +00:00
|
|
|
|
|
|
|
if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
|
|
|
|
NULL, 0, NULL, uap->id, NULL, uap->flags);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_new(struct thread *td, struct thr_new_args *uap)
|
|
|
|
/* struct thr_param * */
|
|
|
|
{
|
|
|
|
struct thr_param param;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (uap->param_size < sizeof(param))
|
|
|
|
return (EINVAL);
|
|
|
|
if ((error = copyin(uap->param, ¶m, sizeof(param))))
|
|
|
|
return (error);
|
|
|
|
error = create_thread(td, NULL, param.start_func, param.arg,
|
|
|
|
param.stack_base, param.stack_size, param.tls_base,
|
|
|
|
param.child_tid, param.parent_tid, param.flags);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
create_thread(struct thread *td, mcontext_t *ctx,
|
|
|
|
void (*start_func)(void *), void *arg,
|
|
|
|
char *stack_base, size_t stack_size,
|
|
|
|
char *tls_base,
|
|
|
|
long *child_tid, long *parent_tid,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
stack_t stack;
|
|
|
|
struct thread *newtd;
|
2004-09-05 02:09:54 +00:00
|
|
|
struct ksegrp *kg, *newkg;
|
|
|
|
struct proc *p;
|
2005-04-23 02:36:07 +00:00
|
|
|
long id;
|
|
|
|
int error, scope_sys, linkkg;
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2005-04-23 02:36:07 +00:00
|
|
|
error = 0;
|
2004-09-05 02:09:54 +00:00
|
|
|
p = td->td_proc;
|
|
|
|
kg = td->td_ksegrp;
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2005-04-23 02:36:07 +00:00
|
|
|
/* Have race condition but it is cheap. */
|
2004-09-05 02:09:54 +00:00
|
|
|
if ((p->p_numksegrps >= max_groups_per_proc) ||
|
|
|
|
(p->p_numthreads >= max_threads_per_proc)) {
|
|
|
|
return (EPROCLIM);
|
|
|
|
}
|
2004-10-06 02:29:19 +00:00
|
|
|
|
2005-04-23 02:36:07 +00:00
|
|
|
/* Check PTHREAD_SCOPE_SYSTEM */
|
|
|
|
scope_sys = (flags & THR_SYSTEM_SCOPE) != 0;
|
|
|
|
|
|
|
|
/* sysctl overrides user's flag */
|
|
|
|
if (thr_scope == 1)
|
|
|
|
scope_sys = 0;
|
|
|
|
else if (thr_scope == 2)
|
|
|
|
scope_sys = 1;
|
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
/* Initialize our td and new ksegrp.. */
|
|
|
|
newtd = thread_alloc();
|
2005-04-23 02:36:07 +00:00
|
|
|
|
2003-04-01 00:30:30 +00:00
|
|
|
/*
|
2005-04-23 02:36:07 +00:00
|
|
|
* Try the copyout as soon as we allocate the td so we don't
|
|
|
|
* have to tear things down in a failure case below.
|
|
|
|
* Here we copy out tid to two places, one for child and one
|
|
|
|
* for parent, because pthread can create a detached thread,
|
|
|
|
* if parent wants to safely access child tid, it has to provide
|
|
|
|
* its storage, because child thread may exit quickly and
|
|
|
|
* memory is freed before parent thread can access it.
|
2003-04-01 00:30:30 +00:00
|
|
|
*/
|
2004-09-05 02:09:54 +00:00
|
|
|
id = newtd->td_tid;
|
2005-04-23 02:36:07 +00:00
|
|
|
if ((child_tid != NULL &&
|
|
|
|
(error = copyout(&id, child_tid, sizeof(long)))) ||
|
|
|
|
(parent_tid != NULL &&
|
|
|
|
(error = copyout(&id, parent_tid, sizeof(long))))) {
|
|
|
|
thread_free(newtd);
|
2003-04-01 00:30:30 +00:00
|
|
|
return (error);
|
|
|
|
}
|
2004-09-05 02:09:54 +00:00
|
|
|
bzero(&newtd->td_startzero,
|
2004-11-20 23:00:59 +00:00
|
|
|
__rangeof(struct thread, td_startzero, td_endzero));
|
2004-09-05 02:09:54 +00:00
|
|
|
bcopy(&td->td_startcopy, &newtd->td_startcopy,
|
2004-11-20 23:00:59 +00:00
|
|
|
__rangeof(struct thread, td_startcopy, td_endcopy));
|
2004-09-05 02:09:54 +00:00
|
|
|
newtd->td_proc = td->td_proc;
|
|
|
|
newtd->td_ucred = crhold(td->td_ucred);
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
cpu_set_upcall(newtd, td);
|
2005-04-23 02:36:07 +00:00
|
|
|
|
|
|
|
if (ctx != NULL) { /* old way to set user context */
|
|
|
|
error = set_mcontext(newtd, ctx);
|
|
|
|
if (error != 0) {
|
|
|
|
thread_free(newtd);
|
|
|
|
crfree(td->td_ucred);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Set up our machine context. */
|
|
|
|
stack.ss_sp = stack_base;
|
|
|
|
stack.ss_size = stack_size;
|
|
|
|
/* Set upcall address to user thread entry function. */
|
|
|
|
cpu_set_upcall_kse(newtd, start_func, arg, &stack);
|
|
|
|
/* Setup user TLS address and TLS pointer register. */
|
2005-07-10 23:31:11 +00:00
|
|
|
error = cpu_set_user_tls(newtd, tls_base);
|
|
|
|
if (error != 0) {
|
|
|
|
thread_free(newtd);
|
|
|
|
crfree(td->td_ucred);
|
|
|
|
return (error);
|
|
|
|
}
|
2004-07-02 00:40:07 +00:00
|
|
|
}
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2005-04-23 02:36:07 +00:00
|
|
|
if ((td->td_proc->p_flag & P_HADTHREADS) == 0) {
|
|
|
|
/* Treat initial thread as it has PTHREAD_SCOPE_PROCESS. */
|
|
|
|
p->p_procscopegrp = kg;
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
sched_set_concurrency(kg,
|
|
|
|
thr_concurrency ? thr_concurrency : (2*mp_ncpus));
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
linkkg = 0;
|
2004-10-06 02:29:19 +00:00
|
|
|
if (scope_sys) {
|
2005-04-23 02:36:07 +00:00
|
|
|
linkkg = 1;
|
|
|
|
newkg = ksegrp_alloc();
|
|
|
|
bzero(&newkg->kg_startzero,
|
|
|
|
__rangeof(struct ksegrp, kg_startzero, kg_endzero));
|
|
|
|
bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
|
|
|
|
__rangeof(struct ksegrp, kg_startcopy, kg_endcopy));
|
|
|
|
sched_init_concurrency(newkg);
|
|
|
|
PROC_LOCK(td->td_proc);
|
2004-09-07 06:33:39 +00:00
|
|
|
} else {
|
2005-04-23 02:36:07 +00:00
|
|
|
/*
|
|
|
|
* Try to create a KSE group which will be shared
|
|
|
|
* by all PTHREAD_SCOPE_PROCESS threads.
|
|
|
|
*/
|
|
|
|
retry:
|
|
|
|
PROC_LOCK(td->td_proc);
|
|
|
|
if ((newkg = p->p_procscopegrp) == NULL) {
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
newkg = ksegrp_alloc();
|
|
|
|
bzero(&newkg->kg_startzero,
|
|
|
|
__rangeof(struct ksegrp, kg_startzero, kg_endzero));
|
|
|
|
bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
|
|
|
|
__rangeof(struct ksegrp, kg_startcopy, kg_endcopy));
|
|
|
|
PROC_LOCK(p);
|
|
|
|
if (p->p_procscopegrp == NULL) {
|
|
|
|
p->p_procscopegrp = newkg;
|
|
|
|
sched_init_concurrency(newkg);
|
|
|
|
sched_set_concurrency(newkg,
|
|
|
|
thr_concurrency ? thr_concurrency : (2*mp_ncpus));
|
|
|
|
linkkg = 1;
|
|
|
|
} else {
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
ksegrp_free(newkg);
|
|
|
|
goto retry;
|
|
|
|
}
|
2004-09-07 06:33:39 +00:00
|
|
|
}
|
|
|
|
}
|
2005-04-23 02:36:07 +00:00
|
|
|
|
|
|
|
td->td_proc->p_flag |= P_HADTHREADS;
|
2004-09-05 02:09:54 +00:00
|
|
|
newtd->td_sigmask = td->td_sigmask;
|
2003-04-01 00:30:30 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2005-04-23 02:36:07 +00:00
|
|
|
if (linkkg)
|
2004-09-07 06:33:39 +00:00
|
|
|
ksegrp_link(newkg, p);
|
2004-09-05 02:09:54 +00:00
|
|
|
thread_link(newtd, newkg);
|
|
|
|
PROC_UNLOCK(p);
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
/* let the scheduler know about these things. */
|
2005-04-23 02:36:07 +00:00
|
|
|
if (linkkg)
|
2004-09-07 06:33:39 +00:00
|
|
|
sched_fork_ksegrp(td, newkg);
|
2004-09-05 02:09:54 +00:00
|
|
|
sched_fork_thread(td, newtd);
|
|
|
|
TD_SET_CAN_RUN(newtd);
|
2005-04-23 02:36:07 +00:00
|
|
|
/* if ((flags & THR_SUSPENDED) == 0) */
|
2004-09-05 02:09:54 +00:00
|
|
|
setrunqueue(newtd, SRQ_BORING);
|
2003-04-01 00:30:30 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_self(struct thread *td, struct thr_self_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long *id */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
2004-07-02 00:40:07 +00:00
|
|
|
long id;
|
2003-04-01 00:30:30 +00:00
|
|
|
int error;
|
|
|
|
|
2004-07-02 00:40:07 +00:00
|
|
|
id = td->td_tid;
|
|
|
|
if ((error = copyout(&id, uap->id, sizeof(long))))
|
2003-04-01 00:30:30 +00:00
|
|
|
return (error);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_exit(struct thread *td, struct thr_exit_args *uap)
|
2004-10-06 14:23:00 +00:00
|
|
|
/* long *state */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
|
|
|
|
p = td->td_proc;
|
|
|
|
|
2004-10-06 14:23:00 +00:00
|
|
|
/* Signal userland that it can free the stack. */
|
2005-10-26 06:55:46 +00:00
|
|
|
if ((void *)uap->state != NULL) {
|
2004-10-06 14:23:00 +00:00
|
|
|
suword((void *)uap->state, 1);
|
2005-10-26 06:55:46 +00:00
|
|
|
kern_umtx_wake(td, uap->state, INT_MAX);
|
|
|
|
}
|
2004-10-06 14:23:00 +00:00
|
|
|
|
2003-04-01 00:30:30 +00:00
|
|
|
PROC_LOCK(p);
|
1. Change prototype of trapsignal and sendsig to use ksiginfo_t *, most
changes in MD code are trivial, before this change, trapsignal and
sendsig use discrete parameters, now they uses member fields of
ksiginfo_t structure. For sendsig, this change allows us to pass
POSIX realtime signal value to user code.
2. Remove cpu_thread_siginfo, it is no longer needed because we now always
generate ksiginfo_t data and feed it to libpthread.
3. Add p_sigqueue to proc structure to hold shared signals which were
blocked by all threads in the proc.
4. Add td_sigqueue to thread structure to hold all signals delivered to
thread.
5. i386 and amd64 now return POSIX standard si_code, other arches will
be fixed.
6. In this sigqueue implementation, pending signal set is kept as before,
an extra siginfo list holds additional siginfo_t data for signals.
kernel code uses psignal() still behavior as before, it won't be failed
even under memory pressure, only exception is when deleting a signal,
we should call sigqueue_delete to remove signal from sigqueue but
not SIGDELSET. Current there is no kernel code will deliver a signal
with additional data, so kernel should be as stable as before,
a ksiginfo can carry more information, for example, allow signal to
be delivered but throw away siginfo data if memory is not enough.
SIGKILL and SIGSTOP have fast path in sigqueue_add, because they can
not be caught or masked.
The sigqueue() syscall allows user code to queue a signal to target
process, if resource is unavailable, EAGAIN will be returned as
specification said.
Just before thread exits, signal queue memory will be freed by
sigqueue_flush.
Current, all signals are allowed to be queued, not only realtime signals.
Earlier patch reviewed by: jhb, deischen
Tested on: i386, amd64
2005-10-14 12:43:47 +00:00
|
|
|
sigqueue_flush(&td->td_sigqueue);
|
2003-04-01 00:30:30 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
|
|
|
|
/*
|
2004-09-05 02:09:54 +00:00
|
|
|
* Shutting down last thread in the proc. This will actually
|
|
|
|
* call exit() in the trampoline when it returns.
|
2003-04-01 00:30:30 +00:00
|
|
|
*/
|
2004-09-05 02:09:54 +00:00
|
|
|
if (p->p_numthreads != 1) {
|
2006-02-10 03:34:29 +00:00
|
|
|
thread_stopped(p);
|
2004-09-05 02:09:54 +00:00
|
|
|
thread_exit();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2003-04-01 00:30:30 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2004-09-05 02:09:54 +00:00
|
|
|
PROC_UNLOCK(p);
|
2003-04-01 00:30:30 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_kill(struct thread *td, struct thr_kill_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long id, int sig */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
|
|
|
struct thread *ttd;
|
|
|
|
struct proc *p;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
p = td->td_proc;
|
|
|
|
error = 0;
|
|
|
|
PROC_LOCK(p);
|
2006-01-07 03:15:21 +00:00
|
|
|
if (uap->id == -1) {
|
|
|
|
if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
|
|
|
|
error = EINVAL;
|
|
|
|
} else {
|
|
|
|
error = ESRCH;
|
|
|
|
FOREACH_THREAD_IN_PROC(p, ttd) {
|
|
|
|
if (ttd != td) {
|
|
|
|
error = 0;
|
|
|
|
if (uap->sig == 0)
|
|
|
|
break;
|
|
|
|
tdsignal(p, ttd, uap->sig, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (uap->id != td->td_tid)
|
|
|
|
ttd = thread_find(p, uap->id);
|
|
|
|
else
|
|
|
|
ttd = td;
|
|
|
|
if (ttd == NULL)
|
|
|
|
error = ESRCH;
|
|
|
|
else if (uap->sig == 0)
|
|
|
|
;
|
|
|
|
else if (!_SIG_VALID(uap->sig))
|
|
|
|
error = EINVAL;
|
|
|
|
else
|
|
|
|
tdsignal(p, ttd, uap->sig, NULL);
|
2003-04-01 00:30:30 +00:00
|
|
|
}
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (error);
|
|
|
|
}
|
2004-03-27 14:30:43 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
thr_suspend(struct thread *td, struct thr_suspend_args *uap)
|
|
|
|
/* const struct timespec *timeout */
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
struct timeval tv;
|
|
|
|
int error;
|
|
|
|
int hz;
|
|
|
|
|
|
|
|
hz = 0;
|
|
|
|
error = 0;
|
|
|
|
if (uap->timeout != NULL) {
|
|
|
|
error = copyin((const void *)uap->timeout, (void *)&ts,
|
|
|
|
sizeof(struct timespec));
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
if (ts.tv_nsec < 0 || ts.tv_nsec > 1000000000)
|
|
|
|
return (EINVAL);
|
|
|
|
if (ts.tv_sec == 0 && ts.tv_nsec == 0)
|
|
|
|
return (ETIMEDOUT);
|
|
|
|
TIMESPEC_TO_TIMEVAL(&tv, &ts);
|
|
|
|
hz = tvtohz(&tv);
|
|
|
|
}
|
|
|
|
PROC_LOCK(td->td_proc);
|
2004-07-12 15:28:31 +00:00
|
|
|
if ((td->td_flags & TDF_THRWAKEUP) == 0)
|
2006-04-17 18:20:38 +00:00
|
|
|
error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr",
|
|
|
|
hz);
|
2004-12-01 13:50:04 +00:00
|
|
|
if (td->td_flags & TDF_THRWAKEUP) {
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
td->td_flags &= ~TDF_THRWAKEUP;
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
PROC_UNLOCK(td->td_proc);
|
|
|
|
return (0);
|
|
|
|
}
|
2004-03-27 14:30:43 +00:00
|
|
|
PROC_UNLOCK(td->td_proc);
|
2004-12-01 13:50:04 +00:00
|
|
|
if (error == EWOULDBLOCK)
|
|
|
|
error = ETIMEDOUT;
|
|
|
|
else if (error == ERESTART) {
|
|
|
|
if (hz != 0)
|
|
|
|
error = EINTR;
|
|
|
|
}
|
|
|
|
return (error);
|
2004-03-27 14:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_wake(struct thread *td, struct thr_wake_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long id */
|
2004-03-27 14:30:43 +00:00
|
|
|
{
|
2005-11-03 01:34:08 +00:00
|
|
|
struct proc *p;
|
2004-07-02 00:40:07 +00:00
|
|
|
struct thread *ttd;
|
2004-03-27 14:30:43 +00:00
|
|
|
|
2005-11-03 01:34:08 +00:00
|
|
|
p = td->td_proc;
|
|
|
|
PROC_LOCK(p);
|
|
|
|
ttd = thread_find(p, uap->id);
|
2004-03-27 14:30:43 +00:00
|
|
|
if (ttd == NULL) {
|
2005-11-03 01:34:08 +00:00
|
|
|
PROC_UNLOCK(p);
|
2004-03-27 14:30:43 +00:00
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
mtx_lock_spin(&sched_lock);
|
2004-07-02 00:40:07 +00:00
|
|
|
ttd->td_flags |= TDF_THRWAKEUP;
|
2004-03-27 14:30:43 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2004-12-01 13:50:04 +00:00
|
|
|
wakeup((void *)ttd);
|
2005-11-03 01:34:08 +00:00
|
|
|
PROC_UNLOCK(p);
|
2004-03-27 14:30:43 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2006-02-05 02:18:46 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
thr_set_name(struct thread *td, struct thr_set_name_args *uap)
|
|
|
|
{
|
|
|
|
struct proc *p = td->td_proc;
|
|
|
|
char name[MAXCOMLEN + 1];
|
|
|
|
struct thread *ttd;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
name[0] = '\0';
|
|
|
|
if (uap->name != NULL) {
|
|
|
|
error = copyinstr(uap->name, name, sizeof(name),
|
|
|
|
NULL);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
PROC_LOCK(p);
|
|
|
|
if (uap->id == td->td_tid)
|
|
|
|
ttd = td;
|
|
|
|
else
|
|
|
|
ttd = thread_find(p, uap->id);
|
|
|
|
if (ttd != NULL)
|
|
|
|
strcpy(ttd->td_name, name);
|
|
|
|
else
|
|
|
|
error = ESRCH;
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (error);
|
|
|
|
}
|