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