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