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$");
|
|
|
|
|
2006-07-13 06:26:43 +00:00
|
|
|
#include "opt_posix.h"
|
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>
|
2006-07-11 05:34:35 +00:00
|
|
|
#include <sys/rtprio.h>
|
|
|
|
#include <posix4/sched.h>
|
2006-07-13 06:26:43 +00:00
|
|
|
#include <posix4/posix4.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;
|
2004-09-07 06:33:39 +00:00
|
|
|
|
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,
|
2006-07-11 05:34:35 +00:00
|
|
|
int flags, struct thr_sched_param *sched);
|
2005-04-23 02:36:07 +00:00
|
|
|
|
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,
|
2006-07-11 05:34:35 +00:00
|
|
|
NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
|
2005-04-23 02:36:07 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_new(struct thread *td, struct thr_new_args *uap)
|
|
|
|
/* struct thr_param * */
|
|
|
|
{
|
|
|
|
struct thr_param param;
|
2006-07-11 05:34:35 +00:00
|
|
|
struct thr_sched_param sched_param, *sched;
|
2005-04-23 02:36:07 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
if (uap->param_size < sizeof(param))
|
|
|
|
return (EINVAL);
|
|
|
|
if ((error = copyin(uap->param, ¶m, sizeof(param))))
|
|
|
|
return (error);
|
2006-07-11 05:34:35 +00:00
|
|
|
sched = NULL;
|
2006-07-13 06:26:43 +00:00
|
|
|
if (param.sched_param != NULL) {
|
|
|
|
if (param.sched_param_size != sizeof(struct thr_sched_param))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
error = copyin(param.sched_param, &sched_param,
|
2006-07-11 05:34:35 +00:00
|
|
|
sizeof(sched_param));
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
sched = &sched_param;
|
|
|
|
}
|
|
|
|
|
2005-04-23 02:36:07 +00:00
|
|
|
error = create_thread(td, NULL, param.start_func, param.arg,
|
|
|
|
param.stack_base, param.stack_size, param.tls_base,
|
2006-07-11 05:34:35 +00:00
|
|
|
param.child_tid, param.parent_tid, param.flags,
|
|
|
|
sched);
|
2005-04-23 02:36:07 +00:00
|
|
|
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,
|
2006-07-11 05:34:35 +00:00
|
|
|
int flags, struct thr_sched_param *sched)
|
2005-04-23 02:36:07 +00:00
|
|
|
{
|
|
|
|
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;
|
2006-07-10 23:14:07 +00:00
|
|
|
int error;
|
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. */
|
2006-07-10 23:14:07 +00:00
|
|
|
if (p->p_numthreads >= max_threads_per_proc)
|
2004-09-05 02:09:54 +00:00
|
|
|
return (EPROCLIM);
|
2005-04-23 02:36:07 +00:00
|
|
|
|
2006-07-11 05:34:35 +00:00
|
|
|
if (sched != NULL) {
|
2006-07-11 08:19:57 +00:00
|
|
|
switch(sched->policy) {
|
|
|
|
case SCHED_FIFO:
|
|
|
|
case SCHED_RR:
|
|
|
|
/* Only root can set scheduler policy */
|
2006-07-11 05:34:35 +00:00
|
|
|
if (suser(td) != 0)
|
|
|
|
return (EPERM);
|
|
|
|
if (sched->param.sched_priority < RTP_PRIO_MIN ||
|
|
|
|
sched->param.sched_priority > RTP_PRIO_MAX)
|
|
|
|
return (EINVAL);
|
2006-07-11 08:19:57 +00:00
|
|
|
break;
|
|
|
|
case SCHED_OTHER:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
2006-07-11 05:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2006-07-10 23:14:07 +00:00
|
|
|
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);
|
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);
|
2006-07-10 23:14:07 +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. */
|
2006-07-10 23:14:07 +00:00
|
|
|
sched_fork_ksegrp(td, newkg);
|
2004-09-05 02:09:54 +00:00
|
|
|
sched_fork_thread(td, newtd);
|
2006-07-11 05:34:35 +00:00
|
|
|
if (sched != NULL) {
|
|
|
|
struct rtprio rtp;
|
|
|
|
switch (sched->policy) {
|
|
|
|
case SCHED_FIFO:
|
|
|
|
rtp.type = PRI_FIFO;
|
|
|
|
rtp.prio = sched->param.sched_priority;
|
2006-07-11 06:01:14 +00:00
|
|
|
rtp_to_pri(&rtp, newkg);
|
|
|
|
sched_prio(newtd, newkg->kg_user_pri);
|
2006-07-11 05:34:35 +00:00
|
|
|
break;
|
|
|
|
case SCHED_RR:
|
|
|
|
rtp.type = PRI_REALTIME;
|
|
|
|
rtp.prio = sched->param.sched_priority;
|
2006-07-11 06:01:14 +00:00
|
|
|
rtp_to_pri(&rtp, newkg);
|
|
|
|
sched_prio(newtd, newkg->kg_user_pri);
|
2006-07-11 05:34:35 +00:00
|
|
|
break;
|
|
|
|
case SCHED_OTHER:
|
2006-07-12 07:02:28 +00:00
|
|
|
if (newkg->kg_pri_class != PRI_TIMESHARE) {
|
2006-07-11 06:01:14 +00:00
|
|
|
rtp.type = PRI_TIMESHARE;
|
2006-07-11 05:34:35 +00:00
|
|
|
rtp.prio = 0;
|
2006-07-11 06:01:14 +00:00
|
|
|
rtp_to_pri(&rtp, newkg);
|
|
|
|
sched_prio(newtd, newkg->kg_user_pri);
|
|
|
|
}
|
2006-07-11 05:34:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
panic("sched policy");
|
|
|
|
}
|
|
|
|
}
|
2004-09-05 02:09:54 +00:00
|
|
|
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);
|
|
|
|
}
|
2006-07-13 06:26:43 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
thr_setscheduler(struct thread *td, struct thr_setscheduler_args *uap)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
struct thread *ttd;
|
|
|
|
struct rtprio rtp;
|
|
|
|
struct sched_param param;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (uap->param_size != sizeof(struct sched_param))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
ret = copyin(uap->param, ¶m, sizeof(struct sched_param));
|
|
|
|
if (ret)
|
|
|
|
return (ret);
|
|
|
|
|
|
|
|
switch(uap->policy) {
|
|
|
|
case SCHED_FIFO:
|
|
|
|
if (suser(td) != 0)
|
|
|
|
return (EPERM);
|
|
|
|
rtp.type = PRI_FIFO;
|
|
|
|
break;
|
|
|
|
case SCHED_RR:
|
|
|
|
if (suser(td) != 0)
|
|
|
|
return (EPERM);
|
|
|
|
rtp.type = PRI_REALTIME;
|
|
|
|
break;
|
|
|
|
case SCHED_OTHER:
|
|
|
|
rtp.type = PRI_TIMESHARE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
rtp.prio = param.sched_priority;
|
|
|
|
|
|
|
|
p = td->td_proc;
|
|
|
|
PROC_LOCK(p);
|
|
|
|
ret = p_cansched(td, p);
|
|
|
|
if (ret != 0) {
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
ttd = thread_find(p, uap->id);
|
|
|
|
if (ttd == NULL) {
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
ret = rtp_to_pri(&rtp, ttd->td_ksegrp);
|
|
|
|
if (ret == 0) {
|
|
|
|
if (TD_IS_RUNNING(ttd))
|
|
|
|
ttd->td_flags |= TDF_NEEDRESCHED;
|
|
|
|
else if (ttd->td_priority > ttd->td_ksegrp->kg_user_pri)
|
|
|
|
sched_prio(ttd, ttd->td_ksegrp->kg_user_pri);
|
|
|
|
}
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_getscheduler(struct thread *td, struct thr_getscheduler_args *uap)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
struct thread *ttd;
|
|
|
|
struct rtprio rtp;
|
|
|
|
struct sched_param param;
|
|
|
|
int policy;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (uap->param_size != sizeof(struct sched_param))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
p = td->td_proc;
|
|
|
|
PROC_LOCK(p);
|
|
|
|
ttd = thread_find(p, uap->id);
|
|
|
|
if (ttd == NULL) {
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
switch(ttd->td_ksegrp->kg_pri_class) {
|
|
|
|
case PRI_TIMESHARE:
|
|
|
|
policy = SCHED_OTHER;
|
|
|
|
break;
|
|
|
|
case PRI_FIFO:
|
|
|
|
policy = SCHED_FIFO;
|
|
|
|
break;
|
|
|
|
case PRI_REALTIME:
|
|
|
|
policy = SCHED_RR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
policy = SCHED_OTHER; /* XXX SCHED_IDLE */
|
|
|
|
}
|
|
|
|
pri_to_rtp(ttd->td_ksegrp, &rtp);
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
|
|
|
|
param.sched_priority = rtp.prio;
|
|
|
|
ret = copyout(&policy, uap->policy, sizeof(policy));
|
|
|
|
if (ret == 0)
|
|
|
|
ret = copyout(¶m, uap->param, sizeof(param));
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_setschedparam(struct thread *td, struct thr_setschedparam_args *uap)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
struct thread *ttd;
|
|
|
|
struct rtprio rtp;
|
|
|
|
struct sched_param param;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (uap->param_size != sizeof(struct sched_param))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
ret = copyin(uap->param, ¶m, sizeof(struct sched_param));
|
|
|
|
if (ret)
|
|
|
|
return (ret);
|
|
|
|
|
|
|
|
p = td->td_proc;
|
|
|
|
PROC_LOCK(p);
|
|
|
|
ret = p_cansched(td, p);
|
|
|
|
if (ret != 0) {
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
ttd = thread_find(p, uap->id);
|
|
|
|
if (ttd == NULL) {
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
pri_to_rtp(ttd->td_ksegrp, &rtp);
|
|
|
|
rtp.prio = param.sched_priority;
|
|
|
|
ret = rtp_to_pri(&rtp, ttd->td_ksegrp);
|
|
|
|
if (ret == 0) {
|
|
|
|
if (TD_IS_RUNNING(ttd))
|
|
|
|
ttd->td_flags |= TDF_NEEDRESCHED;
|
|
|
|
else if (ttd->td_priority > ttd->td_ksegrp->kg_user_pri)
|
|
|
|
sched_prio(ttd, ttd->td_ksegrp->kg_user_pri);
|
|
|
|
}
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (ret);
|
|
|
|
}
|