The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_thr.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice unmodified, this list of conditions, and the following
   10  *    disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/5.3/sys/kern/kern_thr.c 136588 2004-10-16 08:43:07Z cvs2svn $");
   29 
   30 #include <sys/param.h>
   31 #include <sys/kernel.h>
   32 #include <sys/lock.h>
   33 #include <sys/mutex.h>
   34 #include <sys/proc.h>
   35 #include <sys/resourcevar.h>
   36 #include <sys/sched.h>
   37 #include <sys/sysctl.h>
   38 #include <sys/smp.h>
   39 #include <sys/sysent.h>
   40 #include <sys/systm.h>
   41 #include <sys/sysproto.h>
   42 #include <sys/signalvar.h>
   43 #include <sys/ucontext.h>
   44 #include <sys/thr.h>
   45 
   46 #include <machine/frame.h>
   47 
   48 extern int max_threads_per_proc;
   49 extern int max_groups_per_proc;
   50 
   51 SYSCTL_DECL(_kern_threads);
   52 static int thr_scope_sys = 0;
   53 SYSCTL_INT(_kern_threads, OID_AUTO, thr_scope_sys, CTLFLAG_RW,
   54         &thr_scope_sys, 0, "sys or proc scope scheduling");
   55 
   56 static int thr_concurrency = 0;
   57 SYSCTL_INT(_kern_threads, OID_AUTO, thr_concurrency, CTLFLAG_RW,
   58         &thr_concurrency, 0, "a concurrency value if not default");
   59 
   60 /*
   61  * Back end support functions.
   62  */
   63 
   64 #define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
   65 
   66 /*
   67  * System call interface.
   68  */
   69 int
   70 thr_create(struct thread *td, struct thr_create_args *uap)
   71     /* ucontext_t *ctx, long *id, int flags */
   72 {
   73         struct thread *newtd;
   74         ucontext_t ctx;
   75         long id;
   76         int error;
   77         struct ksegrp *kg, *newkg;
   78         struct proc *p;
   79         int scope_sys;
   80 
   81         p = td->td_proc;
   82         kg = td->td_ksegrp;
   83         if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
   84                 return (error);
   85 
   86         /* Have race condition but it is cheap */
   87         if ((p->p_numksegrps >= max_groups_per_proc) ||
   88             (p->p_numthreads >= max_threads_per_proc)) {
   89                 return (EPROCLIM);
   90         }
   91 
   92         /*
   93          * Use a local copy to close a race against the user
   94          * changing thr_scope_sys.
   95          */
   96         scope_sys = thr_scope_sys;
   97 
   98         /* Initialize our td and new ksegrp.. */
   99         newtd = thread_alloc();
  100         if (scope_sys)
  101                 newkg = ksegrp_alloc();
  102         else
  103                 newkg = kg;
  104         /*
  105          * Try the copyout as soon as we allocate the td so we don't have to
  106          * tear things down in a failure case below.
  107          */
  108         id = newtd->td_tid;
  109         if ((error = copyout(&id, uap->id, sizeof(long)))) {
  110                 if (scope_sys)
  111                         ksegrp_free(newkg);
  112                 thread_free(newtd);
  113                 return (error);
  114         }
  115 
  116         bzero(&newtd->td_startzero,
  117             (unsigned) RANGEOF(struct thread, td_startzero, td_endzero));
  118         bcopy(&td->td_startcopy, &newtd->td_startcopy,
  119             (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy));
  120 
  121         if (scope_sys) {
  122                 bzero(&newkg->kg_startzero,
  123                     (unsigned)RANGEOF(struct ksegrp, kg_startzero, kg_endzero));
  124                 bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
  125                     (unsigned)RANGEOF(struct ksegrp, kg_startcopy, kg_endcopy));
  126         }
  127 
  128         newtd->td_proc = td->td_proc;
  129         newtd->td_ucred = crhold(td->td_ucred);
  130 
  131         /* Set up our machine context. */
  132         cpu_set_upcall(newtd, td);
  133         error = set_mcontext(newtd, &ctx.uc_mcontext);
  134         if (error != 0) {
  135                 if (scope_sys)
  136                         ksegrp_free(newkg);
  137                 thread_free(newtd);
  138                 crfree(td->td_ucred);
  139                 goto out;
  140         }
  141 
  142         /* Link the thread and kse into the ksegrp and make it runnable. */
  143         PROC_LOCK(td->td_proc);
  144         if (scope_sys) {
  145                         sched_init_concurrency(newkg);
  146         } else {
  147                 if ((td->td_proc->p_flag & P_HADTHREADS) == 0) {
  148                         sched_set_concurrency(kg,
  149                             thr_concurrency ? thr_concurrency : (2*mp_ncpus));
  150                 }
  151         }
  152                         
  153         td->td_proc->p_flag |= P_HADTHREADS; 
  154         newtd->td_sigmask = td->td_sigmask;
  155         mtx_lock_spin(&sched_lock);
  156         if (scope_sys)
  157                 ksegrp_link(newkg, p);
  158         thread_link(newtd, newkg);
  159         mtx_unlock_spin(&sched_lock);
  160         PROC_UNLOCK(p);
  161 
  162         /* let the scheduler know about these things. */
  163         mtx_lock_spin(&sched_lock);
  164         if (scope_sys)
  165                 sched_fork_ksegrp(td, newkg);
  166         sched_fork_thread(td, newtd);
  167 
  168         TD_SET_CAN_RUN(newtd);
  169         if ((uap->flags & THR_SUSPENDED) == 0)
  170                 setrunqueue(newtd, SRQ_BORING);
  171 
  172         mtx_unlock_spin(&sched_lock);
  173 
  174 out:
  175         return (error);
  176 }
  177 
  178 int
  179 thr_self(struct thread *td, struct thr_self_args *uap)
  180     /* long *id */
  181 {
  182         long id;
  183         int error;
  184 
  185         id = td->td_tid;
  186         if ((error = copyout(&id, uap->id, sizeof(long))))
  187                 return (error);
  188 
  189         return (0);
  190 }
  191 
  192 int
  193 thr_exit(struct thread *td, struct thr_exit_args *uap)
  194     /* long *state */
  195 {
  196         struct proc *p;
  197 
  198         p = td->td_proc;
  199 
  200         /* Signal userland that it can free the stack. */
  201         if ((void *)uap->state != NULL)
  202                 suword((void *)uap->state, 1);
  203 
  204         PROC_LOCK(p);
  205         mtx_lock_spin(&sched_lock);
  206 
  207         /*
  208          * Shutting down last thread in the proc.  This will actually
  209          * call exit() in the trampoline when it returns.
  210          */
  211         if (p->p_numthreads != 1) {
  212                 thread_exit();
  213                 /* NOTREACHED */
  214         }
  215         mtx_unlock_spin(&sched_lock);
  216         PROC_UNLOCK(p);
  217         return (0);
  218 }
  219 
  220 int
  221 thr_kill(struct thread *td, struct thr_kill_args *uap)
  222     /* long id, int sig */
  223 {
  224         struct thread *ttd;
  225         struct proc *p;
  226         int error;
  227 
  228         p = td->td_proc;
  229         error = 0;
  230         PROC_LOCK(p);
  231         FOREACH_THREAD_IN_PROC(p, ttd) {
  232                 if (ttd->td_tid == uap->id)
  233                         break;
  234         }
  235         if (ttd == NULL) {
  236                 error = ESRCH;
  237                 goto out;
  238         }
  239         if (uap->sig == 0)
  240                 goto out;
  241         if (!_SIG_VALID(uap->sig)) {
  242                 error = EINVAL;
  243                 goto out;
  244         }
  245         tdsignal(ttd, uap->sig, SIGTARGET_TD);
  246 out:
  247         PROC_UNLOCK(p);
  248         return (error);
  249 }
  250 
  251 int
  252 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
  253         /* const struct timespec *timeout */
  254 {
  255         struct timespec ts;
  256         struct timeval  tv;
  257         int error;
  258         int hz;
  259 
  260         hz = 0;
  261         error = 0;
  262         if (uap->timeout != NULL) {
  263                 error = copyin((const void *)uap->timeout, (void *)&ts,
  264                     sizeof(struct timespec));
  265                 if (error != 0)
  266                         return (error);
  267                 if (ts.tv_nsec < 0 || ts.tv_nsec > 1000000000)
  268                         return (EINVAL);
  269                 if (ts.tv_sec == 0 && ts.tv_nsec == 0)
  270                         return (ETIMEDOUT);
  271                 TIMESPEC_TO_TIMEVAL(&tv, &ts);
  272                 hz = tvtohz(&tv);
  273         }
  274         PROC_LOCK(td->td_proc);
  275         if ((td->td_flags & TDF_THRWAKEUP) == 0)
  276                 error = msleep((void *)td, &td->td_proc->p_mtx,
  277                     td->td_priority | PCATCH, "lthr", hz);
  278         mtx_lock_spin(&sched_lock);
  279         td->td_flags &= ~TDF_THRWAKEUP;
  280         mtx_unlock_spin(&sched_lock);
  281         PROC_UNLOCK(td->td_proc);
  282         return (error == EWOULDBLOCK ? ETIMEDOUT : error);
  283 }
  284 
  285 int
  286 thr_wake(struct thread *td, struct thr_wake_args *uap)
  287         /* long id */
  288 {
  289         struct thread *ttd;
  290 
  291         PROC_LOCK(td->td_proc);
  292         FOREACH_THREAD_IN_PROC(td->td_proc, ttd) {
  293                 if (ttd->td_tid == uap->id)
  294                         break;
  295         }
  296         if (ttd == NULL) {
  297                 PROC_UNLOCK(td->td_proc);
  298                 return (ESRCH);
  299         }
  300         mtx_lock_spin(&sched_lock);
  301         ttd->td_flags |= TDF_THRWAKEUP;
  302         mtx_unlock_spin(&sched_lock);
  303         wakeup_one((void *)ttd);
  304         PROC_UNLOCK(td->td_proc);
  305         return (0);
  306 }

Cache object: eea5a0b086553eab41ae8bb6e9680570


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.