]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/sync_lock.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / osfmk / kern / sync_lock.c
index 9acae3739f1f21c444019445d80049588fbfc81c..027754519d31837b96699274b2eb6410b979377d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
  *     Contains RT distributed lock synchronization services.
  */
 
-#include <kern/etap_macros.h>
+#include <mach/mach_types.h>
+#include <mach/lock_set_server.h>
+#include <mach/task_server.h>
+
 #include <kern/misc_protos.h>
+#include <kern/kalloc.h>
 #include <kern/sync_lock.h>
 #include <kern/sched_prim.h>
 #include <kern/ipc_kobject.h>
 #include <kern/ipc_sync.h>
-#include <kern/etap_macros.h>
 #include <kern/thread.h>
 #include <kern/task.h>
 
 
 #define ulock_ownership_set(ul, th)                            \
        MACRO_BEGIN                                             \
-       thread_act_t _th_act;                                   \
-       _th_act = (th)->top_act;                                \
-       act_lock(_th_act);                                      \
-       enqueue (&_th_act->held_ulocks, (queue_entry_t) (ul));  \
-       act_unlock(_th_act);                                    \
-       (ul)->holder = _th_act;                                 \
+       thread_mtx_lock(th);                                    \
+       enqueue (&th->held_ulocks, (queue_entry_t) (ul));  \
+       thread_mtx_unlock(th);                                  \
+       (ul)->holder = th;                                      \
        MACRO_END
 
 #define ulock_ownership_clear(ul)                              \
        MACRO_BEGIN                                             \
