]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/sync_sema.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / osfmk / kern / sync_sema.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 *
33 */
34 /*
35 * File: kern/sync_sema.c
36 * Author: Joseph CaraDonna
37 *
38 * Contains RT distributed semaphore synchronization services.
39 */
40
41 #include <mach/mach_types.h>
42 #include <mach/mach_traps.h>
43 #include <mach/kern_return.h>
44 #include <mach/semaphore.h>
45 #include <mach/sync_policy.h>
46 #include <mach/task.h>
47
48 #include <kern/misc_protos.h>
49 #include <kern/sync_sema.h>
50 #include <kern/spl.h>
51 #include <kern/ipc_kobject.h>
52 #include <kern/ipc_sync.h>
53 #include <kern/ipc_tt.h>
54 #include <kern/thread.h>
55 #include <kern/clock.h>
56 #include <ipc/ipc_port.h>
57 #include <ipc/ipc_space.h>
58 #include <kern/host.h>
59 #include <kern/wait_queue.h>
60 #include <kern/zalloc.h>
61 #include <kern/mach_param.h>
62
63 static unsigned int semaphore_event;
64 #define SEMAPHORE_EVENT ((event64_t)&semaphore_event)
65
66 zone_t semaphore_zone;
67 unsigned int semaphore_max = SEMAPHORE_MAX;
68
69 /* Forward declarations */
70
71
72 kern_return_t
73 semaphore_wait_trap_internal(
74 mach_port_name_t name,
75 void (*caller_cont)(kern_return_t));
76
77 kern_return_t
78 semaphore_wait_signal_trap_internal(
79 mach_port_name_t wait_name,
80 mach_port_name_t signal_name,
81 void (*caller_cont)(kern_return_t));
82
83 kern_return_t
84 semaphore_timedwait_trap_internal(
85 mach_port_name_t name,
86 unsigned int sec,
87 clock_res_t nsec,
88 void (*caller_cont)(kern_return_t));
89
90 kern_return_t
91 semaphore_timedwait_signal_trap_internal(
92 mach_port_name_t wait_name,
93 mach_port_name_t signal_name,
94 unsigned int sec,
95 clock_res_t nsec,
96 void (*caller_cont)(kern_return_t));
97
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 kern_return_t
113 semaphore_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
119 /*
120 * ROUTINE: semaphore_init [private]
121 *
122 * Initialize the semaphore mechanisms.
123 * Right now, we only need to initialize the semaphore zone.
124 */
125 void
126 semaphore_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 */
140 kern_return_t
141 semaphore_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 */
209 kern_return_t
210 semaphore_destroy(
211 task_t task,
212 semaphore_t semaphore)
213 {
214 int old_count;
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) {
250 wait_queue_wakeup64_all_locked(&semaphore->wait_queue,
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 */
277 kern_return_t
278 semaphore_signal_internal(
279 semaphore_t semaphore,
280 thread_t thread,
281 int options)
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
295 if (thread != THREAD_NULL) {
296 if (semaphore->count < 0) {
297 kr = wait_queue_wakeup64_thread_locked(
298 &semaphore->wait_queue,
299 SEMAPHORE_EVENT,
300 thread,
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 */
316 kr = wait_queue_wakeup64_all_locked(
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) {
332 if (wait_queue_wakeup64_one_locked(
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 *
356 * If the specified thread is blocked on the semaphore, it is
357 * woken up. If a NULL thread was supplied, then any one
358 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
359 * and the semaphore is unchanged.
360 */
361 kern_return_t
362 semaphore_signal_thread(
363 semaphore_t semaphore,
364 thread_t thread)
365 {
366 kern_return_t ret;
367
368 if (semaphore == SEMAPHORE_NULL)
369 return KERN_INVALID_ARGUMENT;
370
371 ret = semaphore_signal_internal(semaphore,
372 thread,
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 */
382 kern_return_t
383 semaphore_signal_thread_trap(
384 struct semaphore_signal_thread_trap_args *args)
385 {
386 mach_port_name_t sema_name = args->signal_name;
387 mach_port_name_t thread_name = args->thread_name;
388 semaphore_t semaphore;
389 thread_t thread;
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) {
398 thread = port_name_to_thread(thread_name);
399 if (thread == THREAD_NULL)
400 return KERN_INVALID_ARGUMENT;
401 } else
402 thread = THREAD_NULL;
403
404 kr = port_name_to_semaphore(sema_name, &semaphore);
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);
413 }
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 */
430 kern_return_t
431 semaphore_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,
440 THREAD_NULL,
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 */
452 kern_return_t
453 semaphore_signal_trap(
454 struct semaphore_signal_trap_args *args)
455 {
456 mach_port_name_t sema_name = args->signal_name;
457 semaphore_t semaphore;
458 kern_return_t kr;
459
460 kr = port_name_to_semaphore(sema_name, &semaphore);
461 if (kr == KERN_SUCCESS) {
462 kr = semaphore_signal_internal(semaphore,
463 THREAD_NULL,
464 SEMAPHORE_SIGNAL_PREPOST);
465 semaphore_dereference(semaphore);
466 if (kr == KERN_NOT_WAITING)
467 kr = KERN_SUCCESS;
468 }
469 return kr;
470 }
471
472 /*
473 * Routine: semaphore_signal_all
474 *
475 * Awakens ALL threads currently blocked on the semaphore.
476 * The semaphore count returns to zero.
477 */
478 kern_return_t
479 semaphore_signal_all(
480 semaphore_t semaphore)
481 {
482 kern_return_t kr;
483
484 if (semaphore == SEMAPHORE_NULL)
485 return KERN_INVALID_ARGUMENT;
486
487 kr = semaphore_signal_internal(semaphore,
488 THREAD_NULL,
489 SEMAPHORE_SIGNAL_ALL);
490 if (kr == KERN_NOT_WAITING)
491 return KERN_SUCCESS;
492 return kr;
493 }
494
495 /*
496 * Routine: semaphore_signal_all_trap
497 *
498 * Trap interface to the semaphore_signal_all function.
499 */
500 kern_return_t
501 semaphore_signal_all_trap(
502 struct semaphore_signal_all_trap_args *args)
503 {
504 mach_port_name_t sema_name = args->signal_name;
505 semaphore_t semaphore;
506 kern_return_t kr;
507
508 kr = port_name_to_semaphore(sema_name, &semaphore);
509 if (kr == KERN_SUCCESS) {
510 kr = semaphore_signal_internal(semaphore,
511 THREAD_NULL,
512 SEMAPHORE_SIGNAL_ALL);
513 semaphore_dereference(semaphore);
514 if (kr == KERN_NOT_WAITING)
515 kr = KERN_SUCCESS;
516 }
517 return kr;
518 }
519
520 /*
521 * Routine: semaphore_convert_wait_result
522 *
523 * Generate the return code after a semaphore wait/block. It
524 * takes the wait result as an input and coverts that to an
525 * appropriate result.
526 */
527 kern_return_t
528 semaphore_convert_wait_result(int wait_result)
529 {
530 switch (wait_result) {
531 case THREAD_AWAKENED:
532 return KERN_SUCCESS;
533
534 case THREAD_TIMED_OUT:
535 return KERN_OPERATION_TIMED_OUT;
536
537 case THREAD_INTERRUPTED:
538 return KERN_ABORTED;
539
540 case THREAD_RESTART:
541 return KERN_TERMINATED;
542
543 default:
544 panic("semaphore_block\n");
545 return KERN_FAILURE;
546 }
547 }
548
549 /*
550 * Routine: semaphore_wait_continue
551 *
552 * Common continuation routine after waiting on a semphore.
553 * It returns directly to user space.
554 */
555 void
556 semaphore_wait_continue(void)
557 {
558 thread_t self = current_thread();
559 int wait_result = self->wait_result;
560 void (*caller_cont)(kern_return_t) = self->sth_continuation;
561
562 assert(self->sth_waitsemaphore != SEMAPHORE_NULL);
563 semaphore_dereference(self->sth_waitsemaphore);
564 if (self->sth_signalsemaphore != SEMAPHORE_NULL)
565 semaphore_dereference(self->sth_signalsemaphore);
566
567 assert(caller_cont != (void (*)(kern_return_t))0);
568 (*caller_cont)(semaphore_convert_wait_result(wait_result));
569 }
570
571 /*
572 * Routine: semaphore_wait_internal
573 *
574 * Decrements the semaphore count by one. If the count is
575 * negative after the decrement, the calling thread blocks
576 * (possibly at a continuation and/or with a timeout).
577 *
578 * Assumptions:
579 * The reference
580 * A reference is held on the signal semaphore.
581 */
582 kern_return_t
583 semaphore_wait_internal(
584 semaphore_t wait_semaphore,
585 semaphore_t signal_semaphore,
586 mach_timespec_t *wait_timep,
587 void (*caller_cont)(kern_return_t))
588 {
589 boolean_t nonblocking;
590 int wait_result;
591 spl_t spl_level;
592 kern_return_t kr = KERN_ALREADY_WAITING;
593
594 spl_level = splsched();
595 semaphore_lock(wait_semaphore);
596
597 /*
598 * Decide if we really have to wait.
599 */
600 nonblocking = (wait_timep != (mach_timespec_t *)0) ?
601 (wait_timep->tv_sec == 0 && wait_timep->tv_nsec == 0) :
602 FALSE;
603
604 if (!wait_semaphore->active) {
605 kr = KERN_TERMINATED;
606 } else if (wait_semaphore->count > 0) {
607 wait_semaphore->count--;
608 kr = KERN_SUCCESS;
609 } else if (nonblocking) {
610 kr = KERN_OPERATION_TIMED_OUT;
611 } else {
612 uint64_t abstime;
613 thread_t self = current_thread();
614
615 wait_semaphore->count = -1; /* we don't keep an actual count */
616 thread_lock(self);
617
618 /*
619 * If it is a timed wait, calculate the wake up deadline.
620 */
621 if (wait_timep != (mach_timespec_t *)0) {
622 nanoseconds_to_absolutetime((uint64_t)wait_timep->tv_sec *
623 NSEC_PER_SEC + wait_timep->tv_nsec, &abstime);
624 clock_absolutetime_interval_to_deadline(abstime, &abstime);
625 }
626 else
627 abstime = 0;
628
629 (void)wait_queue_assert_wait64_locked(
630 &wait_semaphore->wait_queue,
631 SEMAPHORE_EVENT,
632 THREAD_ABORTSAFE, abstime,
633 self);
634 thread_unlock(self);
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,
652 THREAD_NULL,
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;
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;
700 wait_result = thread_block((thread_continue_t)semaphore_wait_continue);
701 }
702 else {
703 wait_result = thread_block(THREAD_CONTINUE_NULL);
704 }
705
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 */
716 kern_return_t
717 semaphore_wait(
718 semaphore_t semaphore)
719 {
720
721 if (semaphore == SEMAPHORE_NULL)
722 return KERN_INVALID_ARGUMENT;
723
724 return(semaphore_wait_internal(semaphore,
725 SEMAPHORE_NULL,
726 (mach_timespec_t *)0,
727 (void (*)(kern_return_t))0));
728 }
729
730 /*
731 * Trap: semaphore_wait_trap
732 *
733 * Trap version of semaphore wait. Called on behalf of user-level
734 * clients.
735 */
736
737 kern_return_t
738 semaphore_wait_trap(
739 struct semaphore_wait_trap_args *args)
740 {
741 return(semaphore_wait_trap_internal(args->wait_name, thread_syscall_return));
742 }
743
744
745
746 kern_return_t
747 semaphore_wait_trap_internal(
748 mach_port_name_t name,
749 void (*caller_cont)(kern_return_t))
750 {
751 semaphore_t semaphore;
752 kern_return_t kr;
753
754 kr = port_name_to_semaphore(name, &semaphore);
755 if (kr == KERN_SUCCESS) {
756 kr = semaphore_wait_internal(semaphore,
757 SEMAPHORE_NULL,
758 (mach_timespec_t *)0,
759 caller_cont);
760 semaphore_dereference(semaphore);
761 }
762 return kr;
763 }
764
765 /*
766 * Routine: semaphore_timedwait
767 *
768 * Traditional (non-continuation) interface presented to
769 * in-kernel clients to wait on a semaphore with a timeout.
770 *
771 * A timeout of {0,0} is considered non-blocking.
772 */
773 kern_return_t
774 semaphore_timedwait(
775 semaphore_t semaphore,
776 mach_timespec_t wait_time)
777 {
778 if (semaphore == SEMAPHORE_NULL)
779 return KERN_INVALID_ARGUMENT;
780
781 if(BAD_MACH_TIMESPEC(&wait_time))
782 return KERN_INVALID_VALUE;
783
784 return (semaphore_wait_internal(semaphore,
785 SEMAPHORE_NULL,
786 &wait_time,
787 (void(*)(kern_return_t))0));
788
789 }
790
791 /*
792 * Trap: semaphore_timedwait_trap
793 *
794 * Trap version of a semaphore_timedwait. The timeout parameter
795 * is passed in two distinct parts and re-assembled on this side
796 * of the trap interface (to accomodate calling conventions that
797 * pass structures as pointers instead of inline in registers without
798 * having to add a copyin).
799 *
800 * A timeout of {0,0} is considered non-blocking.
801 */
802 kern_return_t
803 semaphore_timedwait_trap(
804 struct semaphore_timedwait_trap_args *args)
805 {
806
807 return(semaphore_timedwait_trap_internal(args->wait_name, args->sec, args->nsec, thread_syscall_return));
808 }
809
810
811 kern_return_t
812 semaphore_timedwait_trap_internal(
813 mach_port_name_t name,
814 unsigned int sec,
815 clock_res_t nsec,
816 void (*caller_cont)(kern_return_t))
817 {
818
819 semaphore_t semaphore;
820 mach_timespec_t wait_time;
821 kern_return_t kr;
822
823 wait_time.tv_sec = sec;
824 wait_time.tv_nsec = nsec;
825 if(BAD_MACH_TIMESPEC(&wait_time))
826 return KERN_INVALID_VALUE;
827
828 kr = port_name_to_semaphore(name, &semaphore);
829 if (kr == KERN_SUCCESS) {
830 kr = semaphore_wait_internal(semaphore,
831 SEMAPHORE_NULL,
832 &wait_time,
833 caller_cont);
834 semaphore_dereference(semaphore);
835 }
836 return kr;
837 }
838
839 /*
840 * Routine: semaphore_wait_signal
841 *
842 * Atomically register a wait on a semaphore and THEN signal
843 * another. This is the in-kernel entry point that does not
844 * block at a continuation and does not free a signal_semaphore
845 * reference.
846 */
847 kern_return_t
848 semaphore_wait_signal(
849 semaphore_t wait_semaphore,
850 semaphore_t signal_semaphore)
851 {
852 if (wait_semaphore == SEMAPHORE_NULL)
853 return KERN_INVALID_ARGUMENT;
854
855 return(semaphore_wait_internal(wait_semaphore,
856 signal_semaphore,
857 (mach_timespec_t *)0,
858 (void(*)(kern_return_t))0));
859 }
860
861 /*
862 * Trap: semaphore_wait_signal_trap
863 *
864 * Atomically register a wait on a semaphore and THEN signal
865 * another. This is the trap version from user space.
866 */
867 kern_return_t
868 semaphore_wait_signal_trap(
869 struct semaphore_wait_signal_trap_args *args)
870 {
871 return(semaphore_wait_signal_trap_internal(args->wait_name, args->signal_name, thread_syscall_return));
872 }
873
874 kern_return_t
875 semaphore_wait_signal_trap_internal(
876 mach_port_name_t wait_name,
877 mach_port_name_t signal_name,
878 void (*caller_cont)(kern_return_t))
879 {
880 semaphore_t wait_semaphore;
881 semaphore_t signal_semaphore;
882 kern_return_t kr;
883
884 kr = port_name_to_semaphore(signal_name, &signal_semaphore);
885 if (kr == KERN_SUCCESS) {
886 kr = port_name_to_semaphore(wait_name, &wait_semaphore);
887 if (kr == KERN_SUCCESS) {
888 kr = semaphore_wait_internal(wait_semaphore,
889 signal_semaphore,
890 (mach_timespec_t *)0,
891 caller_cont);
892 semaphore_dereference(wait_semaphore);
893 }
894 semaphore_dereference(signal_semaphore);
895 }
896 return kr;
897 }
898
899
900 /*
901 * Routine: semaphore_timedwait_signal
902 *
903 * Atomically register a wait on a semaphore and THEN signal
904 * another. This is the in-kernel entry point that does not
905 * block at a continuation.
906 *
907 * A timeout of {0,0} is considered non-blocking.
908 */
909 kern_return_t
910 semaphore_timedwait_signal(
911 semaphore_t wait_semaphore,
912 semaphore_t signal_semaphore,
913 mach_timespec_t wait_time)
914 {
915 if (wait_semaphore == SEMAPHORE_NULL)
916 return KERN_INVALID_ARGUMENT;
917
918 if(BAD_MACH_TIMESPEC(&wait_time))
919 return KERN_INVALID_VALUE;
920
921 return(semaphore_wait_internal(wait_semaphore,
922 signal_semaphore,
923 &wait_time,
924 (void(*)(kern_return_t))0));
925 }
926
927 /*
928 * Trap: semaphore_timedwait_signal_trap
929 *
930 * Atomically register a timed wait on a semaphore and THEN signal
931 * another. This is the trap version from user space.
932 */
933 kern_return_t
934 semaphore_timedwait_signal_trap(
935 struct semaphore_timedwait_signal_trap_args *args)
936 {
937 return(semaphore_timedwait_signal_trap_internal(args->wait_name, args->signal_name, args->sec, args->nsec, thread_syscall_return));
938 }
939
940 kern_return_t
941 semaphore_timedwait_signal_trap_internal(
942 mach_port_name_t wait_name,
943 mach_port_name_t signal_name,
944 unsigned int sec,
945 clock_res_t nsec,
946 void (*caller_cont)(kern_return_t))
947 {
948 semaphore_t wait_semaphore;
949 semaphore_t signal_semaphore;
950 mach_timespec_t wait_time;
951 kern_return_t kr;
952
953 wait_time.tv_sec = sec;
954 wait_time.tv_nsec = nsec;
955 if(BAD_MACH_TIMESPEC(&wait_time))
956 return KERN_INVALID_VALUE;
957
958 kr = port_name_to_semaphore(signal_name, &signal_semaphore);
959 if (kr == KERN_SUCCESS) {
960 kr = port_name_to_semaphore(wait_name, &wait_semaphore);
961 if (kr == KERN_SUCCESS) {
962 kr = semaphore_wait_internal(wait_semaphore,
963 signal_semaphore,
964 &wait_time,
965 caller_cont);
966 semaphore_dereference(wait_semaphore);
967 }
968 semaphore_dereference(signal_semaphore);
969 }
970 return kr;
971 }
972
973
974 /*
975 * Routine: semaphore_reference
976 *
977 * Take out a reference on a semaphore. This keeps the data structure
978 * in existence (but the semaphore may be deactivated).
979 */
980 void
981 semaphore_reference(
982 semaphore_t semaphore)
983 {
984 spl_t spl_level;
985
986 spl_level = splsched();
987 semaphore_lock(semaphore);
988
989 semaphore->ref_count++;
990
991 semaphore_unlock(semaphore);
992 splx(spl_level);
993 }
994
995 /*
996 * Routine: semaphore_dereference
997 *
998 * Release a reference on a semaphore. If this is the last reference,
999 * the semaphore data structure is deallocated.
1000 */
1001 void
1002 semaphore_dereference(
1003 semaphore_t semaphore)
1004 {
1005 int ref_count;
1006 spl_t spl_level;
1007
1008 if (semaphore != NULL) {
1009 spl_level = splsched();
1010 semaphore_lock(semaphore);
1011
1012 ref_count = --(semaphore->ref_count);
1013
1014 semaphore_unlock(semaphore);
1015 splx(spl_level);
1016
1017 if (ref_count == 0) {
1018 assert(wait_queue_empty(&semaphore->wait_queue));
1019 zfree(semaphore_zone, semaphore);
1020 }
1021 }
1022 }