]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/kern_synch.c
xnu-2782.30.5.tar.gz
[apple/xnu.git] / bsd / kern / kern_synch.c
index 06f1d4591d7f54d43fd11a040a98707d989283e4..b1c4eda1c44664e73c1fa8493bbc287a1be7a476 100644 (file)
@@ -1,16 +1,19 @@
 /*
 /*
- * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
  *
  *
- * @APPLE_LICENSE_HEADER_START@
- * 
- * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * 
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
+ * 
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
  * 
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * 
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
@@ -20,7 +23,7 @@
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * 
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /* 
  * Mach Operating System
  */
 /* 
  * Mach Operating System
 
 #include <sys/param.h>
 #include <sys/systm.h>
 
 #include <sys/param.h>
 #include <sys/systm.h>
-#include <sys/proc.h>
+#include <sys/proc_internal.h>
 #include <sys/user.h>
 #include <sys/user.h>
-#include <sys/file.h>
+#include <sys/file_internal.h>
 #include <sys/vnode.h>
 #include <sys/kernel.h>
 #include <sys/vnode.h>
 #include <sys/kernel.h>
-#include <sys/buf.h>
 
 #include <machine/spl.h>
 
 
 #include <machine/spl.h>
 
 
 #include <kern/task.h>
 #include <mach/time_value.h>
 
 #include <kern/task.h>
 #include <mach/time_value.h>
+#include <kern/locks.h>
+
+#include <sys/systm.h>                 /* for unix_syscall_return() */
+#include <libkern/OSAtomic.h>
+
+extern void compute_averunnable(void *);       /* XXX */
+
 
 
-#if KTRACE
-#include <sys/uio.h>
-#include <sys/ktrace.h>
-#endif 
 
 static void
 
 static void
