]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/kern_synch.c
xnu-6153.81.5.tar.gz
[apple/xnu.git] / bsd / kern / kern_synch.c
index 841cdeba95a58210104b3ac15958338917063b20..019952e736158c4cb0cc8062ec321448cdb6cac0 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 2000-2016 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * Copyright (c) 2000-2016 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
  * 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.
  * 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.
  * 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
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
- * 
+ *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
-/* 
+/*
  * Mach Operating System
  * Copyright (c) 1987 Carnegie-Mellon University
  * All rights reserved.  The CMU software License Agreement specifies
  * Mach Operating System
  * Copyright (c) 1987 Carnegie-Mellon University
  * All rights reserved.  The CMU software License Agreement specifies
 #include <kern/locks.h>
 #include <kern/policy_internal.h>
 
 #include <kern/locks.h>
 #include <kern/policy_internal.h>
 
-#include <sys/systm.h>                 /* for unix_syscall_return() */
+#include <sys/systm.h>                  /* for unix_syscall_return() */
 #include <libkern/OSAtomic.h>
 
 #include <libkern/OSAtomic.h>
 
-extern void compute_averunnable(void *);       /* XXX */
+extern void compute_averunnable(void *);        /* XXX */
 
 __attribute__((noreturn))
 static void
 
 __attribute__((noreturn))
 static void
