2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33 * File: kern/sync_lock.c
34 * Author: Joseph CaraDonna
36 * Contains RT distributed lock synchronization services.
39 #include <mach/mach_types.h>
40 #include <mach/lock_set_server.h>
41 #include <mach/task_server.h>
43 #include <kern/misc_protos.h>
44 #include <kern/kalloc.h>
45 #include <kern/sync_lock.h>
46 #include <kern/sched_prim.h>
47 #include <kern/ipc_kobject.h>
48 #include <kern/ipc_sync.h>
49 #include <kern/thread.h>
50 #include <kern/task.h>
52 #include <ipc/ipc_port.h>
53 #include <ipc/ipc_space.h>
56 * Ulock ownership MACROS
58 * Assumes: ulock internal lock is held
61 #define ulock_ownership_set(ul, th) \
63 thread_mtx_lock(th); \
64 enqueue (&th->held_ulocks, (queue_entry_t) (ul)); \
65 thread_mtx_unlock(th); \
69 #define ulock_ownership_clear(ul) \
74 thread_mtx_lock(th); \
75 remqueue((queue_entry_t) (ul)); \
76 thread_mtx_unlock(th); \
78 remqueue((queue_entry_t) (ul)); \
80 (ul)->holder = THREAD_NULL; \
84 * Lock set ownership MACROS
87 #define lock_set_ownership_set(ls, t) \
90 enqueue_head(&(t)->lock_set_list, (queue_entry_t) (ls));\
91 (t)->lock_sets_owned++; \
96 #define lock_set_ownership_clear(ls, t) \
99 remqueue((queue_entry_t) (ls)); \
100 (t)->lock_sets_owned--; \
104 unsigned int lock_set_event
;
105 #define LOCK_SET_EVENT CAST_EVENT64_T(&lock_set_event)
107 unsigned int lock_set_handoff
;
108 #define LOCK_SET_HANDOFF CAST_EVENT64_T(&lock_set_handoff)
111 lck_attr_t lock_set_attr
;
112 lck_grp_t lock_set_grp
;
113 static lck_grp_attr_t lock_set_grp_attr
;
118 * ROUTINE: lock_set_init [private]
120 * Initialize the lock_set subsystem.
125 lck_grp_attr_setdefault(&lock_set_grp_attr
);
126 lck_grp_init(&lock_set_grp
, "lock_set", &lock_set_grp_attr
);
127 lck_attr_setdefault(&lock_set_attr
);
132 * ROUTINE: lock_set_create [exported]
134 * Creates a lock set.
135 * The port representing the lock set is returned as a parameter.
140 lock_set_t
*new_lock_set
,
144 lock_set_t lock_set
= LOCK_SET_NULL
;
149 *new_lock_set
= LOCK_SET_NULL
;
151 if (task
== TASK_NULL
|| n_ulocks
<= 0 || policy
> SYNC_POLICY_MAX
)
152 return KERN_INVALID_ARGUMENT
;
154 if ((VM_MAX_ADDRESS
- sizeof(struct lock_set
))/sizeof(struct ulock
) < (unsigned)n_ulocks
)
155 return KERN_RESOURCE_SHORTAGE
;
157 size
= sizeof(struct lock_set
) + (sizeof(struct ulock
) * (n_ulocks
-1));
158 lock_set
= (lock_set_t
) kalloc (size
);
160 if (lock_set
== LOCK_SET_NULL
)
161 return KERN_RESOURCE_SHORTAGE
;
164 lock_set_lock_init(lock_set
);
165 lock_set
->n_ulocks
= n_ulocks
;
166 lock_set
->ref_count
= (task
== kernel_task
) ? 1 : 2; /* one for kernel, one for port */
169 * Create and initialize the lock set port
171 lock_set
->port
= ipc_port_alloc_kernel();
172 if (lock_set
->port
== IP_NULL
) {
173 kfree(lock_set
, size
);
174 return KERN_RESOURCE_SHORTAGE
;
177 ipc_kobject_set (lock_set
->port
,
178 (ipc_kobject_t
) lock_set
,
182 * Initialize each ulock in the lock set
185 for (x
=0; x
< n_ulocks
; x
++) {
186 ulock
= (ulock_t
) &lock_set
->ulock_list
[x
];
187 ulock_lock_init(ulock
);
188 ulock
->lock_set
= lock_set
;
189 ulock
->holder
= THREAD_NULL
;
190 ulock
->blocked
= FALSE
;
191 ulock
->unstable
= FALSE
;
192 ulock
->ho_wait
= FALSE
;
193 ulock
->accept_wait
= FALSE
;
194 wait_queue_init(&ulock
->wait_queue
, policy
);
197 lock_set_ownership_set(lock_set
, task
);
199 lock_set
->active
= TRUE
;
200 *new_lock_set
= lock_set
;
206 * ROUTINE: lock_set_destroy [exported]
208 * Destroys a lock set. This call will only succeed if the
209 * specified task is the SAME task name specified at the lock set's
213 * - All threads currently blocked on the lock set's ulocks are awoken.
214 * - These threads will return with the KERN_LOCK_SET_DESTROYED error.
217 lock_set_destroy (task_t task
, lock_set_t lock_set
)
222 if (task
== TASK_NULL
|| lock_set
== LOCK_SET_NULL
)
223 return KERN_INVALID_ARGUMENT
;
225 if (lock_set
->owner
!= task
)
226 return KERN_INVALID_RIGHT
;
228 lock_set_lock(lock_set
);
229 if (!lock_set
->active
) {
230 lock_set_unlock(lock_set
);
231 return KERN_LOCK_SET_DESTROYED
;
235 * Deactivate lock set
237 lock_set
->active
= FALSE
;
240 * If a ulock is currently held in the target lock set:
242 * 1) Wakeup all threads blocked on the ulock (if any). Threads
243 * may be blocked waiting normally, or waiting for a handoff.
244 * Blocked threads will return with KERN_LOCK_SET_DESTROYED.
246 * 2) ulock ownership is cleared.
247 * The thread currently holding the ulock is revoked of its
250 for (i
= 0; i
< lock_set
->n_ulocks
; i
++) {
251 ulock
= &lock_set
->ulock_list
[i
];
255 if (ulock
->accept_wait
) {
256 ulock
->accept_wait
= FALSE
;
257 wait_queue_wakeup64_one(&ulock
->wait_queue
,
263 if (ulock
->blocked
) {
264 ulock
->blocked
= FALSE
;
265 wait_queue_wakeup64_all(&ulock
->wait_queue
,
269 if (ulock
->ho_wait
) {
270 ulock
->ho_wait
= FALSE
;
271 wait_queue_wakeup64_one(&ulock
->wait_queue
,
275 ulock_ownership_clear(ulock
);
281 lock_set_unlock(lock_set
);
282 lock_set_ownership_clear(lock_set
, task
);
285 * Drop the lock set reference given to the containing task,
286 * which inturn destroys the lock set structure if the reference
287 * count goes to zero.
289 lock_set_dereference(lock_set
);
295 lock_acquire (lock_set_t lock_set
, int lock_id
)
299 if (lock_set
== LOCK_SET_NULL
)
300 return KERN_INVALID_ARGUMENT
;
302 if (lock_id
< 0 || lock_id
>= lock_set
->n_ulocks
)
303 return KERN_INVALID_ARGUMENT
;
306 lock_set_lock(lock_set
);
307 if (!lock_set
->active
) {
308 lock_set_unlock(lock_set
);
309 return KERN_LOCK_SET_DESTROYED
;
312 ulock
= (ulock_t
) &lock_set
->ulock_list
[lock_id
];
314 lock_set_unlock(lock_set
);
317 * Block the current thread if the lock is already held.
320 if (ulock
->holder
!= THREAD_NULL
) {
323 if (ulock
->holder
== current_thread()) {
325 return KERN_LOCK_OWNED_SELF
;
328 ulock
->blocked
= TRUE
;
329 wait_result
= wait_queue_assert_wait64(&ulock
->wait_queue
,
331 THREAD_ABORTSAFE
, 0);
335 * Block - Wait for lock to become available.
337 if (wait_result
== THREAD_WAITING
)
338 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
341 * Check the result status:
343 * Check to see why thread was woken up. In all cases, we
344 * already have been removed from the queue.
346 switch (wait_result
) {
347 case THREAD_AWAKENED
:
348 /* lock transitioned from old locker to us */
349 /* he already made us owner */
350 return (ulock
->unstable
) ? KERN_LOCK_UNSTABLE
:
353 case THREAD_INTERRUPTED
:
357 goto retry
; /* probably a dead lock_set */
360 panic("lock_acquire\n");
365 * Assign lock ownership
367 ulock_ownership_set(ulock
, current_thread());
370 return (ulock
->unstable
) ? KERN_LOCK_UNSTABLE
: KERN_SUCCESS
;
374 lock_release (lock_set_t lock_set
, int lock_id
)
378 if (lock_set
== LOCK_SET_NULL
)
379 return KERN_INVALID_ARGUMENT
;
381 if (lock_id
< 0 || lock_id
>= lock_set
->n_ulocks
)
382 return KERN_INVALID_ARGUMENT
;
384 ulock
= (ulock_t
) &lock_set
->ulock_list
[lock_id
];
386 return (ulock_release_internal(ulock
, current_thread()));
390 lock_try (lock_set_t lock_set
, int lock_id
)
395 if (lock_set
== LOCK_SET_NULL
)
396 return KERN_INVALID_ARGUMENT
;
398 if (lock_id
< 0 || lock_id
>= lock_set
->n_ulocks
)
399 return KERN_INVALID_ARGUMENT
;
402 lock_set_lock(lock_set
);
403 if (!lock_set
->active
) {
404 lock_set_unlock(lock_set
);
405 return KERN_LOCK_SET_DESTROYED
;
408 ulock
= (ulock_t
) &lock_set
->ulock_list
[lock_id
];
410 lock_set_unlock(lock_set
);
413 * If the lock is already owned, we return without blocking.
415 * An ownership status is returned to inform the caller as to
416 * whether it already holds the lock or another thread does.
419 if (ulock
->holder
!= THREAD_NULL
) {
420 lock_set_unlock(lock_set
);
422 if (ulock
->holder
== current_thread()) {
424 return KERN_LOCK_OWNED_SELF
;
428 return KERN_LOCK_OWNED
;
432 * Add the ulock to the lock set's held_ulocks list.
435 ulock_ownership_set(ulock
, current_thread());
438 return (ulock
->unstable
) ? KERN_LOCK_UNSTABLE
: KERN_SUCCESS
;
442 lock_make_stable (lock_set_t lock_set
, int lock_id
)
447 if (lock_set
== LOCK_SET_NULL
)
448 return KERN_INVALID_ARGUMENT
;
450 if (lock_id
< 0 || lock_id
>= lock_set
->n_ulocks
)
451 return KERN_INVALID_ARGUMENT
;
454 lock_set_lock(lock_set
);
455 if (!lock_set
->active
) {
456 lock_set_unlock(lock_set
);
457 return KERN_LOCK_SET_DESTROYED
;
460 ulock
= (ulock_t
) &lock_set
->ulock_list
[lock_id
];
462 lock_set_unlock(lock_set
);
464 if (ulock
->holder
!= current_thread()) {
466 return KERN_INVALID_RIGHT
;
469 ulock
->unstable
= FALSE
;
476 * ROUTINE: lock_make_unstable [internal]
478 * Marks the lock as unstable.
481 * - All future acquisitions of the lock will return with a
482 * KERN_LOCK_UNSTABLE status, until the lock is made stable again.
485 lock_make_unstable (ulock_t ulock
, thread_t thread
)
489 lock_set
= ulock
->lock_set
;
490 lock_set_lock(lock_set
);
491 if (!lock_set
->active
) {
492 lock_set_unlock(lock_set
);
493 return KERN_LOCK_SET_DESTROYED
;
497 lock_set_unlock(lock_set
);
499 if (ulock
->holder
!= thread
) {
501 return KERN_INVALID_RIGHT
;
504 ulock
->unstable
= TRUE
;
511 * ROUTINE: ulock_release_internal [internal]
513 * Releases the ulock.
514 * If any threads are blocked waiting for the ulock, one is woken-up.
518 ulock_release_internal (ulock_t ulock
, thread_t thread
)
522 if ((lock_set
= ulock
->lock_set
) == LOCK_SET_NULL
)
523 return KERN_INVALID_ARGUMENT
;
525 lock_set_lock(lock_set
);
526 if (!lock_set
->active
) {
527 lock_set_unlock(lock_set
);
528 return KERN_LOCK_SET_DESTROYED
;
531 lock_set_unlock(lock_set
);
533 if (ulock
->holder
!= thread
) {
535 return KERN_INVALID_RIGHT
;
539 * If we have a hint that threads might be waiting,
540 * try to transfer the lock ownership to a waiting thread
543 if (ulock
->blocked
) {
544 wait_queue_t wq
= &ulock
->wait_queue
;
550 wqthread
= wait_queue_wakeup64_identity_locked(wq
,
554 /* wait_queue now unlocked, thread locked */
556 if (wqthread
!= THREAD_NULL
) {
557 thread_unlock(wqthread
);
561 * Transfer ulock ownership
562 * from the current thread to the acquisition thread.
564 ulock_ownership_clear(ulock
);
565 ulock_ownership_set(ulock
, wqthread
);
570 ulock
->blocked
= FALSE
;
578 ulock_ownership_clear(ulock
);
585 lock_handoff (lock_set_t lock_set
, int lock_id
)
591 if (lock_set
== LOCK_SET_NULL
)
592 return KERN_INVALID_ARGUMENT
;
594 if (lock_id
< 0 || lock_id
>= lock_set
->n_ulocks
)
595 return KERN_INVALID_ARGUMENT
;
598 lock_set_lock(lock_set
);
600 if (!lock_set
->active
) {
601 lock_set_unlock(lock_set
);
602 return KERN_LOCK_SET_DESTROYED
;
605 ulock
= (ulock_t
) &lock_set
->ulock_list
[lock_id
];
607 lock_set_unlock(lock_set
);
609 if (ulock
->holder
!= current_thread()) {
611 return KERN_INVALID_RIGHT
;
615 * If the accepting thread (the receiver) is already waiting
616 * to accept the lock from the handoff thread (the sender),
617 * then perform the hand-off now.
620 if (ulock
->accept_wait
) {
621 wait_queue_t wq
= &ulock
->wait_queue
;
626 * See who the lucky devil is, if he is still there waiting.
630 thread
= wait_queue_wakeup64_identity_locked(
635 /* wait queue unlocked, thread locked */
638 * Transfer lock ownership
640 if (thread
!= THREAD_NULL
) {
642 * The thread we are transferring to will try
643 * to take the lock on the ulock, and therefore
644 * will wait for us complete the handoff even
645 * through we set the thread running.
647 thread_unlock(thread
);
650 ulock_ownership_clear(ulock
);
651 ulock_ownership_set(ulock
, thread
);
652 ulock
->accept_wait
= FALSE
;
658 * OOPS. The accepting thread must have been aborted.
659 * and is racing back to clear the flag that says is
660 * waiting for an accept. He will clear it when we
661 * release the lock, so just fall thru and wait for
662 * the next accept thread (that's the way it is
670 * Indicate that there is a hand-off thread waiting, and then wait
671 * for an accepting thread.
673 ulock
->ho_wait
= TRUE
;
674 wait_result
= wait_queue_assert_wait64(&ulock
->wait_queue
,
676 THREAD_ABORTSAFE
, 0);
679 if (wait_result
== THREAD_WAITING
)
680 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
683 * If the thread was woken-up via some action other than
684 * lock_handoff_accept or lock_set_destroy (i.e. thread_terminate),
685 * then we need to clear the ulock's handoff state.
687 switch (wait_result
) {
690 case THREAD_AWAKENED
:
692 * we take the ulock lock to syncronize with the
693 * thread that is accepting ownership.
696 assert(ulock
->holder
!= current_thread());
700 case THREAD_INTERRUPTED
:
702 assert(ulock
->holder
== current_thread());
703 ulock
->ho_wait
= FALSE
;
711 panic("lock_handoff");
716 lock_handoff_accept (lock_set_t lock_set
, int lock_id
)
722 if (lock_set
== LOCK_SET_NULL
)
723 return KERN_INVALID_ARGUMENT
;
725 if (lock_id
< 0 || lock_id
>= lock_set
->n_ulocks
)
726 return KERN_INVALID_ARGUMENT
;
729 lock_set_lock(lock_set
);
730 if (!lock_set
->active
) {
731 lock_set_unlock(lock_set
);
732 return KERN_LOCK_SET_DESTROYED
;
735 ulock
= (ulock_t
) &lock_set
->ulock_list
[lock_id
];
737 lock_set_unlock(lock_set
);
740 * If there is another accepting thread that beat us, just
741 * return with an error.
743 if (ulock
->accept_wait
) {
745 return KERN_ALREADY_WAITING
;
748 if (ulock
->holder
== current_thread()) {
750 return KERN_LOCK_OWNED_SELF
;
754 * If the handoff thread (the sender) is already waiting to
755 * hand-off the lock to the accepting thread (the receiver),
756 * then perform the hand-off now.
758 if (ulock
->ho_wait
) {
759 wait_queue_t wq
= &ulock
->wait_queue
;
762 * See who the lucky devil is, if he is still there waiting.
764 assert(ulock
->holder
!= THREAD_NULL
);
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
,
793 THREAD_ABORTSAFE
, 0);
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
:
808 * Take the lock to synchronize with the thread handing
809 * off the lock to us. We don't want to continue until
810 * they complete the handoff.
813 assert(ulock
->accept_wait
== FALSE
);
814 assert(ulock
->holder
== current_thread());
818 case THREAD_INTERRUPTED
:
820 ulock
->accept_wait
= FALSE
;
828 panic("lock_handoff_accept");
833 * Routine: lock_set_reference
835 * Take out a reference on a lock set. This keeps the data structure
836 * in existence (but the lock set may be deactivated).
839 lock_set_reference(lock_set_t lock_set
)
841 lock_set_lock(lock_set
);
842 lock_set
->ref_count
++;
843 lock_set_unlock(lock_set
);
847 * Routine: lock_set_dereference
849 * Release a reference on a lock set. If this is the last reference,
850 * the lock set data structure is deallocated.
853 lock_set_dereference(lock_set_t lock_set
)
858 lock_set_lock(lock_set
);
859 ref_count
= --(lock_set
->ref_count
);
860 lock_set_unlock(lock_set
);
862 if (ref_count
== 0) {
863 ipc_port_dealloc_kernel(lock_set
->port
);
864 size
= (int)(sizeof(struct lock_set
) +
865 (sizeof(struct ulock
) * (lock_set
->n_ulocks
- 1)));
866 kfree(lock_set
, size
);
876 while (!queue_empty(&thread
->held_ulocks
)) {
877 ulock
= (ulock_t
)queue_first(&thread
->held_ulocks
);
878 lock_make_unstable(ulock
, thread
);
879 ulock_release_internal(ulock
, thread
);