]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sync_sema.c
xnu-1699.24.8.tar.gz
[apple/xnu.git] / osfmk / kern / sync_sema.c
1 /*
2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 *
31 */
32 /*
33 * File: kern/sync_sema.c
34 * Author: Joseph CaraDonna
35 *
36 * Contains RT distributed semaphore synchronization services.
37 */
38
39 #include <mach/mach_types.h>
40 #include <mach/mach_traps.h>
41 #include <mach/kern_return.h>
42 #include <mach/semaphore.h>
43 #include <mach/sync_policy.h>
44 #include <mach/task.h>
45
46 #include <kern/misc_protos.h>
47 #include <kern/sync_sema.h>
48 #include <kern/spl.h>
49 #include <kern/ipc_kobject.h>
50 #include <kern/ipc_sync.h>
51 #include <kern/ipc_tt.h>
52 #include <kern/thread.h>
53 #include <kern/clock.h>
54 #include <ipc/ipc_port.h>
55 #include <ipc/ipc_space.h>
56 #include <kern/host.h>
57 #include <kern/wait_queue.h>
58 #include <kern/zalloc.h>
59 #include <kern/mach_param.h>
60
61 static unsigned int semaphore_event;
62 #define SEMAPHORE_EVENT CAST_EVENT64_T(&semaphore_event)
63
64 zone_t semaphore_zone;
65 unsigned int semaphore_max;
66
67 /* Forward declarations */
68
69
70 kern_return_t
71 semaphore_wait_trap_internal(
72 mach_port_name_t name,
73 void (*caller_cont)(kern_return_t));
74
75 kern_return_t
76 semaphore_wait_signal_trap_internal(
77 mach_port_name_t wait_name,
78 mach_port_name_t signal_name,
79 void (*caller_cont)(kern_return_t));
80
81 kern_return_t
82 semaphore_timedwait_trap_internal(
83 mach_port_name_t name,
84 unsigned int sec,
85 clock_res_t nsec,
86 void (*caller_cont)(kern_return_t));
87
88 kern_return_t
89 semaphore_timedwait_signal_trap_internal(
90 mach_port_name_t wait_name,
91 mach_port_name_t signal_name,
92 unsigned int sec,
93 clock_res_t nsec,
94 void (*caller_cont)(kern_return_t));
95
96 kern_return_t
97 semaphore_signal_internal_trap(mach_port_name_t sema_name);
98
99 kern_return_t
100 semaphore_signal_internal(
101 semaphore_t semaphore,
102 thread_t thread,
103 int options);
104
105 kern_return_t
106 semaphore_convert_wait_result(
107 int wait_result);
108
109 void
110 semaphore_wait_continue(void);
111
112 static kern_return_t
113 semaphore_wait_internal(
114 semaphore_t wait_semaphore,
115 semaphore_t signal_semaphore,
116 uint64_t deadline,
117 int option,
118 void (*caller_cont)(kern_return_t));
119
120 static __inline__ uint64_t
121 semaphore_deadline(
122 unsigned int sec,
123 clock_res_t nsec)
124 {
125 uint64_t abstime;
126
127 nanoseconds_to_absolutetime((uint64_t)sec * NSEC_PER_SEC + nsec, &abstime);
128 clock_absolutetime_interval_to_deadline(abstime, &abstime);
129
130 return (abstime);
131 }
132
133 /*
134 * ROUTINE: semaphore_init [private]
135 *
136 * Initialize the semaphore mechanisms.
137 * Right now, we only need to initialize the semaphore zone.
138 */
139 void
140 semaphore_init(void)
141 {
142 semaphore_zone = zinit(sizeof(struct semaphore),
143 semaphore_max * sizeof(struct semaphore),
144 sizeof(struct semaphore),
145 "semaphores");
146 zone_change(semaphore_zone, Z_NOENCRYPT, TRUE);
147 }
148
149 /*
150 * Routine: semaphore_create
151 *
152 * Creates a semaphore.
153 * The port representing the semaphore is returned as a parameter.
154 */
155 kern_return_t
156 semaphore_create(
157 task_t task,
158 semaphore_t *new_semaphore,
159 int policy,
160 int value)
161 {
162 semaphore_t s = SEMAPHORE_NULL;
163 kern_return_t kret;
164
165
166 *new_semaphore = SEMAPHORE_NULL;
167 if (task == TASK_NULL || value < 0 || policy > SYNC_POLICY_MAX)
168 return KERN_INVALID_ARGUMENT;
169
170 s = (semaphore_t) zalloc (semaphore_zone);
171
172 if (s == SEMAPHORE_NULL)
173 return KERN_RESOURCE_SHORTAGE;
174
175 kret = wait_queue_init(&s->wait_queue, policy); /* also inits lock */
176 if (kret != KERN_SUCCESS) {
177 zfree(semaphore_zone, s);
178 return kret;
179 }
180
181 s->count = value;
182 s->ref_count = (task == kernel_task) ? 1 : 2;
183
184 /*
185 * Create and initialize the semaphore port
186 */
187 s->port = ipc_port_alloc_kernel();
188 if (s->port == IP_NULL) {
189 zfree(semaphore_zone, s);
190 return KERN_RESOURCE_SHORTAGE;
191 }
192
193 ipc_kobject_set (s->port, (ipc_kobject_t) s, IKOT_SEMAPHORE);
194
195 /*
196 * Associate the new semaphore with the task by adding
197 * the new semaphore to the task's semaphore list.
198 *
199 * Associate the task with the new semaphore by having the
200 * semaphores task pointer point to the owning task's structure.
201 */
202 task_lock(task);
203 enqueue_head(&task->semaphore_list, (queue_entry_t) s);
204 task->semaphores_owned++;
205 s->owner = task;
206 s->active = TRUE;
207 task_unlock(task);
208
209 *new_semaphore = s;
210
211 return KERN_SUCCESS;
212 }
213
214 /*
215 * Routine: semaphore_destroy
216 *
217 * Destroys a semaphore. This call will only succeed if the
218 * specified task is the SAME task name specified at the semaphore's
219 * creation.
220 *
221 * All threads currently blocked on the semaphore are awoken. These
222 * threads will return with the KERN_TERMINATED error.
223 */
224 kern_return_t
225 semaphore_destroy(
226 task_t task,
227 semaphore_t semaphore)
228 {
229 int old_count;
230 spl_t spl_level;
231
232
233 if (task == TASK_NULL || semaphore == SEMAPHORE_NULL)
234 return KERN_INVALID_ARGUMENT;
235
236 /*
237 * Disown semaphore
238 */
239 task_lock(task);
240 if (semaphore->owner != task) {
241 task_unlock(task);
242 return KERN_INVALID_ARGUMENT;
243 }
244 remqueue((queue_entry_t) semaphore);
245 semaphore->owner = TASK_NULL;
246 task->semaphores_owned--;
247 task_unlock(task);
248
249 spl_level = splsched();
250 semaphore_lock(semaphore);
251
252 /*
253 * Deactivate semaphore
254 */
255 assert(semaphore->active);
256 semaphore->active = FALSE;
257
258 /*
259 * Wakeup blocked threads
260 */
261 old_count = semaphore->count;
262 semaphore->count = 0;
263
264 if (old_count < 0) {
265 wait_queue_wakeup64_all_locked(&semaphore->wait_queue,
266 SEMAPHORE_EVENT,
267 THREAD_RESTART,
268 TRUE); /* unlock? */
269 } else {
270 semaphore_unlock(semaphore);
271 }
272 splx(spl_level);
273
274 /*
275 * Deallocate
276 *
277 * Drop the task's semaphore reference, which in turn deallocates
278 * the semaphore structure if the reference count goes to zero.
279 */
280 semaphore_dereference(semaphore);
281 return KERN_SUCCESS;
282 }
283
284 /*
285 * Routine: semaphore_signal_internal
286 *
287 * Signals the semaphore as direct.
288 * Assumptions:
289 * Semaphore is locked.
290 */
291 kern_return_t
292 semaphore_signal_internal(
293 semaphore_t semaphore,
294 thread_t thread,
295 int options)
296 {
297 kern_return_t kr;
298 spl_t spl_level;
299
300 spl_level = splsched();
301 semaphore_lock(semaphore);
302
303 if (!semaphore->active) {
304 semaphore_unlock(semaphore);
305 splx(spl_level);
306 return KERN_TERMINATED;
307 }
308
309 if (thread != THREAD_NULL) {
310 if (semaphore->count < 0) {
311 kr = wait_queue_wakeup64_thread_locked(
312 &semaphore->wait_queue,
313 SEMAPHORE_EVENT,
314 thread,
315 THREAD_AWAKENED,
316 TRUE); /* unlock? */
317 } else {
318 semaphore_unlock(semaphore);
319 kr = KERN_NOT_WAITING;
320 }
321 splx(spl_level);
322 return kr;
323 }
324
325 if (options & SEMAPHORE_SIGNAL_ALL) {
326 int old_count = semaphore->count;
327
328 if (old_count < 0) {
329 semaphore->count = 0; /* always reset */
330 kr = wait_queue_wakeup64_all_locked(
331 &semaphore->wait_queue,
332 SEMAPHORE_EVENT,
333 THREAD_AWAKENED,
334 TRUE); /* unlock? */
335 } else {
336 if (options & SEMAPHORE_SIGNAL_PREPOST)
337 semaphore->count++;
338 semaphore_unlock(semaphore);
339 kr = KERN_SUCCESS;
340 }
341 splx(spl_level);
342 return kr;
343 }
344
345 if (semaphore->count < 0) {
346 if (wait_queue_wakeup64_one_locked(
347 &semaphore->wait_queue,
348 SEMAPHORE_EVENT,
349 THREAD_AWAKENED,
350 FALSE) == KERN_SUCCESS) {
351 semaphore_unlock(semaphore);
352 splx(spl_level);
353 return KERN_SUCCESS;
354 } else
355 semaphore->count = 0; /* all waiters gone */
356 }
357
358 if (options & SEMAPHORE_SIGNAL_PREPOST) {
359 semaphore->count++;
360 }
361
362 semaphore_unlock(semaphore);
363 splx(spl_level);
364 return KERN_NOT_WAITING;
365 }
366
367 /*
368 * Routine: semaphore_signal_thread
369 *
370 * If the specified thread is blocked on the semaphore, it is
371 * woken up. If a NULL thread was supplied, then any one
372 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
373 * and the semaphore is unchanged.
374 */
375 kern_return_t
376 semaphore_signal_thread(
377 semaphore_t semaphore,
378 thread_t thread)
379 {
380 kern_return_t ret;
381
382 if (semaphore == SEMAPHORE_NULL)
383 return KERN_INVALID_ARGUMENT;
384
385 ret = semaphore_signal_internal(semaphore,
386 thread,
387 SEMAPHORE_OPTION_NONE);
388 return ret;
389 }
390
391 /*
392 * Routine: semaphore_signal_thread_trap
393 *
394 * Trap interface to the semaphore_signal_thread function.
395 */
396 kern_return_t
397 semaphore_signal_thread_trap(
398 struct semaphore_signal_thread_trap_args *args)
399 {
400 mach_port_name_t sema_name = args->signal_name;
401 mach_port_name_t thread_name = args->thread_name;
402 semaphore_t semaphore;
403 thread_t thread;
404 kern_return_t kr;
405
406 /*
407 * MACH_PORT_NULL is not an error. It means that we want to
408 * select any one thread that is already waiting, but not to
409 * pre-post the semaphore.
410 */
411 if (thread_name != MACH_PORT_NULL) {
412 thread = port_name_to_thread(thread_name);
413 if (thread == THREAD_NULL)
414 return KERN_INVALID_ARGUMENT;
415 } else
416 thread = THREAD_NULL;
417
418 kr = port_name_to_semaphore(sema_name, &semaphore);
419 if (kr == KERN_SUCCESS) {
420 kr = semaphore_signal_internal(semaphore,
421 thread,
422 SEMAPHORE_OPTION_NONE);
423 semaphore_dereference(semaphore);
424 }
425 if (thread != THREAD_NULL) {
426 thread_deallocate(thread);
427 }
428 return kr;
429 }
430
431
432
433 /*
434 * Routine: semaphore_signal
435 *
436 * Traditional (in-kernel client and MIG interface) semaphore
437 * signal routine. Most users will access the trap version.
438 *
439 * This interface in not defined to return info about whether
440 * this call found a thread waiting or not. The internal
441 * routines (and future external routines) do. We have to
442 * convert those into plain KERN_SUCCESS returns.
443 */
444 kern_return_t
445 semaphore_signal(
446 semaphore_t semaphore)
447 {
448 kern_return_t kr;
449
450 if (semaphore == SEMAPHORE_NULL)
451 return KERN_INVALID_ARGUMENT;
452
453 kr = semaphore_signal_internal(semaphore,
454 THREAD_NULL,
455 SEMAPHORE_SIGNAL_PREPOST);
456 if (kr == KERN_NOT_WAITING)
457 return KERN_SUCCESS;
458 return kr;
459 }
460
461 /*
462 * Routine: semaphore_signal_trap
463 *
464 * Trap interface to the semaphore_signal function.
465 */
466 kern_return_t
467 semaphore_signal_trap(
468 struct semaphore_signal_trap_args *args)
469 {
470 mach_port_name_t sema_name = args->signal_name;
471
472 return (semaphore_signal_internal_trap(sema_name));
473 }
474
475 kern_return_t
476 semaphore_signal_internal_trap(mach_port_name_t sema_name)
477 {
478 semaphore_t semaphore;
479 kern_return_t kr;
480
481 kr = port_name_to_semaphore(sema_name, &semaphore);
482 if (kr == KERN_SUCCESS) {
483 kr = semaphore_signal_internal(semaphore,
484 THREAD_NULL,
485 SEMAPHORE_SIGNAL_PREPOST);
486 semaphore_dereference(semaphore);
487 if (kr == KERN_NOT_WAITING)
488 kr = KERN_SUCCESS;
489 }
490 return kr;
491 }
492
493 /*
494 * Routine: semaphore_signal_all
495 *
496 * Awakens ALL threads currently blocked on the semaphore.
497 * The semaphore count returns to zero.
498 */
499 kern_return_t
500 semaphore_signal_all(
501 semaphore_t semaphore)
502 {
503 kern_return_t kr;
504
505 if (semaphore == SEMAPHORE_NULL)
506 return KERN_INVALID_ARGUMENT;
507
508 kr = semaphore_signal_internal(semaphore,
509 THREAD_NULL,
510 SEMAPHORE_SIGNAL_ALL);
511 if (kr == KERN_NOT_WAITING)
512 return KERN_SUCCESS;
513 return kr;
514 }
515
516 /*
517 * Routine: semaphore_signal_all_trap
518 *
519 * Trap interface to the semaphore_signal_all function.
520 */
521 kern_return_t
522 semaphore_signal_all_trap(
523 struct semaphore_signal_all_trap_args *args)
524 {
525 mach_port_name_t sema_name = args->signal_name;
526 semaphore_t semaphore;
527 kern_return_t kr;
528
529 kr = port_name_to_semaphore(sema_name, &semaphore);
530 if (kr == KERN_SUCCESS) {
531 kr = semaphore_signal_internal(semaphore,
532 THREAD_NULL,
533 SEMAPHORE_SIGNAL_ALL);
534 semaphore_dereference(semaphore);
535 if (kr == KERN_NOT_WAITING)
536 kr = KERN_SUCCESS;
537 }
538 return kr;
539 }
540
541 /*
542 * Routine: semaphore_convert_wait_result
543 *
544 * Generate the return code after a semaphore wait/block. It
545 * takes the wait result as an input and coverts that to an
546 * appropriate result.
547 */
548 kern_return_t
549 semaphore_convert_wait_result(int wait_result)
550 {
551 switch (wait_result) {
552 case THREAD_AWAKENED:
553 return KERN_SUCCESS;
554
555 case THREAD_TIMED_OUT:
556 return KERN_OPERATION_TIMED_OUT;
557
558 case THREAD_INTERRUPTED:
559 return KERN_ABORTED;
560
561 case THREAD_RESTART:
562 return KERN_TERMINATED;
563
564 default:
565 panic("semaphore_block\n");
566 return KERN_FAILURE;
567 }
568 }
569
570 /*
571 * Routine: semaphore_wait_continue
572 *
573 * Common continuation routine after waiting on a semphore.
574 * It returns directly to user space.
575 */
576 void
577 semaphore_wait_continue(void)
578 {
579 thread_t self = current_thread();
580 int wait_result = self->wait_result;
581 void (*caller_cont)(kern_return_t) = self->sth_continuation;
582
583 assert(self->sth_waitsemaphore != SEMAPHORE_NULL);
584 semaphore_dereference(self->sth_waitsemaphore);
585 if (self->sth_signalsemaphore != SEMAPHORE_NULL)
586 semaphore_dereference(self->sth_signalsemaphore);
587
588 assert(caller_cont != (void (*)(kern_return_t))0);
589 (*caller_cont)(semaphore_convert_wait_result(wait_result));
590 }
591
592 /*
593 * Routine: semaphore_wait_internal
594 *
595 * Decrements the semaphore count by one. If the count is
596 * negative after the decrement, the calling thread blocks
597 * (possibly at a continuation and/or with a timeout).
598 *
599 * Assumptions:
600 * The reference
601 * A reference is held on the signal semaphore.
602 */
603 static kern_return_t
604 semaphore_wait_internal(
605 semaphore_t wait_semaphore,
606 semaphore_t signal_semaphore,
607 uint64_t deadline,
608 int option,
609 void (*caller_cont)(kern_return_t))
610 {
611 int wait_result;
612 spl_t spl_level;
613 kern_return_t kr = KERN_ALREADY_WAITING;
614
615 spl_level = splsched();
616 semaphore_lock(wait_semaphore);
617
618 if (!wait_semaphore->active) {
619 kr = KERN_TERMINATED;
620 } else if (wait_semaphore->count > 0) {
621 wait_semaphore->count--;
622 kr = KERN_SUCCESS;
623 } else if (option & SEMAPHORE_TIMEOUT_NOBLOCK) {
624 kr = KERN_OPERATION_TIMED_OUT;
625 } else {
626 thread_t self = current_thread();
627
628 wait_semaphore->count = -1; /* we don't keep an actual count */
629 thread_lock(self);
630 (void)wait_queue_assert_wait64_locked(
631 &wait_semaphore->wait_queue,
632 SEMAPHORE_EVENT,
633 THREAD_ABORTSAFE, deadline,
634 self);
635 thread_unlock(self);
636 }
637 semaphore_unlock(wait_semaphore);
638 splx(spl_level);
639
640 /*
641 * wait_semaphore is unlocked so we are free to go ahead and
642 * signal the signal_semaphore (if one was provided).
643 */
644 if (signal_semaphore != SEMAPHORE_NULL) {
645 kern_return_t signal_kr;
646
647 /*
648 * lock the signal semaphore reference we got and signal it.
649 * This will NOT block (we cannot block after having asserted
650 * our intention to wait above).
651 */
652 signal_kr = semaphore_signal_internal(signal_semaphore,
653 THREAD_NULL,
654 SEMAPHORE_SIGNAL_PREPOST);
655
656 if (signal_kr == KERN_NOT_WAITING)
657 signal_kr = KERN_SUCCESS;
658 else if (signal_kr == KERN_TERMINATED) {
659 /*
660 * Uh!Oh! The semaphore we were to signal died.
661 * We have to get ourselves out of the wait in
662 * case we get stuck here forever (it is assumed
663 * that the semaphore we were posting is gating
664 * the decision by someone else to post the
665 * semaphore we are waiting on). People will
666 * discover the other dead semaphore soon enough.
667 * If we got out of the wait cleanly (someone
668 * already posted a wakeup to us) then return that
669 * (most important) result. Otherwise,
670 * return the KERN_TERMINATED status.
671 */
672 thread_t self = current_thread();
673
674 clear_wait(self, THREAD_INTERRUPTED);
675 kr = semaphore_convert_wait_result(self->wait_result);
676 if (kr == KERN_ABORTED)
677 kr = KERN_TERMINATED;
678 }
679 }
680
681 /*
682 * If we had an error, or we didn't really need to wait we can
683 * return now that we have signalled the signal semaphore.
684 */
685 if (kr != KERN_ALREADY_WAITING)
686 return kr;
687
688 /*
689 * Now, we can block. If the caller supplied a continuation
690 * pointer of his own for after the block, block with the
691 * appropriate semaphore continuation. Thiswill gather the
692 * semaphore results, release references on the semaphore(s),
693 * and then call the caller's continuation.
694 */
695 if (caller_cont) {
696 thread_t self = current_thread();
697
698 self->sth_continuation = caller_cont;
699 self->sth_waitsemaphore = wait_semaphore;
700 self->sth_signalsemaphore = signal_semaphore;
701 wait_result = thread_block((thread_continue_t)semaphore_wait_continue);
702 }
703 else {
704 wait_result = thread_block(THREAD_CONTINUE_NULL);
705 }
706
707 return (semaphore_convert_wait_result(wait_result));
708 }
709
710
711 /*
712 * Routine: semaphore_wait
713 *
714 * Traditional (non-continuation) interface presented to
715 * in-kernel clients to wait on a semaphore.
716 */
717 kern_return_t
718 semaphore_wait(
719 semaphore_t semaphore)
720 {
721
722 if (semaphore == SEMAPHORE_NULL)
723 return KERN_INVALID_ARGUMENT;
724
725 return(semaphore_wait_internal(semaphore,
726 SEMAPHORE_NULL,
727 0ULL, SEMAPHORE_OPTION_NONE,
728 (void (*)(kern_return_t))0));
729 }
730
731 kern_return_t
732 semaphore_wait_noblock(
733 semaphore_t semaphore)
734 {
735
736 if (semaphore == SEMAPHORE_NULL)
737 return KERN_INVALID_ARGUMENT;
738
739 return(semaphore_wait_internal(semaphore,
740 SEMAPHORE_NULL,
741 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK,
742 (void (*)(kern_return_t))0));
743 }
744
745 kern_return_t
746 semaphore_wait_deadline(
747 semaphore_t semaphore,
748 uint64_t deadline)
749 {
750
751 if (semaphore == SEMAPHORE_NULL)
752 return KERN_INVALID_ARGUMENT;
753
754 return(semaphore_wait_internal(semaphore,
755 SEMAPHORE_NULL,
756 deadline, SEMAPHORE_OPTION_NONE,
757 (void (*)(kern_return_t))0));
758 }
759
760 /*
761 * Trap: semaphore_wait_trap
762 *
763 * Trap version of semaphore wait. Called on behalf of user-level
764 * clients.
765 */
766
767 kern_return_t
768 semaphore_wait_trap(
769 struct semaphore_wait_trap_args *args)
770 {
771 return(semaphore_wait_trap_internal(args->wait_name, thread_syscall_return));
772 }
773
774
775
776 kern_return_t
777 semaphore_wait_trap_internal(
778 mach_port_name_t name,
779 void (*caller_cont)(kern_return_t))
780 {
781 semaphore_t semaphore;
782 kern_return_t kr;
783
784 kr = port_name_to_semaphore(name, &semaphore);
785 if (kr == KERN_SUCCESS) {
786 kr = semaphore_wait_internal(semaphore,
787 SEMAPHORE_NULL,
788 0ULL, SEMAPHORE_OPTION_NONE,
789 caller_cont);
790 semaphore_dereference(semaphore);
791 }
792 return kr;
793 }
794
795 /*
796 * Routine: semaphore_timedwait
797 *
798 * Traditional (non-continuation) interface presented to
799 * in-kernel clients to wait on a semaphore with a timeout.
800 *
801 * A timeout of {0,0} is considered non-blocking.
802 */
803 kern_return_t
804 semaphore_timedwait(
805 semaphore_t semaphore,
806 mach_timespec_t wait_time)
807 {
808 int option = SEMAPHORE_OPTION_NONE;
809 uint64_t deadline = 0;
810
811 if (semaphore == SEMAPHORE_NULL)
812 return KERN_INVALID_ARGUMENT;
813
814 if(BAD_MACH_TIMESPEC(&wait_time))
815 return KERN_INVALID_VALUE;
816
817 if (wait_time.tv_sec == 0 && wait_time.tv_nsec == 0)
818 option = SEMAPHORE_TIMEOUT_NOBLOCK;
819 else
820 deadline = semaphore_deadline(wait_time.tv_sec, wait_time.tv_nsec);
821
822 return (semaphore_wait_internal(semaphore,
823 SEMAPHORE_NULL,
824 deadline, option,
825 (void(*)(kern_return_t))0));
826
827 }
828
829 /*
830 * Trap: semaphore_timedwait_trap
831 *
832 * Trap version of a semaphore_timedwait. The timeout parameter
833 * is passed in two distinct parts and re-assembled on this side
834 * of the trap interface (to accomodate calling conventions that
835 * pass structures as pointers instead of inline in registers without
836 * having to add a copyin).
837 *
838 * A timeout of {0,0} is considered non-blocking.
839 */
840 kern_return_t
841 semaphore_timedwait_trap(
842 struct semaphore_timedwait_trap_args *args)
843 {
844
845 return(semaphore_timedwait_trap_internal(args->wait_name, args->sec, args->nsec, thread_syscall_return));
846 }
847
848
849 kern_return_t
850 semaphore_timedwait_trap_internal(
851 mach_port_name_t name,
852 unsigned int sec,
853 clock_res_t nsec,
854 void (*caller_cont)(kern_return_t))
855 {
856 semaphore_t semaphore;
857 mach_timespec_t wait_time;
858 kern_return_t kr;
859
860 wait_time.tv_sec = sec;
861 wait_time.tv_nsec = nsec;
862 if(BAD_MACH_TIMESPEC(&wait_time))
863 return KERN_INVALID_VALUE;
864
865 kr = port_name_to_semaphore(name, &semaphore);
866 if (kr == KERN_SUCCESS) {
867 int option = SEMAPHORE_OPTION_NONE;
868 uint64_t deadline = 0;
869
870 if (sec == 0 && nsec == 0)
871 option = SEMAPHORE_TIMEOUT_NOBLOCK;
872 else
873 deadline = semaphore_deadline(sec, nsec);
874
875 kr = semaphore_wait_internal(semaphore,
876 SEMAPHORE_NULL,
877 deadline, option,
878 caller_cont);
879 semaphore_dereference(semaphore);
880 }
881 return kr;
882 }
883
884 /*
885 * Routine: semaphore_wait_signal
886 *
887 * Atomically register a wait on a semaphore and THEN signal
888 * another. This is the in-kernel entry point that does not
889 * block at a continuation and does not free a signal_semaphore
890 * reference.
891 */
892 kern_return_t
893 semaphore_wait_signal(
894 semaphore_t wait_semaphore,
895 semaphore_t signal_semaphore)
896 {
897 if (wait_semaphore == SEMAPHORE_NULL)
898 return KERN_INVALID_ARGUMENT;
899
900 return(semaphore_wait_internal(wait_semaphore,
901 signal_semaphore,
902 0ULL, SEMAPHORE_OPTION_NONE,
903 (void(*)(kern_return_t))0));
904 }
905
906 /*
907 * Trap: semaphore_wait_signal_trap
908 *
909 * Atomically register a wait on a semaphore and THEN signal
910 * another. This is the trap version from user space.
911 */
912 kern_return_t
913 semaphore_wait_signal_trap(
914 struct semaphore_wait_signal_trap_args *args)
915 {
916 return(semaphore_wait_signal_trap_internal(args->wait_name, args->signal_name, thread_syscall_return));
917 }
918
919 kern_return_t
920 semaphore_wait_signal_trap_internal(
921 mach_port_name_t wait_name,
922 mach_port_name_t signal_name,
923 void (*caller_cont)(kern_return_t))
924 {
925 semaphore_t wait_semaphore;
926 semaphore_t signal_semaphore;
927 kern_return_t kr;
928
929 kr = port_name_to_semaphore(signal_name, &signal_semaphore);
930 if (kr == KERN_SUCCESS) {
931 kr = port_name_to_semaphore(wait_name, &wait_semaphore);
932 if (kr == KERN_SUCCESS) {
933 kr = semaphore_wait_internal(wait_semaphore,
934 signal_semaphore,
935 0ULL, SEMAPHORE_OPTION_NONE,
936 caller_cont);
937 semaphore_dereference(wait_semaphore);
938 }
939 semaphore_dereference(signal_semaphore);
940 }
941 return kr;
942 }
943
944
945 /*
946 * Routine: semaphore_timedwait_signal
947 *
948 * Atomically register a wait on a semaphore and THEN signal
949 * another. This is the in-kernel entry point that does not
950 * block at a continuation.
951 *
952 * A timeout of {0,0} is considered non-blocking.
953 */
954 kern_return_t
955 semaphore_timedwait_signal(
956 semaphore_t wait_semaphore,
957 semaphore_t signal_semaphore,
958 mach_timespec_t wait_time)
959 {
960 int option = SEMAPHORE_OPTION_NONE;
961 uint64_t deadline = 0;
962
963 if (wait_semaphore == SEMAPHORE_NULL)
964 return KERN_INVALID_ARGUMENT;
965
966 if(BAD_MACH_TIMESPEC(&wait_time))
967 return KERN_INVALID_VALUE;
968
969 if (wait_time.tv_sec == 0 && wait_time.tv_nsec == 0)
970 option = SEMAPHORE_TIMEOUT_NOBLOCK;
971 else
972 deadline = semaphore_deadline(wait_time.tv_sec, wait_time.tv_nsec);
973
974 return(semaphore_wait_internal(wait_semaphore,
975 signal_semaphore,
976 deadline, option,
977 (void(*)(kern_return_t))0));
978 }
979
980 /*
981 * Trap: semaphore_timedwait_signal_trap
982 *
983 * Atomically register a timed wait on a semaphore and THEN signal
984 * another. This is the trap version from user space.
985 */
986 kern_return_t
987 semaphore_timedwait_signal_trap(
988 struct semaphore_timedwait_signal_trap_args *args)
989 {
990 return(semaphore_timedwait_signal_trap_internal(args->wait_name, args->signal_name, args->sec, args->nsec, thread_syscall_return));
991 }
992
993 kern_return_t
994 semaphore_timedwait_signal_trap_internal(
995 mach_port_name_t wait_name,
996 mach_port_name_t signal_name,
997 unsigned int sec,
998 clock_res_t nsec,
999 void (*caller_cont)(kern_return_t))
1000 {
1001 semaphore_t wait_semaphore;
1002 semaphore_t signal_semaphore;
1003 mach_timespec_t wait_time;
1004 kern_return_t kr;
1005
1006 wait_time.tv_sec = sec;
1007 wait_time.tv_nsec = nsec;
1008 if(BAD_MACH_TIMESPEC(&wait_time))
1009 return KERN_INVALID_VALUE;
1010
1011 kr = port_name_to_semaphore(signal_name, &signal_semaphore);
1012 if (kr == KERN_SUCCESS) {
1013 kr = port_name_to_semaphore(wait_name, &wait_semaphore);
1014 if (kr == KERN_SUCCESS) {
1015 int option = SEMAPHORE_OPTION_NONE;
1016 uint64_t deadline = 0;
1017
1018 if (sec == 0 && nsec == 0)
1019 option = SEMAPHORE_TIMEOUT_NOBLOCK;
1020 else
1021 deadline = semaphore_deadline(sec, nsec);
1022
1023 kr = semaphore_wait_internal(wait_semaphore,
1024 signal_semaphore,
1025 deadline, option,
1026 caller_cont);
1027 semaphore_dereference(wait_semaphore);
1028 }
1029 semaphore_dereference(signal_semaphore);
1030 }
1031 return kr;
1032 }
1033
1034
1035 /*
1036 * Routine: semaphore_reference
1037 *
1038 * Take out a reference on a semaphore. This keeps the data structure
1039 * in existence (but the semaphore may be deactivated).
1040 */
1041 void
1042 semaphore_reference(
1043 semaphore_t semaphore)
1044 {
1045 (void)hw_atomic_add(&semaphore->ref_count, 1);
1046 }
1047
1048 /*
1049 * Routine: semaphore_dereference
1050 *
1051 * Release a reference on a semaphore. If this is the last reference,
1052 * the semaphore data structure is deallocated.
1053 */
1054 void
1055 semaphore_dereference(
1056 semaphore_t semaphore)
1057 {
1058 int ref_count;
1059
1060 if (semaphore != NULL) {
1061 ref_count = hw_atomic_sub(&semaphore->ref_count, 1);
1062
1063 if (ref_count == 0) {
1064 assert(wait_queue_empty(&semaphore->wait_queue));
1065 ipc_port_dealloc_kernel(semaphore->port);
1066 zfree(semaphore_zone, semaphore);
1067 }
1068 }
1069 }