@@ -76,49 +76,54 @@ _sleep_continue( __unused void *parameter, wait_result_t wresult)
        spinmutex = ut->uu_pri & PSPIN;
 
        switch (wresult) {
        spinmutex = ut->uu_pri & PSPIN;
 
        switch (wresult) {
-               case THREAD_TIMED_OUT:
-                       error = EWOULDBLOCK;
+       case THREAD_TIMED_OUT:
+               error = EWOULDBLOCK;
+               break;
+       case THREAD_AWAKENED:
+               /*
+                * Posix implies any signal should be delivered
+                * first, regardless of whether awakened due
+                * to receiving event.
+                */
+               if (!catch) {
                        break;
                        break;
-               case THREAD_AWAKENED:
-                       /*
-                        * Posix implies any signal should be delivered
-                        * first, regardless of whether awakened due
-                        * to receiving event.
-                        */
-                       if (!catch)
-                               break;
-                       /* else fall through */
-               case THREAD_INTERRUPTED:
-                       if (catch) {
-                               if (thread_should_abort(self)) {
-                                       error = EINTR;
-                               } else if (SHOULDissignal(p,ut)) {
-                                       if ((sig = CURSIG(p)) != 0) {
-                                               if (p->p_sigacts->ps_sigintr & sigmask(sig))
-                                                       error = EINTR;
-                                               else
-                                                       error = ERESTART;
-                                       }
-                                       if (thread_should_abort(self)) {
+               }
+       /* else fall through */
+       case THREAD_INTERRUPTED:
+               if (catch) {
+                       if (thread_should_abort(self)) {
+                               error = EINTR;
+                       } else if (SHOULDissignal(p, ut)) {
+                               if ((sig = CURSIG(p)) != 0) {
+                                       if (p->p_sigacts->ps_sigintr & sigmask(sig)) {
                                                error = EINTR;
                                                error = EINTR;
+                                       } else {
+                                               error = ERESTART;
                                        }
                                        }
-                               } else if( (ut->uu_flag & ( UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
-                                        /* due to thread cancel */
-                                        error = EINTR;
-                                }
-                       }  else
+                               }
+                               if (thread_should_abort(self)) {
+                                       error = EINTR;
+                               }
+                       } else if ((ut->uu_flag & (UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
+                               /* due to thread cancel */
                                error = EINTR;
                                error = EINTR;
-                       break;
+                       }
+               } else {
+                       error = EINTR;
+               }
+               break;
        }
 
        }
 
-       if (error == EINTR || error == ERESTART)
+       if (error == EINTR || error == ERESTART) {
                act_set_astbsd(self);
                act_set_astbsd(self);
+       }
 
        if (ut->uu_mtx && !dropmutex) {
 
        if (ut->uu_mtx && !dropmutex) {
-               if (spinmutex)
+               if (spinmutex) {
                        lck_mtx_lock_spin(ut->uu_mtx);
                        lck_mtx_lock_spin(ut->uu_mtx);
-               else
+               } else {
                        lck_mtx_lock(ut->uu_mtx);
                        lck_mtx_lock(ut->uu_mtx);
+               }
        }
        ut->uu_wchan = NULL;
        ut->uu_wmesg = NULL;
        }
        ut->uu_wchan = NULL;
        ut->uu_wmesg = NULL;
@@ -149,12 +154,12 @@ _sleep_continue( __unused void *parameter, wait_result_t wresult)
 
 static int
 _sleep(
 
 static int
 _sleep(
-       caddr_t         chan,
-       int             pri,
-       const char      *wmsg,
-       u_int64_t       abstime,
-       int             (*continuation)(int),
-        lck_mtx_t      *mtx)
+       caddr_t         chan,
+       int             pri,
+       const char      *wmsg,
+       u_int64_t       abstime,
+       int             (*continuation)(int),
+       lck_mtx_t       *mtx)
 {
        struct proc *p;
        thread_t self = current_thread();
 {
        struct proc *p;
        thread_t self = current_thread();
@@ -170,176 +175,192 @@ _sleep(
        p = current_proc();
        p->p_priority = pri & PRIMASK;
        /* It can still block in proc_exit() after the teardown. */
        p = current_proc();
        p->p_priority = pri & PRIMASK;
        /* It can still block in proc_exit() after the teardown. */
-       if (p->p_stats != NULL)
+       if (p->p_stats != NULL) {
                OSIncrementAtomicLong(&p->p_stats->p_ru.ru_nvcsw);
                OSIncrementAtomicLong(&p->p_stats->p_ru.ru_nvcsw);
-       
-       if (pri & PCATCH)
+       }
+
+       if (pri & PCATCH) {
                catch = THREAD_ABORTSAFE;
                catch = THREAD_ABORTSAFE;
-       else
+       } else {
                catch = THREAD_UNINT;
                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) {
 
        /* 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;
+               int     flags;
 
 
-               if (dropmutex)
+               if (dropmutex) {
                        flags = LCK_SLEEP_UNLOCK;
                        flags = LCK_SLEEP_UNLOCK;
-               else
+               } else {
                        flags = LCK_SLEEP_DEFAULT;
                        flags = LCK_SLEEP_DEFAULT;
+               }
 
 
-               if (spinmutex)
+               if (spinmutex) {
                        flags |= LCK_SLEEP_SPIN;
                        flags |= LCK_SLEEP_SPIN;
+               }
 
 
-               if (abstime)
+               if (abstime) {
                        wait_result = lck_mtx_sleep_deadline(mtx, flags, chan, catch, abstime);
                        wait_result = lck_mtx_sleep_deadline(mtx, flags, chan, catch, abstime);
-               else
+               } else {
                        wait_result = lck_mtx_sleep(mtx, flags, chan, catch);
                        wait_result = lck_mtx_sleep(mtx, flags, chan, catch);
-       }
-       else {
-               if (chan != NULL)
+               }
+       else {
+               if (chan != NULL) {
                        assert_wait_deadline(chan, catch, abstime);
                        assert_wait_deadline(chan, catch, abstime);
-               if (mtx)
+               }
+               if (mtx) {
                        lck_mtx_unlock(mtx);
                        lck_mtx_unlock(mtx);
+               }
 
                if (catch == THREAD_ABORTSAFE) {
 
                if (catch == THREAD_ABORTSAFE) {
-                       if (SHOULDissignal(p,ut)) {
+                       if (SHOULDissignal(p, ut)) {
                                if ((sig = CURSIG(p)) != 0) {
                                if ((sig = CURSIG(p)) != 0) {
-                                       if (clear_wait(self, THREAD_INTERRUPTED) == KERN_FAILURE)
+                                       if (clear_wait(self, THREAD_INTERRUPTED) == KERN_FAILURE) {
                                                goto block;
                                                goto block;
-                                       if (p->p_sigacts->ps_sigintr & sigmask(sig))
+                                       }
+                                       if (p->p_sigacts->ps_sigintr & sigmask(sig)) {
                                                error = EINTR;
                                                error = EINTR;
-                                       else
+                                       } else {
                                                error = ERESTART;
                                                error = ERESTART;
+                                       }
                                        if (mtx && !dropmutex) {
                                        if (mtx && !dropmutex) {
-                                               if (spinmutex)
+                                               if (spinmutex) {
                                                        lck_mtx_lock_spin(mtx);
                                                        lck_mtx_lock_spin(mtx);
-                                               else
+                                               } else {
                                                        lck_mtx_lock(mtx);
                                                        lck_mtx_lock(mtx);
+                                               }
                                        }
                                        goto out;
                                }
                        }
                        if (thread_should_abort(self)) {
                                        }
                                        goto out;
                                }
                        }
                        if (thread_should_abort(self)) {
-                               if (clear_wait(self, THREAD_INTERRUPTED) == KERN_FAILURE)
+                               if (clear_wait(self, THREAD_INTERRUPTED) == KERN_FAILURE) {
                                        goto block;
                                        goto block;
+                               }
                                error = EINTR;
 
                                if (mtx && !dropmutex) {
                                error = EINTR;
 
                                if (mtx && !dropmutex) {
-                                       if (spinmutex)
+                                       if (spinmutex) {
                                                lck_mtx_lock_spin(mtx);
                                                lck_mtx_lock_spin(mtx);
-                                       else
+                                       } else {
                                                lck_mtx_lock(mtx);
                                                lck_mtx_lock(mtx);
+                                       }
                                }
                                goto out;
                        }
                                }
                                goto out;
                        }
-               }               
+               }
 
 
 block:
                if ((thread_continue_t)continuation != THREAD_CONTINUE_NULL) {
 
 
 block:
                if ((thread_continue_t)continuation != THREAD_CONTINUE_NULL) {
-                       ut->uu_continuation = continuation;
+                       ut->uu_continuation = continuation;
                        ut->uu_pri  = pri;
                        ut->uu_pri  = pri;
-                       ut->uu_timo = abstime? 1: 0;
                        ut->uu_mtx  = mtx;
                        (void) thread_block(_sleep_continue);
                        /* NOTREACHED */
                }
                        ut->uu_mtx  = mtx;
                        (void) thread_block(_sleep_continue);
                        /* NOTREACHED */
                }
-               
+
                wait_result = thread_block(THREAD_CONTINUE_NULL);
 
                if (mtx && !dropmutex) {
                wait_result = thread_block(THREAD_CONTINUE_NULL);
 
                if (mtx && !dropmutex) {
-                       if (spinmutex)
+                       if (spinmutex) {
                                lck_mtx_lock_spin(mtx);
                                lck_mtx_lock_spin(mtx);
-                       else
+                       } else {
                                lck_mtx_lock(mtx);
                                lck_mtx_lock(mtx);
+                       }
                }
        }
 
        switch (wait_result) {
                }
        }
 
        switch (wait_result) {
-               case THREAD_TIMED_OUT:
-                       error = EWOULDBLOCK;
+       case THREAD_TIMED_OUT:
+               error = EWOULDBLOCK;
+               break;
+       case THREAD_AWAKENED:
+       case THREAD_RESTART:
+               /*
+                * Posix implies any signal should be delivered
+                * first, regardless of whether awakened due
+                * to receiving event.
+                */
+               if (catch != THREAD_ABORTSAFE) {
                        break;
                        break;
-               case THREAD_AWAKENED:
-               case THREAD_RESTART:
-                       /*
-                        * Posix implies any signal should be delivered
-                        * first, regardless of whether awakened due
-                        * to receiving event.
-                        */
-                       if (catch != THREAD_ABORTSAFE)
-                               break;
-                       /* else fall through */
-               case THREAD_INTERRUPTED:
-                       if (catch == THREAD_ABORTSAFE) {
-                               if (thread_should_abort(self)) {
-                                       error = EINTR;
-                               } else if (SHOULDissignal(p, ut)) {
-                                       if ((sig = CURSIG(p)) != 0) {
-                                               if (p->p_sigacts->ps_sigintr & sigmask(sig))
-                                                       error = EINTR;
-                                               else
-                                                       error = ERESTART;
-                                       }
-                                       if (thread_should_abort(self)) {
+               }
+       /* else fall through */
+       case THREAD_INTERRUPTED:
+               if (catch == THREAD_ABORTSAFE) {
+                       if (thread_should_abort(self)) {
+                               error = EINTR;
+                       } else if (SHOULDissignal(p, ut)) {
+                               if ((sig = CURSIG(p)) != 0) {
+                                       if (p->p_sigacts->ps_sigintr & sigmask(sig)) {
                                                error = EINTR;
                                                error = EINTR;
+                                       } else {
+                                               error = ERESTART;
                                        }
                                        }
-                               } else if( (ut->uu_flag & ( UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
-                                        /* due to thread cancel */
-                                        error = EINTR;
-                                }
-                       }  else
+                               }
+                               if (thread_should_abort(self)) {
+                                       error = EINTR;
+                               }
+                       } else if ((ut->uu_flag & (UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL) {
+                               /* due to thread cancel */
                                error = EINTR;
                                error = EINTR;
-                       break;
+                       }
+               } else {
+                       error = EINTR;
+               }
+               break;
        }
 out:
        }
 out:
-       if (error == EINTR || error == ERESTART)
+       if (error == EINTR || error == ERESTART) {
                act_set_astbsd(self);
                act_set_astbsd(self);
+       }
        ut->uu_wchan = NULL;
        ut->uu_wmesg = NULL;
 
        ut->uu_wchan = NULL;
        ut->uu_wmesg = NULL;
 
-       return (error);
+       return error;
 }
 
 int
 sleep(
 }
 
 int
 sleep(
-       void    *chan,
-       int             pri)
+       void    *chan,
+       int             pri)
 {
        return _sleep((caddr_t)chan, pri, (char *)NULL, 0, (int (*)(int))0, (lck_mtx_t *)0);
 }
 
 int
 msleep0(
 {
        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))
+       void            *chan,
+       lck_mtx_t       *mtx,
+       int             pri,
+       const char      *wmsg,
+       int             timo,
+       int             (*continuation)(int))
 {
 {
-       u_int64_t       abstime = 0;
+       u_int64_t       abstime = 0;
 
 
-       if (timo)
+       if (timo) {
                clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime);
                clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime);
+       }
 
        return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, mtx);
 }
 
 int
 msleep(
 
        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)
+       void            *chan,
+       lck_mtx_t       *mtx,
+       int             pri,
+       const char      *wmsg,
+       struct timespec         *ts)
 {
 {
-       u_int64_t       abstime = 0;
+       u_int64_t       abstime = 0;
 
        if (ts && (ts->tv_sec || ts->tv_nsec)) {
 
        if (ts && (ts->tv_sec || ts->tv_nsec)) {
-               nanoseconds_to_absolutetime((uint64_t)ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec,  &abstime );
+               nanoseconds_to_absolutetime((uint64_t)ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec, &abstime );
                clock_absolutetime_interval_to_deadline( abstime, &abstime );
        }
 
                clock_absolutetime_interval_to_deadline( abstime, &abstime );
        }
 
@@ -348,52 +369,54 @@ msleep(
 
 int
 msleep1(
 
 int
 msleep1(
-       void            *chan,
-       lck_mtx_t       *mtx,
-       int             pri,
-       const char      *wmsg,
-       u_int64_t       abstime)
+       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(
 {
        return _sleep((caddr_t)chan, pri, wmsg, abstime, (int (*)(int))0, mtx);
 }
 
 int
 tsleep(
-       void            *chan,
-       int             pri,
-       const char      *wmsg,
-       int             timo)
+       void            *chan,
+       int             pri,
+       const char      *wmsg,
+       int             timo)
 {
 {
-       u_int64_t       abstime = 0;
+       u_int64_t       abstime = 0;
 
 
-       if (timo)
+       if (timo) {
                clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime);
                clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime);
+       }
        return _sleep((caddr_t)chan, pri, wmsg, abstime, (int (*)(int))0, (lck_mtx_t *)0);
 }
 
 int
 tsleep0(
        return _sleep((caddr_t)chan, pri, wmsg, abstime, (int (*)(int))0, (lck_mtx_t *)0);
 }
 
 int
 tsleep0(
-       void            *chan,
-       int             pri,
-       const char      *wmsg,
-       int             timo,
-       int             (*continuation)(int))
-{                      
-       u_int64_t       abstime = 0;
-
-       if (timo)
+       void            *chan,
+       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);
                clock_interval_to_deadline(timo, NSEC_PER_SEC / hz, &abstime);
+       }
        return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, (lck_mtx_t *)0);
 }
 
 int
 tsleep1(
        return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, (lck_mtx_t *)0);
 }
 
 int
 tsleep1(
-       void            *chan,
-       int             pri,
-       const char      *wmsg,
-       u_int64_t       abstime,
-       int             (*continuation)(int))
-{                      
+       void            *chan,
+       int             pri,
+       const char      *wmsg,
+       u_int64_t       abstime,
+       int             (*continuation)(int))
+{
        return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, (lck_mtx_t *)0);
 }
 
        return _sleep((caddr_t)chan, pri, wmsg, abstime, continuation, (lck_mtx_t *)0);
 }
 
