]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/wait_queue.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / osfmk / kern / wait_queue.c
index d44cd9ec95efda2aa5927dbe6982be00c4aad9bc..40986215d00c9fb8be2c4a377ab36ed633f575a1 100644 (file)
@@ -1,21 +1,22 @@
 /*
- * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * 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.
  * 
- * This 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
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE 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_LICENSE_HEADER_END@
  */
 
 #include <kern/wait_queue.h>
 
+/* forward declarations */
+static boolean_t wait_queue_member_locked(
+                       wait_queue_t            wq,
+                       wait_queue_set_t        wq_set);
+
+void wait_queue_unlink_one(
+                       wait_queue_t            wq,
+                       wait_queue_set_t        *wq_setp);
+
+kern_return_t wait_queue_set_unlink_all_nofree(
+                       wait_queue_set_t        wq_set);
+
 /*
  *     Routine:        wait_queue_init
  *     Purpose:
@@ -114,7 +127,7 @@ wait_queue_alloc(
        if (wq != WAIT_QUEUE_NULL) {
                ret = wait_queue_init(wq, policy);
                if (ret != KERN_SUCCESS) {
-                       kfree((vm_offset_t)wq, sizeof(struct wait_queue));
+                       kfree(wq, sizeof(struct wait_queue));
                        wq = WAIT_QUEUE_NULL;
                }
        }
@@ -136,7 +149,7 @@ wait_queue_free(
                return KERN_INVALID_ARGUMENT;
        if (!queue_empty(&wq->wq_queue))
                return KERN_FAILURE;
-       kfree((vm_offset_t)wq, sizeof(struct wait_queue));
+       kfree(wq, sizeof(struct wait_queue));
        return KERN_SUCCESS;
 }
 
@@ -169,7 +182,7 @@ wait_queue_set_init(
        return KERN_SUCCESS;
 }
 
-/* legacy API */
+
 kern_return_t
 wait_queue_sub_init(
        wait_queue_set_t wqset,
@@ -178,6 +191,19 @@ wait_queue_sub_init(
        return wait_queue_set_init(wqset, policy);
 }
 
+kern_return_t
+wait_queue_sub_clearrefs(
+        wait_queue_set_t wq_set)
+{
+       if (!wait_queue_is_set(wq_set))
+               return KERN_INVALID_ARGUMENT;
+
+       wqs_lock(wq_set);
+       wq_set->wqs_refcount = 0;
+       wqs_unlock(wq_set);
+       return KERN_SUCCESS;
+}
+
 /*
  *     Routine:        wait_queue_set_alloc
  *     Purpose:
@@ -201,7 +227,7 @@ wait_queue_set_alloc(
 
                ret = wait_queue_set_init(wq_set, policy);
                if (ret != KERN_SUCCESS) {
-                       kfree((vm_offset_t)wq_set, sizeof(struct wait_queue_set));
+                       kfree(wq_set, sizeof(struct wait_queue_set));
                        wq_set = WAIT_QUEUE_SET_NULL;
                }
        }
@@ -225,22 +251,10 @@ wait_queue_set_free(
        if (!queue_empty(&wq_set->wqs_wait_queue.wq_queue))
                return KERN_FAILURE;
 
-       kfree((vm_offset_t)wq_set, sizeof(struct wait_queue_set));
+       kfree(wq_set, sizeof(struct wait_queue_set));
        return KERN_SUCCESS;
 }
 
-kern_return_t
-wait_queue_sub_clearrefs(
-        wait_queue_set_t wq_set)
-{
-       if (!wait_queue_is_set(wq_set))
-               return KERN_INVALID_ARGUMENT;
-
-       wqs_lock(wq_set);
-       wq_set->wqs_refcount = 0;
-       wqs_unlock(wq_set);
-       return KERN_SUCCESS;
-}
 
 /*
  *     
@@ -322,7 +336,7 @@ MACRO_END
  *             The wait queue is locked
  *             The set queue is just that, a set queue
  */
-__private_extern__ boolean_t
+static boolean_t
 wait_queue_member_locked(
        wait_queue_t wq,
        wait_queue_set_t wq_set)
@@ -465,7 +479,7 @@ wait_queue_link(
 
        ret = wait_queue_link_noalloc(wq, wq_set, wql);
        if (ret != KERN_SUCCESS)
-               kfree((vm_offset_t)wql, sizeof(struct wait_queue_link));
+               kfree(wql, sizeof(struct wait_queue_link));
 
        return ret;
 }      
@@ -532,7 +546,7 @@ wait_queue_unlink(
                                wqs_unlock(wq_set);
                                wait_queue_unlock(wq);
                                splx(s);
-                               kfree((vm_offset_t)wql, sizeof(struct wait_queue_link));
+                               kfree(wql, sizeof(struct wait_queue_link));
                                return KERN_SUCCESS;
                        }
                }