-       thread_act_t _th_act;                                   \
-       _th_act = (ul)->holder;                                 \
-        if (_th_act->active) {                                 \
-               act_lock(_th_act);                              \
-               remqueue(&_th_act->held_ulocks,                 \
+       thread_t th;                                    \
+       th = (ul)->holder;                                      \
+        if (th->active) {                                      \
+               thread_mtx_lock(th);                            \
+               remqueue(&th->held_ulocks,                      \
                         (queue_entry_t) (ul));                 \
-               act_unlock(_th_act);                            \
+               thread_mtx_unlock(th);                          \
        } else {                                                \
-               remqueue(&_th_act->held_ulocks,                 \
+               remqueue(&th->held_ulocks,                      \
                         (queue_entry_t) (ul));                 \
        }                                                       \
-       (ul)->holder = THR_ACT_NULL;                            \
+       (ul)->holder = THREAD_NULL;                             \
        MACRO_END
 
 /*
@@ -172,7 +173,7 @@ lock_set_create (
                ulock = (ulock_t) &lock_set->ulock_list[x];
                ulock_lock_init(ulock);
                ulock->lock_set  = lock_set;
-               ulock->holder    = THR_ACT_NULL;
+               ulock->holder    = THREAD_NULL;
                ulock->blocked   = FALSE;
                ulock->unstable  = FALSE;
                ulock->ho_wait   = FALSE;
@@ -201,7 +202,6 @@ lock_set_create (
 kern_return_t
 lock_set_destroy (task_t task, lock_set_t lock_set)
 {
-       thread_t        thread;
        ulock_t         ulock;
        int             i;
 
@@ -306,10 +306,10 @@ lock_acquire (lock_set_t lock_set, int lock_id)
         *  Block the current thread if the lock is already held.
         */
 
-       if (ulock->holder != THR_ACT_NULL) {
+       if (ulock->holder != THREAD_NULL) {
                int wait_result;
 
-               if (ulock->holder == current_act()) {
+               if (ulock->holder == current_thread()) {
                        ulock_unlock(ulock);
                        return KERN_LOCK_OWNED_SELF;
                }
@@ -317,7 +317,7 @@ lock_acquire (lock_set_t lock_set, int lock_id)
                ulock->blocked = TRUE;
                wait_result = wait_queue_assert_wait64(&ulock->wait_queue,
                                       LOCK_SET_EVENT,
-                                      THREAD_ABORTSAFE);
+                                      THREAD_ABORTSAFE, 0);
                ulock_unlock(ulock);
 
                /*
@@ -372,7 +372,7 @@ lock_release (lock_set_t lock_set, int lock_id)
 
        ulock = (ulock_t) &lock_set->ulock_list[lock_id];
 
-       return (lock_release_internal(ulock, current_act()));
+       return (ulock_release_internal(ulock, current_thread()));
 }
 
 kern_return_t
@@ -405,10 +405,10 @@ lock_try (lock_set_t lock_set, int lock_id)
         *  whether it already holds the lock or another thread does.
         */
 
-       if (ulock->holder != THR_ACT_NULL) {
+       if (ulock->holder != THREAD_NULL) {
                lock_set_unlock(lock_set);
 
-               if (ulock->holder == current_act()) {
+               if (ulock->holder == current_thread()) {
                        ulock_unlock(ulock);
                        return KERN_LOCK_OWNED_SELF;
                }
@@ -450,7 +450,7 @@ lock_make_stable (lock_set_t lock_set, int lock_id)
        ulock_lock(ulock);
        lock_set_unlock(lock_set);
 
-       if (ulock->holder != current_act()) {
+       if (ulock->holder != current_thread()) {
                ulock_unlock(ulock);
                return KERN_INVALID_RIGHT;
        }
@@ -471,11 +471,10 @@ lock_make_stable (lock_set_t lock_set, int lock_id)
  *       KERN_LOCK_UNSTABLE status, until the lock is made stable again.
  */
 kern_return_t
-lock_make_unstable (ulock_t ulock, thread_act_t thr_act)
+lock_make_unstable (ulock_t ulock, thread_t thread)
 {
        lock_set_t      lock_set;
 
-
        lock_set = ulock->lock_set;
        lock_set_lock(lock_set);
        if (!lock_set->active) {
@@ -486,7 +485,7 @@ lock_make_unstable (ulock_t ulock, thread_act_t thr_act)
        ulock_lock(ulock);
        lock_set_unlock(lock_set);
 
-       if (ulock->holder != thr_act) {
+       if (ulock->holder != thread) {
                ulock_unlock(ulock);
                return KERN_INVALID_RIGHT;
        }
@@ -498,18 +497,16 @@ lock_make_unstable (ulock_t ulock, thread_act_t thr_act)
 }
 
 /*
- *     ROUTINE:        lock_release_internal   [internal]
+ *     ROUTINE:        ulock_release_internal  [internal]
  *
  *     Releases the ulock.
  *     If any threads are blocked waiting for the ulock, one is woken-up.
  *
  */
 kern_return_t
-lock_release_internal (ulock_t ulock, thread_act_t thr_act)
+ulock_release_internal (ulock_t ulock, thread_t thread)
 {
        lock_set_t      lock_set;
-       int             result;
-
 
        if ((lock_set = ulock->lock_set) == LOCK_SET_NULL)
                return KERN_INVALID_ARGUMENT;
@@ -522,7 +519,7 @@ lock_release_internal (ulock_t ulock, thread_act_t thr_act)
        ulock_lock(ulock);
        lock_set_unlock(lock_set);              
 
-       if (ulock->holder != thr_act) {
+       if (ulock->holder != thread) {
                ulock_unlock(ulock);
                return KERN_INVALID_RIGHT;
        }
@@ -534,18 +531,18 @@ lock_release_internal (ulock_t ulock, thread_act_t thr_act)
         */
        if (ulock->blocked) {
                wait_queue_t    wq = &ulock->wait_queue;
-               thread_t        thread;
+               thread_t        wqthread;
                spl_t           s;
 
                s = splsched();
                wait_queue_lock(wq);
-               thread = wait_queue_wakeup64_identity_locked(wq,
+               wqthread = wait_queue_wakeup64_identity_locked(wq,
                                                           LOCK_SET_EVENT,
                                                           THREAD_AWAKENED,
                                                           TRUE);
                /* wait_queue now unlocked, thread locked */
 
-               if (thread != THREAD_NULL) {
+               if (wqthread != THREAD_NULL) {
                        /*
                         * JMM - These ownership transfer macros have a
                         * locking/race problem.  To keep the thread from
@@ -556,7 +553,7 @@ lock_release_internal (ulock_t ulock, thread_act_t thr_act)
                         * Since this code was already broken before I got
                         * here, I will leave it for now.
                         */
-                       thread_unlock(thread);
+                       thread_unlock(wqthread);
                        splx(s);
 
                        /*
@@ -564,7 +561,7 @@ lock_release_internal (ulock_t ulock, thread_act_t thr_act)
                         *  from the current thread to the acquisition thread.
                         */
                        ulock_ownership_clear(ulock);
-                       ulock_ownership_set(ulock, thread);
+                       ulock_ownership_set(ulock, wqthread);
                        ulock_unlock(ulock);
                        
                        return KERN_SUCCESS;
@@ -608,7 +605,7 @@ lock_handoff (lock_set_t lock_set, int lock_id)
        ulock_lock(ulock);
        lock_set_unlock(lock_set);
 
-       if (ulock->holder != current_act()) {
+       if (ulock->holder != current_thread()) {
                ulock_unlock(ulock);
                return KERN_INVALID_RIGHT;
        }
@@ -646,7 +643,8 @@ lock_handoff (lock_set_t lock_set, int lock_id)
                         * changing states on us (nullifying the ownership
                         * assignment) we need to keep the thread locked
                         * during the assignment.  But we can't because the
-                        * macros take an activation lock, which is a mutex.
+                        * macros take a thread mutex lock.
+                        *
                         * Since this code was already broken before I got
                         * here, I will leave it for now.
                         */
@@ -679,7 +677,7 @@ lock_handoff (lock_set_t lock_set, int lock_id)
        ulock->ho_wait = TRUE;
        wait_result = wait_queue_assert_wait64(&ulock->wait_queue,
                               LOCK_SET_HANDOFF,
-                              THREAD_ABORTSAFE);
+                              THREAD_ABORTSAFE, 0);
        ulock_unlock(ulock);
 
        if (wait_result == THREAD_WAITING)
@@ -697,17 +695,17 @@ lock_handoff (lock_set_t lock_set, int lock_id)
 
        case THREAD_INTERRUPTED:
                ulock_lock(ulock);
-               assert(ulock->holder == current_act());
+               assert(ulock->holder == current_thread());
                ulock->ho_wait = FALSE;
                ulock_unlock(ulock);
                return KERN_ABORTED;
 
        case THREAD_RESTART:
                goto retry;
-
-       default:
-               panic("lock_handoff");
        }
+
+       panic("lock_handoff");
+       return KERN_FAILURE;
 }
 
 kern_return_t
@@ -743,7 +741,7 @@ lock_handoff_accept (lock_set_t lock_set, int lock_id)
                return KERN_ALREADY_WAITING;
        }
 
-       if (ulock->holder == current_act()) {
+       if (ulock->holder == current_thread()) {
                ulock_unlock(ulock);
                return KERN_LOCK_OWNED_SELF;
        }
@@ -755,17 +753,15 @@ lock_handoff_accept (lock_set_t lock_set, int lock_id)
         */
        if (ulock->ho_wait) {
                wait_queue_t    wq = &ulock->wait_queue;
-               thread_t        thread;
 
                /*
                 *  See who the lucky devil is, if he is still there waiting.
                 */
-               assert(ulock->holder != THR_ACT_NULL);
-               thread = ulock->holder->thread;
+               assert(ulock->holder != THREAD_NULL);
 
                if (wait_queue_wakeup64_thread(wq,
                                            LOCK_SET_HANDOFF,
-                                           thread,
+                                           ulock->holder,
                                            THREAD_AWAKENED) == KERN_SUCCESS) {
                        /*
                         * Holder thread was still waiting to give it
@@ -790,7 +786,7 @@ lock_handoff_accept (lock_set_t lock_set, int lock_id)
        ulock->accept_wait = TRUE;
        wait_result = wait_queue_assert_wait64(&ulock->wait_queue,
                               LOCK_SET_HANDOFF,
-                              THREAD_ABORTSAFE);
+                              THREAD_ABORTSAFE, 0);
        ulock_unlock(ulock);
 
        if (wait_result == THREAD_WAITING)
@@ -814,10 +810,10 @@ lock_handoff_accept (lock_set_t lock_set, int lock_id)
 
        case THREAD_RESTART:
                goto retry;
-
-       default:
-               panic("lock_handoff_accept");
        }
+
+       panic("lock_handoff_accept");
+       return KERN_FAILURE;
 }
 
 /*
@@ -853,6 +849,19 @@ lock_set_dereference(lock_set_t lock_set)
        if (ref_count == 0) {
                size =  sizeof(struct lock_set) +
                        (sizeof(struct ulock) * (lock_set->n_ulocks - 1));
-               kfree((vm_offset_t) lock_set, size);
+               kfree(lock_set, size);
+       }
+}
+
+void
+ulock_release_all(
+       thread_t                thread)
+{
+       ulock_t         ulock;
+
+       while (!queue_empty(&thread->held_ulocks)) {
+               ulock = (ulock_t)queue_first(&thread->held_ulocks);
+               lock_make_unstable(ulock, thread);
+               ulock_release_internal(ulock, thread);
        }
 }