@@ -430,25 +453,26 @@ resetpriority(struct proc *p)
 }
 
 struct loadavg averunnable =
 }
 
 struct loadavg averunnable =
-       { {0, 0, 0}, FSCALE };          /* load average, of runnable procs */
+{ {0, 0, 0}, FSCALE };                  /* load average, of runnable procs */
 /*
  * Constants for averages over 1, 5, and 15 minutes
  * when sampling at 5 second intervals.
  */
 static fixpt_t cexp[3] = {
 /*
  * Constants for averages over 1, 5, and 15 minutes
  * when sampling at 5 second intervals.
  */
 static fixpt_t cexp[3] = {
-    (fixpt_t)(0.9200444146293232 * FSCALE),    /* exp(-1/12) */
-    (fixpt_t)(0.9834714538216174 * FSCALE),    /* exp(-1/60) */
-    (fixpt_t)(0.9944598480048967 * FSCALE),    /* exp(-1/180) */
+       (fixpt_t)(0.9200444146293232 * FSCALE), /* exp(-1/12) */
+       (fixpt_t)(0.9834714538216174 * FSCALE), /* exp(-1/60) */
+       (fixpt_t)(0.9944598480048967 * FSCALE), /* exp(-1/180) */
 };
 
 void
 compute_averunnable(void *arg)
 {
 };
 
 void
 compute_averunnable(void *arg)
 {
-       unsigned int            nrun = *(unsigned int *)arg;
-       struct loadavg          *avg = &averunnable;
-       int             i;
+       unsigned int            nrun = *(unsigned int *)arg;
+       struct loadavg          *avg = &averunnable;
+       int             i;
 
 
-    for (i = 0; i < 3; i++)
-        avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
-            nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
+       for (i = 0; i < 3; i++) {
+               avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
+                   nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
+       }
 }
 }