]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sync_lock.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
27 * File: kern/sync_lock.c
28 * Author: Joseph CaraDonna
30 * Contains RT distributed lock synchronization services.
33 #include <kern/etap_macros.h>
34 #include <kern/misc_protos.h>
35 #include <kern/sync_lock.h>
36 #include <kern/sched_prim.h>
37 #include <kern/ipc_kobject.h>
38 #include <kern/ipc_sync.h>
39 #include <kern/etap_macros.h>
40 #include <kern/thread.h>
41 #include <kern/task.h>
43 #include <ipc/ipc_port.h>
44 #include <ipc/ipc_space.h>
47 * Ulock ownership MACROS
49 * Assumes: ulock internal lock is held
52 #define ulock_ownership_set(ul, th) \
54 thread_act_t _th_act; \
55 _th_act = (th)->top_act; \
57 enqueue (&_th_act->held_ulocks, (queue_entry_t) (ul)); \
58 act_unlock(_th_act); \
59 (ul)->holder = _th_act; \
62 #define ulock_ownership_clear(ul) \
64 thread_act_t _th_act; \
65 _th_act = (ul)->holder; \
66 if (_th_act->active) { \
68 remqueue(&_th_act->held_ulocks, \
69 (queue_entry_t) (ul)); \
70 act_unlock(_th_act); \
72 remqueue(&_th_act->held_ulocks, \
73 (queue_entry_t) (ul)); \
75 (ul)->holder = THR_ACT_NULL; \
79 * Lock set ownership MACROS
82 #define lock_set_ownership_set(ls, t) \
85 enqueue_head(&(t)->lock_set_list, (queue_entry_t) (ls));\
86 (t)->lock_sets_owned++; \
91 #define lock_set_ownership_clear(ls, t) \
94 remqueue(&(t)->lock_set_list, (queue_entry_t) (ls)); \
95 (t)->lock_sets_owned--; \
99 unsigned int lock_set_event
;
100 #define LOCK_SET_EVENT ((event64_t)&lock_set_event)
102 unsigned int lock_set_handoff
;
103 #define LOCK_SET_HANDOFF ((event64_t)&lock_set_handoff)
106 * ROUTINE: lock_set_init [private]
108 * Initialize the lock_set subsystem.
110 * For now, we don't have anything to do here.
120 * ROUTINE: lock_set_create [exported]
122 * Creates a lock set.
123 * The port representing the lock set is returned as a parameter.
128 lock_set_t
*new_lock_set
,
132 lock_set_t lock_set
= LOCK_SET_NULL
;
137 *new_lock_set
= LOCK_SET_NULL
;
139 if (task
== TASK_NULL
|| n_ulocks
<= 0 || policy
> SYNC_POLICY_MAX
)
140 return KERN_INVALID_ARGUMENT
;
142 size
= sizeof(struct lock_set
) + (sizeof(struct ulock
) * (n_ulocks
-1));
143 lock_set
= (lock_set_t
) kalloc (size
);
145 if (lock_set
== LOCK_SET_NULL
)
146 return KERN_RESOURCE_SHORTAGE
;
149 lock_set_lock_init(lock_set
);
150 lock_set
->n_ulocks
= n_ulocks
;
151 lock_set
->ref_count
= 1;
154 * Create and initialize the lock set port
156 lock_set
->port
= ipc_port_alloc_kernel();
157 if (lock_set
->port
== IP_NULL
) {
158 /* This will deallocate the lock set */
159 lock_set_dereference(lock_set
);
160 return KERN_RESOURCE_SHORTAGE
;
163 ipc_kobject_set (lock_set
->port
,
164 (ipc_kobject_t
) lock_set
,
168 * Initialize each ulock in the lock set
171 for (x
=0; x
< n_ulocks
; x
++) {
172 ulock
= (ulock_t
) &lock_set
->ulock_list
[x
];
173 ulock_lock_init(ulock
);
174 ulock
->lock_set
= lock_set
;
175 ulock
->holder
= THR_ACT_NULL
;
176 ulock
->blocked
= FALSE
;
177 ulock
->unstable
= FALSE
;
178 ulock
->ho_wait
= FALSE
;
179 wait_queue_init(&ulock
->wait_queue
, policy
);
182 lock_set_ownership_set(lock_set
, task
);
184 lock_set
->active
= TRUE
;
185 *new_lock_set
= lock_set
;
191 * ROUTINE: lock_set_destroy [exported]
193 * Destroys a lock set. This call will only succeed if the
194 * specified task is the SAME task name specified at the lock set's
198 * - All threads currently blocked on the lock set's ulocks are awoken.
199 * - These threads will return with the KERN_LOCK_SET_DESTROYED error.
202 lock_set_destroy (task_t task
, lock_set_t lock_set
)
208 if (task
== TASK_NULL
|| lock_set
== LOCK_SET_NULL
)
209 return KERN_INVALID_ARGUMENT
;
211 if (lock_set
->owner
!= task
)
212 return KERN_INVALID_RIGHT
;
214 lock_set_lock(lock_set
);
215 if (!lock_set
->active
) {
216 lock_set_unlock(lock_set
);
217 return KERN_LOCK_SET_DESTROYED
;
221 * Deactivate lock set
223 lock_set
->active
= FALSE
;
226 * If a ulock is currently held in the target lock set:
228 * 1) Wakeup all threads blocked on the ulock (if any). Threads
229 * may be blocked waiting normally, or waiting for a handoff.
230 * Blocked threads will return with KERN_LOCK_SET_DESTROYED.
232 * 2) ulock ownership is cleared.
233 * The thread currently holding the ulock is revoked of its
236 for (i
= 0; i
< lock_set
->n_ulocks
; i
++) {
237 ulock
= &lock_set
->ulock_list
[i
];
241 if (ulock
->accept_wait
) {
242 ulock
->accept_wait
= FALSE
;
243 wait_queue_wakeup64_one(&ulock
->wait_queue
,
249 if (ulock
->blocked
) {
250 ulock
->blocked
= FALSE
;
251 wait_queue_wakeup64_all(&ulock
->wait_queue
,
255 if (ulock
->ho_wait
) {
256 ulock
->ho_wait
= FALSE
;
257 wait_queue_wakeup64_one(&ulock
->wait_queue
,
261 ulock_ownership_clear(ulock
);
267 lock_set_unlock(lock_set
);
268 lock_set_ownership_clear(lock_set
, task
);
273 * Drop the lock set reference, which inturn destroys the
274 * lock set structure if the reference count goes to zero.
277 ipc_port_dealloc_kernel(lock_set
->port
);
278 lock_set_dereference(lock_set
);
284 lock_acquire (lock_set_t lock_set
, int lock_id
)
288 if (lock_set
== LOCK_SET_NULL
)
289 return KERN_INVALID_ARGUMENT
;
291 if (lock_id
< 0 || lock_id
>= lock_set
->n_ulocks
)
292 return KERN_INVALID_ARGUMENT
;
295 lock_set_lock(lock_set
);
296 if (!lock_set
->active
) {
297 lock_set_unlock(lock_set
);
298 return KERN_LOCK_SET_DESTROYED
;
301 ulock
= (ulock_t
) &lock_set
->ulock_list
[lock_id
];
303 lock_set_unlock(lock_set
);
306 * Block the current thread if the lock is already held.
309 if (ulock
->holder
!= THR_ACT_NULL
) {
312 if (ulock
->holder
== current_act()) {
314 return KERN_LOCK_OWNED_SELF
;
317 ulock
->blocked
= TRUE
;
318 wait_result
= wait_queue_assert_wait64(&ulock
->wait_queue
,
324 * Block - Wait for lock to become available.
326 if (wait_result
== THREAD_WAITING
)
327 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
330 * Check the result status:
332 * Check to see why thread was woken up. In all cases, we
333 * already have been removed from the queue.
335 switch (wait_result
) {
336 case THREAD_AWAKENED
:
337 /* lock transitioned from old locker to us */
338 /* he already made us owner */
339 return (ulock
->unstable
) ? KERN_LOCK_UNSTABLE
:
342 case THREAD_INTERRUPTED
:
346 goto retry
; /* probably a dead lock_set */
349 panic("lock_acquire\n");
354 * Assign lock ownership
356 ulock_ownership_set(ulock
, current_thread());
359 return (ulock
->unstable
) ? KERN_LOCK_UNSTABLE
: KERN_SUCCESS
;
363 lock_release (lock_set_t lock_set
, int lock_id
)
367 if (lock_set
== LOCK_SET_NULL
)
368 return KERN_INVALID_ARGUMENT
;
370 if (lock_id
< 0 || lock_id
>= lock_set
->n_ulocks
)
371 return KERN_INVALID_ARGUMENT
;
373 ulock
= (ulock_t
) &lock_set
->ulock_list
[lock_id
];
375 return (lock_release_internal(ulock
, current_act()));
379 lock_try (lock_set_t lock_set
, int lock_id
)
384 if (lock_set
== LOCK_SET_NULL
)
385 return KERN_INVALID_ARGUMENT
;
387 if (lock_id
< 0 || lock_id
>= lock_set
->n_ulocks
)
388 return KERN_INVALID_ARGUMENT
;
391 lock_set_lock(lock_set
);
392 if (!lock_set
->active
) {
393 lock_set_unlock(lock_set
);
394 return KERN_LOCK_SET_DESTROYED
;
397 ulock
= (ulock_t
) &lock_set
->ulock_list
[lock_id
];
399 lock_set_unlock(lock_set
);
402 * If the lock is already owned, we return without blocking.
404 * An ownership status is returned to inform the caller as to
405 * whether it already holds the lock or another thread does.
408 if (ulock
->holder
!= THR_ACT_NULL
) {
409 lock_set_unlock(lock_set
);
411 if (ulock
->holder
== current_act()) {
413 return KERN_LOCK_OWNED_SELF
;
417 return KERN_LOCK_OWNED
;
421 * Add the ulock to the lock set's held_ulocks list.
424 ulock_ownership_set(ulock
, current_thread());
427 return (ulock
->unstable
) ? KERN_LOCK_UNSTABLE
: KERN_SUCCESS
;
431 lock_make_stable (lock_set_t lock_set
, int lock_id
)
436 if (lock_set
== LOCK_SET_NULL
)
437 return KERN_INVALID_ARGUMENT
;
439 if (lock_id
< 0 || lock_id
>= lock_set
->n_ulocks
)
440 return KERN_INVALID_ARGUMENT
;
443 lock_set_lock(lock_set
);
444 if (!lock_set
->active
) {
445 lock_set_unlock(lock_set
);
446 return KERN_LOCK_SET_DESTROYED
;
449 ulock
= (ulock_t
) &lock_set
->ulock_list
[lock_id
];
451 lock_set_unlock(lock_set
);
453 if (ulock
->holder
!= current_act()) {
455 return KERN_INVALID_RIGHT
;
458 ulock
->unstable
= FALSE
;
465 * ROUTINE: lock_make_unstable [internal]
467 * Marks the lock as unstable.
470 * - All future acquisitions of the lock will return with a
471 * KERN_LOCK_UNSTABLE status, until the lock is made stable again.
474 lock_make_unstable (ulock_t ulock
, thread_act_t thr_act
)
479 lock_set
= ulock
->lock_set
;
480 lock_set_lock(lock_set
);
481 if (!lock_set
->active
) {
482 lock_set_unlock(lock_set
);
483 return KERN_LOCK_SET_DESTROYED
;
487 lock_set_unlock(lock_set
);
489 if (ulock
->holder
!= thr_act
) {
491 return KERN_INVALID_RIGHT
;
494 ulock
->unstable
= TRUE
;
501 * ROUTINE: lock_release_internal [internal]
503 * Releases the ulock.
504 * If any threads are blocked waiting for the ulock, one is woken-up.
508 lock_release_internal (ulock_t ulock
, thread_act_t thr_act
)
514 if ((lock_set
= ulock
->lock_set
) == LOCK_SET_NULL
)
515 return KERN_INVALID_ARGUMENT
;
517 lock_set_lock(lock_set
);
518 if (!lock_set
->active
) {
519 lock_set_unlock(lock_set
);
520 return KERN_LOCK_SET_DESTROYED
;
523 lock_set_unlock(lock_set
);
525 if (ulock
->holder
!= thr_act
) {
527 return KERN_INVALID_RIGHT
;
531 * If we have a hint that threads might be waiting,
532 * try to transfer the lock ownership to a waiting thread
535 if (ulock
->blocked
) {
536 wait_queue_t wq
= &ulock
->wait_queue
;
542 thread
= wait_queue_wakeup64_identity_locked(wq
,
546 /* wait_queue now unlocked, thread locked */
548 if (thread
!= THREAD_NULL
) {
550 * JMM - These ownership transfer macros have a
551 * locking/race problem. To keep the thread from
552 * changing states on us (nullifying the ownership
553 * assignment) we need to keep the thread locked
554 * during the assignment. But we can't because the
555 * macros take an activation lock, which is a mutex.
556 * Since this code was already broken before I got
557 * here, I will leave it for now.
559 thread_unlock(thread
);
563 * Transfer ulock ownership
564 * from the current thread to the acquisition thread.
566 ulock_ownership_clear(ulock
);
567 ulock_ownership_set(ulock
, thread
);
572 ulock
->blocked
= FALSE
;
580 ulock_ownership_clear(ulock
);
587 lock_handoff (lock_set_t lock_set
, int lock_id
)
593 if (lock_set
== LOCK_SET_NULL
)
594 return KERN_INVALID_ARGUMENT
;
596 if (lock_id
< 0 || lock_id
>= lock_set
->n_ulocks
)
597 return KERN_INVALID_ARGUMENT
;
600 lock_set_lock(lock_set
);
602 if (!lock_set
->active
) {
603 lock_set_unlock(lock_set
);
604 return KERN_LOCK_SET_DESTROYED
;
607 ulock
= (ulock_t
) &lock_set
->ulock_list
[lock_id
];
609 lock_set_unlock(lock_set
);
611 if (ulock
->holder
!= current_act()) {
613 return KERN_INVALID_RIGHT
;
617 * If the accepting thread (the receiver) is already waiting
618 * to accept the lock from the handoff thread (the sender),
619 * then perform the hand-off now.
622 if (ulock
->accept_wait
) {
623 wait_queue_t wq
= &ulock
->wait_queue
;
628 * See who the lucky devil is, if he is still there waiting.
632 thread
= wait_queue_wakeup64_identity_locked(
637 /* wait queue unlocked, thread locked */
640 * Transfer lock ownership
642 if (thread
!= THREAD_NULL
) {
644 * JMM - These ownership transfer macros have a
645 * locking/race problem. To keep the thread from
646 * changing states on us (nullifying the ownership
647 * assignment) we need to keep the thread locked
648 * during the assignment. But we can't because the
649 * macros take an activation lock, which is a mutex.
650 * Since this code was already broken before I got
651 * here, I will leave it for now.
653 thread_unlock(thread
);
656 ulock_ownership_clear(ulock
);
657 ulock_ownership_set(ulock
, thread
);
658 ulock
->accept_wait
= FALSE
;
664 * OOPS. The accepting thread must have been aborted.
665 * and is racing back to clear the flag that says is
666 * waiting for an accept. He will clear it when we
667 * release the lock, so just fall thru and wait for
668 * the next accept thread (that's the way it is
676 * Indicate that there is a hand-off thread waiting, and then wait
677 * for an accepting thread.
679 ulock
->ho_wait
= TRUE
;
680 wait_result
= wait_queue_assert_wait64(&ulock
->wait_queue
,
685 if (wait_result
== THREAD_WAITING
)
686 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
689 * If the thread was woken-up via some action other than
690 * lock_handoff_accept or lock_set_destroy (i.e. thread_terminate),
691 * then we need to clear the ulock's handoff state.
693 switch (wait_result
) {
695 case THREAD_AWAKENED
:
698 case THREAD_INTERRUPTED
:
700 assert(ulock
->holder
== current_act());
701 ulock
->ho_wait
= FALSE
;
709 panic("lock_handoff");
714 lock_handoff_accept (lock_set_t lock_set
, int lock_id
)
720 if (lock_set
== LOCK_SET_NULL
)
721 return KERN_INVALID_ARGUMENT
;
723 if (lock_id
< 0 || lock_id
>= lock_set
->n_ulocks
)
724 return KERN_INVALID_ARGUMENT
;
727 lock_set_lock(lock_set
);
728 if (!lock_set
->active
) {
729 lock_set_unlock(lock_set
);
730 return KERN_LOCK_SET_DESTROYED
;
733 ulock
= (ulock_t
) &lock_set
->ulock_list
[lock_id
];
735 lock_set_unlock(lock_set
);
738 * If there is another accepting thread that beat us, just
739 * return with an error.
741 if (ulock
->accept_wait
) {
743 return KERN_ALREADY_WAITING
;
746 if (ulock
->holder
== current_act()) {
748 return KERN_LOCK_OWNED_SELF
;
752 * If the handoff thread (the sender) is already waiting to
753 * hand-off the lock to the accepting thread (the receiver),
754 * then perform the hand-off now.
756 if (ulock
->ho_wait
) {
757 wait_queue_t wq
= &ulock
->wait_queue
;
761 * See who the lucky devil is, if he is still there waiting.
763 assert(ulock
->holder
!= THR_ACT_NULL
);
764 thread
= ulock
->holder
->thread
;
766 if (wait_queue_wakeup64_thread(wq
,
769 THREAD_AWAKENED
) == KERN_SUCCESS
) {
771 * Holder thread was still waiting to give it
772 * away. Take over ownership.
774 ulock_ownership_clear(ulock
);
775 ulock_ownership_set(ulock
, current_thread());
776 ulock
->ho_wait
= FALSE
;
778 return (ulock
->unstable
) ? KERN_LOCK_UNSTABLE
:
783 * OOPS. The owner was aborted out of the handoff.
784 * He will clear his own flag when he gets back.
785 * in the meantime, we will wait as if we didn't
786 * even see his flag (by falling thru).
790 ulock
->accept_wait
= TRUE
;
791 wait_result
= wait_queue_assert_wait64(&ulock
->wait_queue
,
796 if (wait_result
== THREAD_WAITING
)
797 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
800 * If the thread was woken-up via some action other than
801 * lock_handoff_accept or lock_set_destroy (i.e. thread_terminate),
802 * then we need to clear the ulock's handoff state.
804 switch (wait_result
) {
806 case THREAD_AWAKENED
:
809 case THREAD_INTERRUPTED
:
811 ulock
->accept_wait
= FALSE
;
819 panic("lock_handoff_accept");
824 * Routine: lock_set_reference
826 * Take out a reference on a lock set. This keeps the data structure
827 * in existence (but the lock set may be deactivated).
830 lock_set_reference(lock_set_t lock_set
)
832 lock_set_lock(lock_set
);
833 lock_set
->ref_count
++;
834 lock_set_unlock(lock_set
);
838 * Routine: lock_set_dereference
840 * Release a reference on a lock set. If this is the last reference,
841 * the lock set data structure is deallocated.
844 lock_set_dereference(lock_set_t lock_set
)
849 lock_set_lock(lock_set
);
850 ref_count
= --(lock_set
->ref_count
);
851 lock_set_unlock(lock_set
);
853 if (ref_count
== 0) {
854 size
= sizeof(struct lock_set
) +
855 (sizeof(struct ulock
) * (lock_set
->n_ulocks
- 1));
856 kfree((vm_offset_t
) lock_set
, size
);