@@ -652,7 +666,7 @@ wait_queue_unlink_all(
 
        while(!queue_empty(links)) {
                wql = (wait_queue_link_t) dequeue(links);
-               kfree((vm_offset_t) wql, sizeof(struct wait_queue_link));
+               kfree(wql, sizeof(struct wait_queue_link));
        }
 
        return(KERN_SUCCESS);
@@ -675,7 +689,6 @@ wait_queue_set_unlink_all_nofree(
        wait_queue_link_t wql;
        wait_queue_t wq;
        queue_t q;
-       kern_return_t kret;
        spl_t s;
 
        if (!wait_queue_is_set(wq_set)) {
@@ -735,7 +748,6 @@ wait_queue_set_unlink_all(
        queue_t q;
        queue_head_t links_queue_head;
        queue_t links = &links_queue_head;
-       kern_return_t kret;
        spl_t s;
 
        if (!wait_queue_is_set(wq_set)) {
@@ -771,7 +783,7 @@ retry:
 
        while (!queue_empty (links)) {
                wql = (wait_queue_link_t) dequeue(links);
-               kfree((vm_offset_t)wql, sizeof(struct wait_queue_link));
+               kfree(wql, sizeof(struct wait_queue_link));
        }
        return(KERN_SUCCESS);
 }      
@@ -810,7 +822,7 @@ wait_queue_unlink_one(
                        wqs_unlock(wq_set);
                        wait_queue_unlock(wq);
                        splx(s);
-                       kfree((vm_offset_t)wql,sizeof(struct wait_queue_link));
+                       kfree(wql,sizeof(struct wait_queue_link));
                        *wq_setp = wq_set;
                        return;
                }
@@ -832,6 +844,7 @@ wait_queue_unlink_one(
  *
  *     Conditions:
  *             The wait queue is assumed locked.
+ *             The waiting thread is assumed locked.
  *
  */
 __private_extern__ wait_result_t
@@ -839,18 +852,19 @@ wait_queue_assert_wait64_locked(
        wait_queue_t wq,
        event64_t event,
        wait_interrupt_t interruptible,
-       boolean_t unlock)
+       uint64_t deadline,
+       thread_t thread)
 {
-       thread_t thread;
        wait_result_t wait_result;
 
+       if (!wait_queue_assert_possible(thread))
+               panic("wait_queue_assert_wait64_locked");
+
        if (wq->wq_type == _WAIT_QUEUE_SET_inited) {
                wait_queue_set_t wqs = (wait_queue_set_t)wq;
-               if (wqs->wqs_isprepost && wqs->wqs_refcount > 0) {
-                       if (unlock)
-                               wait_queue_unlock(wq);
+
+               if (wqs->wqs_isprepost && wqs->wqs_refcount > 0)
                        return(THREAD_AWAKENED);
-               }
        }
          
        /*
@@ -859,20 +873,22 @@ wait_queue_assert_wait64_locked(
         * the front of the queue.  Later, these queues will honor the policy
         * value set at wait_queue_init time.
         */
-       thread = current_thread();
-       thread_lock(thread);
        wait_result = thread_mark_wait_locked(thread, interruptible);
        if (wait_result == THREAD_WAITING) {
-               if (thread->vm_privilege)
+               if (thread->options & TH_OPT_VMPRIV)
                        enqueue_head(&wq->wq_queue, (queue_entry_t) thread);
                else
                        enqueue_tail(&wq->wq_queue, (queue_entry_t) thread);
+
                thread->wait_event = event;
                thread->wait_queue = wq;
+
+               if (deadline != 0) {
+                       if (!timer_call_enter(&thread->wait_timer, deadline))
+                               thread->wait_timer_active++;
+                       thread->wait_timer_is_set = TRUE;
+               }
        }
-       thread_unlock(thread);
-       if (unlock)
-               wait_queue_unlock(wq);
        return(wait_result);
 }
 
@@ -889,23 +905,24 @@ wait_result_t
 wait_queue_assert_wait(
        wait_queue_t wq,
        event_t event,
-       wait_interrupt_t interruptible)
+       wait_interrupt_t interruptible,
+       uint64_t deadline)
 {
        spl_t s;
        wait_result_t ret;
+       thread_t thread = current_thread();
 
        /* If it is an invalid wait queue, you can't wait on it */
-       if (!wait_queue_is_valid(wq)) {
-               thread_t thread = current_thread();
+       if (!wait_queue_is_valid(wq))
                return (thread->wait_result = THREAD_RESTART);
-       }
 
        s = splsched();
        wait_queue_lock(wq);
-       ret = wait_queue_assert_wait64_locked(
-                               wq, (event64_t)((uint32_t)event),
-                               interruptible, TRUE);
-       /* wait queue unlocked */
+       thread_lock(thread);
+       ret = wait_queue_assert_wait64_locked(wq, (event64_t)((uint32_t)event),
+                                                                                       interruptible, deadline, thread);
+       thread_unlock(thread);
+       wait_queue_unlock(wq);
        splx(s);
        return(ret);
 }
@@ -922,26 +939,27 @@ wait_result_t
 wait_queue_assert_wait64(
        wait_queue_t wq,
        event64_t event,
-       wait_interrupt_t interruptible)
+       wait_interrupt_t interruptible,
+       uint64_t deadline)
 {
        spl_t s;
        wait_result_t ret;
+       thread_t thread = current_thread();
 
        /* If it is an invalid wait queue, you cant wait on it */
-       if (!wait_queue_is_valid(wq)) {
-               thread_t thread = current_thread();
+       if (!wait_queue_is_valid(wq))
                return (thread->wait_result = THREAD_RESTART);
-       }
 
        s = splsched();
        wait_queue_lock(wq);
-       ret = wait_queue_assert_wait64_locked(wq, event, interruptible, TRUE);
-       /* wait queue unlocked */
+       thread_lock(thread);
+       ret = wait_queue_assert_wait64_locked(wq, event, interruptible, deadline, thread);
+       thread_unlock(thread);
+       wait_queue_unlock(wq);
        splx(s);
        return(ret);
 }
 
-
 /*
  *     Routine:        _wait_queue_select64_all
  *     Purpose:
@@ -1060,7 +1078,7 @@ wait_queue_wakeup64_all_locked(
        res = KERN_NOT_WAITING;
        while (!queue_empty (q)) {
                thread_t thread = (thread_t) dequeue(q);
-               res = thread_go_locked(thread, result);
+               res = thread_go(thread, result);
                assert(res == KERN_SUCCESS);
                thread_unlock(thread);
        }
@@ -1194,8 +1212,7 @@ _wait_queue_select64_one(
                         * the event we are posting to this queue, pull
                         * it off the queue and stick it in out wake_queue.
                         */
-                       thread_t t = (thread_t)wq_element;
-
+                       t = (thread_t)wq_element;
                        if (t->wait_event == event) {
                                thread_lock(t);
                                remqueue(q, (queue_entry_t) t);
@@ -1204,6 +1221,8 @@ _wait_queue_select64_one(
                                t->at_safe_point = FALSE;
                                return t;       /* still locked */
                        }
+
+                       t = THREAD_NULL;
                }
                wq_element = wqe_next;
        }
@@ -1237,7 +1256,6 @@ wait_queue_peek64_locked(
 {
        wait_queue_element_t wq_element;
        wait_queue_element_t wqe_next;
-       thread_t t;
        queue_t q;
 
        assert(wq->wq_fifo);
@@ -1423,7 +1441,7 @@ wait_queue_wakeup64_identity_locked(
                wait_queue_unlock(wq);
 
        if (thread) {
-               res = thread_go_locked(thread, result);
+               res = thread_go(thread, result);
                assert(res == KERN_SUCCESS);
        }
        return thread;  /* still locked if not NULL */
@@ -1462,7 +1480,7 @@ wait_queue_wakeup64_one_locked(
        if (thread) {
                kern_return_t res;
                
-               res = thread_go_locked(thread, result);
+               res = thread_go(thread, result);
                assert(res == KERN_SUCCESS);
                thread_unlock(thread);
                return res;
@@ -1503,7 +1521,7 @@ wait_queue_wakeup_one(
        if (thread) {
                kern_return_t res;
 
-               res = thread_go_locked(thread, result);
+               res = thread_go(thread, result);
                assert(res == KERN_SUCCESS);
                thread_unlock(thread);
                splx(s);
@@ -1545,7 +1563,7 @@ wait_queue_wakeup64_one(
        if (thread) {
                kern_return_t res;
 
-               res = thread_go_locked(thread, result);
+               res = thread_go(thread, result);
                assert(res == KERN_SUCCESS);
                thread_unlock(thread);
                splx(s);
@@ -1599,7 +1617,7 @@ wait_queue_wakeup64_thread_locked(
        if (res != KERN_SUCCESS)
                return KERN_NOT_WAITING;
 
-       res = thread_go_locked(thread, result);
+       res = thread_go(thread, result);
        assert(res == KERN_SUCCESS);
        thread_unlock(thread);
        return res;
@@ -1644,7 +1662,7 @@ wait_queue_wakeup_thread(
        wait_queue_unlock(wq);
 
        if (res == KERN_SUCCESS) {
-               res = thread_go_locked(thread, result);
+               res = thread_go(thread, result);
                assert(res == KERN_SUCCESS);
                thread_unlock(thread);
                splx(s);
@@ -1693,7 +1711,7 @@ wait_queue_wakeup64_thread(
        wait_queue_unlock(wq);
 
        if (res == KERN_SUCCESS) {
-               res = thread_go_locked(thread, result);
+               res = thread_go(thread, result);
                assert(res == KERN_SUCCESS);
                thread_unlock(thread);
                splx(s);