-_sleep_continue(void)
+_sleep_continue( __unused void *parameter, wait_result_t wresult)
 {
 {
-       register struct proc *p;
-       register thread_t thread = current_thread();
-       thread_act_t th_act;
+       struct proc *p = current_proc();
+       thread_t self  = current_thread();
        struct uthread * ut;
        int sig, catch;
        int error = 0;
        struct uthread * ut;
        int sig, catch;
        int error = 0;
+       int dropmutex, spinmutex;
 
 
-       th_act = current_act();
-       ut = get_bsdthread_info(th_act);
-       catch = ut->uu_pri & PCATCH;
-       p = current_proc();
+       ut = get_bsdthread_info(self);
+       catch     = ut->uu_pri & PCATCH;
+       dropmutex = ut->uu_pri & PDROP;
+       spinmutex = ut->uu_pri & PSPIN;
 
 
-#if FIXME  /* [ */
-       thread->wait_mesg = NULL;
-#endif  /* FIXME ] */
-       switch (get_thread_waitresult(thread)) {
+       switch (wresult) {
                case THREAD_TIMED_OUT:
                        error = EWOULDBLOCK;
                        break;
                case THREAD_TIMED_OUT:
                        error = EWOULDBLOCK;
                        break;
@@ -90,34 +92,38 @@ _sleep_continue(void)
                        /* else fall through */
                case THREAD_INTERRUPTED:
                        if (catch) {
                        /* else fall through */
                case THREAD_INTERRUPTED:
                        if (catch) {
-                               if (thread_should_abort(current_thread())) {
+                               if (thread_should_abort(self)) {
                                        error = EINTR;
                                } else if (SHOULDissignal(p,ut)) {
                                        error = EINTR;
                                } else if (SHOULDissignal(p,ut)) {
-                                       if (sig = CURSIG(p)) {
+                                       if ((sig = CURSIG(p)) != 0) {
                                                if (p->p_sigacts->ps_sigintr & sigmask(sig))
                                                        error = EINTR;
                                                else
                                                        error = ERESTART;
                                        }
                                                if (p->p_sigacts->ps_sigintr & sigmask(sig))
                                                        error = EINTR;
                                                else
                                                        error = ERESTART;
                                        }
-                                       if (thread_should_abort(current_thread())) {
+                                       if (thread_should_abort(self)) {
                                                error = EINTR;
                                        }
                                                error = EINTR;
                                        }
-                               }
+                               } else if( (ut->uu_flag & ( UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
+                                        /* due to thread cancel */
+                                        error = EINTR;
+                                }
                        }  else
                                error = EINTR;
                        break;
        }
 
        if (error == EINTR || error == ERESTART)
                        }  else
                                error = EINTR;
                        break;
        }
 
        if (error == EINTR || error == ERESTART)
-               act_set_astbsd(th_act);
-
-       if (ut->uu_timo)
-               thread_cancel_timer();
+               act_set_astbsd(self);
 
 
-#if KTRACE
-       if (KTRPOINT(p, KTR_CSW))
-               ktrcsw(p->p_tracep, 0, 0, -1);
-#endif
+       if (ut->uu_mtx && !dropmutex) {
+               if (spinmutex)
+                       lck_mtx_lock_spin(ut->uu_mtx);
+               else
+                       lck_mtx_lock(ut->uu_mtx);
+       }
+       ut->uu_wchan = NULL;
+       ut->uu_wmesg = NULL;
 
        unix_syscall_return((*ut->uu_continuation)(error));
 }
 
        unix_syscall_return((*ut->uu_continuation)(error));
 }
@@ -134,108 +140,126 @@ _sleep_continue(void)
  * Callers of this routine must be prepared for
  * premature return, and check that the reason for
  * sleeping has gone away.
  * Callers of this routine must be prepared for
  * premature return, and check that the reason for
  * sleeping has gone away.
+ *
+ * if msleep was the entry point, than we have a mutex to deal with
+ *
+ * The mutex is unlocked before the caller is blocked, and
+ * relocked before msleep returns unless the priority includes the PDROP
+ * flag... if PDROP is specified, _sleep returns with the mutex unlocked
+ * regardless of whether it actually blocked or not.
  */
 
 static int
 _sleep(
        caddr_t         chan,
  */
 
 static int
 _sleep(
        caddr_t         chan,
-       int                     pri,
-       char            *wmsg,
+       int             pri,
+       const char      *wmsg,
        u_int64_t       abstime,
        u_int64_t       abstime,
-       int                     (*continuation)(int))
+       int             (*continuation)(int),
+        lck_mtx_t      *mtx)
 {
 {
-       register struct proc *p;
-       register thread_t thread = current_thread();
-       thread_act_t th_act;
+       struct proc *p;
+       thread_t self = current_thread();
        struct uthread * ut;
        struct uthread * ut;
-       int sig, catch = pri & PCATCH;
-       int sigttblock = pri & PTTYBLOCK;
+       int sig, catch;
+       int dropmutex  = pri & PDROP;
+       int spinmutex  = pri & PSPIN;
        int wait_result;
        int error = 0;
        int wait_result;
        int error = 0;
-       spl_t   s;
 
 
-       s = splhigh();
+       ut = get_bsdthread_info(self);
 
 
-       th_act = current_act();
-       ut = get_bsdthread_info(th_act);
-       
        p = current_proc();
        p = current_proc();
-#if KTRACE
-       if (KTRPOINT(p, KTR_CSW))
-               ktrcsw(p->p_tracep, 1, 0, -1);
-#endif 
        p->p_priority = pri & PRIMASK;
        p->p_priority = pri & PRIMASK;
-               
-       if (chan)
-               wait_result = assert_wait(chan,
-                                                                 (catch) ? THREAD_ABORTSAFE : THREAD_UNINT);
-
-       if (abstime)
-               thread_set_timer_deadline(abstime);
-
-       /*
-        * We start our timeout
-        * before calling CURSIG, as we could stop there, and a wakeup
-        * or a SIGCONT (or both) could occur while we were stopped.
-        * A SIGCONT would cause us to be marked as SSLEEP
-        * without resuming us, thus we must be ready for sleep
-        * when CURSIG is called.  If the wakeup happens while we're
-        * stopped, p->p_wchan will be 0 upon return from CURSIG.
-        */
-       if (catch) {
-               if (SHOULDissignal(p,ut)) {
-                       if (sig = CURSIG(p)) {
-                               clear_wait(thread, THREAD_INTERRUPTED);
-                               /* if SIGTTOU or SIGTTIN then block till SIGCONT */
-                               if (sigttblock && ((sig == SIGTTOU) || (sig == SIGTTIN))) {
-                                       p->p_flag |= P_TTYSLEEP;
-                                       /* reset signal bits */
-                                       clear_procsiglist(p, sig);
-                                       assert_wait(&p->p_siglist, THREAD_ABORTSAFE);
-                                       /* assert wait can block and SIGCONT should be checked */
-                                       if (p->p_flag & P_TTYSLEEP)
-                                               thread_block(THREAD_CONTINUE_NULL);
-                                       /* return with success */
-                                       error = 0;
+       /* It can still block in proc_exit() after the teardown. */
+       if (p->p_stats != NULL)
+               OSIncrementAtomicLong(&p->p_stats->p_ru.ru_nvcsw);
+       
+       if (pri & PCATCH)
+               catch = THREAD_ABORTSAFE;
+       else
+               catch = THREAD_UNINT;
+
+       /* set wait message & channel */
+       ut->uu_wchan = chan;
+       ut->uu_wmesg = wmsg ? wmsg : "unknown";
+
+       if (mtx != NULL && chan != NULL && (thread_continue_t)continuation == THREAD_CONTINUE_NULL) {
+               int     flags;
+
+               if (dropmutex)
+                       flags = LCK_SLEEP_UNLOCK;
+               else
+                       flags = LCK_SLEEP_DEFAULT;
+
+               if (spinmutex)
+                       flags |= LCK_SLEEP_SPIN;
+
+               if (abstime)
+                       wait_result = lck_mtx_sleep_deadline(mtx, flags, chan, catch, abstime);
+               else
+                       wait_result = lck_mtx_sleep(mtx, flags, chan, catch);
+       }
+       else {
+               if (chan != NULL)
+                       assert_wait_deadline(chan, catch, abstime);
+               if (mtx)
+                       lck_mtx_unlock(mtx);
+
+               if (catch == THREAD_ABORTSAFE) {
+                       if (SHOULDissignal(p,ut)) {
+                               if ((sig = CURSIG(p)) != 0) {
+                                       if (clear_wait(self, THREAD_INTERRUPTED) == KERN_FAILURE)
+                                               goto block;
+                                       if (p->p_sigacts->ps_sigintr & sigmask(sig))
+                                               error = EINTR;
+                                       else
+                                               error = ERESTART;
+                                       if (mtx && !dropmutex) {
+                                               if (spinmutex)
+                                                       lck_mtx_lock_spin(mtx);
+                                               else
+                                                       lck_mtx_lock(mtx);
+                                       }
                                        goto out;
                                }
                                        goto out;
                                }
-                               if (p->p_sigacts->ps_sigintr & sigmask(sig))
-                                       error = EINTR;
-                               else
-                                       error = ERESTART;
+                       }
+                       if (thread_should_abort(self)) {
+                               if (clear_wait(self, THREAD_INTERRUPTED) == KERN_FAILURE)
+                                       goto block;
+                               error = EINTR;
+
+                               if (mtx && !dropmutex) {
+                                       if (spinmutex)
+                                               lck_mtx_lock_spin(mtx);
+                                       else
+                                               lck_mtx_lock(mtx);
+                               }
                                goto out;
                        }
                                goto out;
                        }
+               }               
+
+
+block:
+               if ((thread_continue_t)continuation != THREAD_CONTINUE_NULL) {
+                       ut->uu_continuation = continuation;
+                       ut->uu_pri  = pri;
+                       ut->uu_timo = abstime? 1: 0;
+                       ut->uu_mtx  = mtx;
+                       (void) thread_block(_sleep_continue);
+                       /* NOTREACHED */
                }
                }
-               if (thread_should_abort(current_thread())) {
-                       clear_wait(thread, THREAD_INTERRUPTED);
-                       error = EINTR;
-                       goto out;
-               }
-               if (get_thread_waitresult(thread) != THREAD_WAITING) {
-                       /*already happened */
-                       goto out;
-               }
-       }
+               
+               wait_result = thread_block(THREAD_CONTINUE_NULL);
 
 
-#if FIXME  /* [ */
-       thread->wait_mesg = wmsg;
-#endif  /* FIXME ] */
-       splx(s);
-       p->p_stats->p_ru.ru_nvcsw++;
-
-       if (continuation != THREAD_CONTINUE_NULL ) {
-         ut->uu_continuation = continuation;
-         ut->uu_pri = pri;
-         ut->uu_timo = abstime? 1: 0;
-         (void) thread_block(_sleep_continue);
-         /* NOTREACHED */
+               if (mtx && !dropmutex) {
+                       if (spinmutex)
+                               lck_mtx_lock_spin(mtx);
+                       else
+                               lck_mtx_lock(mtx);
+               }
        }
 
        }
 
-       wait_result = thread_block(THREAD_CONTINUE_NULL);
-
-#if FIXME  /* [ */
-       thread->wait_mesg = NULL;
-#endif  /* FIXME ] */
        switch (wait_result) {
                case THREAD_TIMED_OUT:
                        error = EWOULDBLOCK;
        switch (wait_result) {
                case THREAD_TIMED_OUT:
                        error = EWOULDBLOCK;
@@ -246,38 +270,37 @@ _sleep(
                         * first, regardless of whether awakened due
                         * to receiving event.
                         */
                         * first, regardless of whether awakened due
                         * to receiving event.
                         */
-                       if (!catch)
+                       if (catch != THREAD_ABORTSAFE)
                                break;
                        /* else fall through */
                case THREAD_INTERRUPTED:
                                break;
                        /* else fall through */
                case THREAD_INTERRUPTED:
-                       if (catch) {
-                               if (thread_should_abort(current_thread())) {
+                       if (catch == THREAD_ABORTSAFE) {
+                               if (thread_should_abort(self)) {
                                        error = EINTR;
                                        error = EINTR;
-                               } else if (SHOULDissignal(p,ut)) {
-                                       if (sig = CURSIG(p)) {
+                               } else if (SHOULDissignal(p, ut)) {
+                                       if ((sig = CURSIG(p)) != 0) {
                                                if (p->p_sigacts->ps_sigintr & sigmask(sig))
                                                        error = EINTR;
                                                else
                                                        error = ERESTART;
                                        }
                                                if (p->p_sigacts->ps_sigintr & sigmask(sig))
                                                        error = EINTR;
                                                else
                                                        error = ERESTART;
                                        }
-                                       if (thread_should_abort(current_thread())) {
+                                       if (thread_should_abort(self)) {
                                                error = EINTR;
                                        }
                                                error = EINTR;
                                        }
-                               }
+                               } else if( (ut->uu_flag & ( UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
+                                        /* due to thread cancel */
+                                        error = EINTR;
+                                }
                        }  else
                                error = EINTR;
                        break;
        }
 out:
        if (error == EINTR || error == ERESTART)
                        }  else
                                error = EINTR;
                        break;
        }
 out:
        if (error == EINTR || error == ERESTART)
-               act_set_astbsd(th_act);
-       if (abstime)
-               thread_cancel_timer();
-       (void) splx(s);
-#if KTRACE
-       if (KTRPOINT(p, KTR_CSW))
-               ktrcsw(p->p_tracep, 0, 0, -1);
-#endif
+               act_set_astbsd(self);
+       ut->uu_wchan = NULL;
+       ut->uu_wmesg = NULL;
+
        return (error);
 }
 
        return (error);
 }
 
@@ -286,28 +309,74 @@ sleep(
        void    *chan,
        int             pri)
 {
        void    *chan,
        int             pri)
 {
-       return _sleep((caddr_t)chan, pri, (char *)NULL, 0, (int (*)(int))0);
+       return _sleep((caddr_t)chan, pri, (char *)NULL, 0, (int (*)(int))0, (lck_mtx_t *)0);
+}
+
+int
+msleep0(
+       void            *chan,
+       lck_mtx_t       *mtx,
+       int             pri,
+       const char      *wmsg,
+       int             timo,
+       int             (*continuation)(int))
+{
+       u_int64_t       abstime = 0;
+
+       if (timo)
+               clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime);
+
+       return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, mtx);
+}
+
+int
+msleep(
+       void            *chan,
+       lck_mtx_t       *mtx,
+       int             pri,
+       const char      *wmsg,
+       struct timespec         *ts)
+{
+       u_int64_t       abstime = 0;
+
+       if (ts && (ts->tv_sec || ts->tv_nsec)) {
+               nanoseconds_to_absolutetime((uint64_t)ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec,  &abstime );
+               clock_absolutetime_interval_to_deadline( abstime, &abstime );
+       }
+
+       return _sleep((caddr_t)chan, pri, wmsg, abstime, (int (*)(int))0, mtx);
+}
+
+int
+msleep1(
+       void            *chan,
+       lck_mtx_t       *mtx,
+       int             pri,
+       const char      *wmsg,
+       u_int64_t       abstime)
+{
+       return _sleep((caddr_t)chan, pri, wmsg, abstime, (int (*)(int))0, mtx);
 }
 
 int
 tsleep(
 }
 
 int
 tsleep(
-       void    *chan,
+       void            *chan,
        int             pri,
        int             pri,
-       char    *wmsg,
+       const char      *wmsg,
        int             timo)
 {
        u_int64_t       abstime = 0;
 
        if (timo)
                clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime);
        int             timo)
 {
        u_int64_t       abstime = 0;
 
        if (timo)
                clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime);
-       return _sleep((caddr_t)chan, pri, wmsg, abstime, (int (*)(int))0);
+       return _sleep((caddr_t)chan, pri, wmsg, abstime, (int (*)(int))0, (lck_mtx_t *)0);
 }
 
 int
 tsleep0(
 }
 
 int
 tsleep0(
-       void    *chan,
+       void            *chan,
        int             pri,
        int             pri,
-       char    *wmsg,
+       const char      *wmsg,
        int             timo,
        int             (*continuation)(int))
 {                      
        int             timo,
        int             (*continuation)(int))
 {                      
@@ -315,28 +384,27 @@ tsleep0(
 
        if (timo)
                clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime);
 
        if (timo)
                clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime);
-       return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation);
+       return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, (lck_mtx_t *)0);
 }
 
 int
 tsleep1(
        void            *chan,
 }
 
 int
 tsleep1(
        void            *chan,
-       int                     pri,
-       char            *wmsg,
+       int             pri,
+       const char      *wmsg,
        u_int64_t       abstime,
        u_int64_t       abstime,
-       int                     (*continuation)(int))
+       int             (*continuation)(int))
 {                      
 {                      
-       return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation);
+       return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, (lck_mtx_t *)0);
 }
 
 /*
  * Wake up all processes sleeping on chan.
  */
 void
 }
 
 /*
  * Wake up all processes sleeping on chan.
  */
 void
-wakeup(chan)
-       register void *chan;
+wakeup(void *chan)
 {
 {
-       thread_wakeup_prim((caddr_t)chan, FALSE, THREAD_AWAKENED);
+       thread_wakeup((caddr_t)chan);
 }
 
 /*
 }
 
 /*
@@ -346,10 +414,9 @@ wakeup(chan)
  * the right one to wakeup.
  */
 void
  * the right one to wakeup.
  */
 void
-wakeup_one(chan)
-       register caddr_t chan;
+wakeup_one(caddr_t chan)
 {
 {
-       thread_wakeup_prim((caddr_t)chan, TRUE, THREAD_AWAKENED);
+       thread_wakeup_one((caddr_t)chan);
 }
 
 /*
 }
 
 /*
@@ -358,8 +425,7 @@ wakeup_one(chan)
  * than that of the current process.
  */
 void
  * than that of the current process.
  */
 void
-resetpriority(p)
-       register struct proc *p;
+resetpriority(struct proc *p)
 {
        (void)task_importance(p->task, -p->p_nice);
 }
 {
        (void)task_importance(p->task, -p->p_nice);
 }
@@ -377,11 +443,11 @@ static fixpt_t cexp[3] = {
 };
 
 void
 };
 
 void
-compute_averunnable(
-       register int    nrun)
+compute_averunnable(void *arg)
 {
 {
-       register int            i;
+       unsigned int            nrun = *(unsigned int *)arg;
        struct loadavg          *avg = &averunnable;
        struct loadavg          *avg = &averunnable;
+       int             i;
 
     for (i = 0; i < 3; i++)
         avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
 
     for (i = 0; i < 3; i++)
         avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +