1 /* 2 * Copyright (c) 2000-2021 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.h 34 * Author: Joseph CaraDonna 35 * 36 * Contains RT distributed semaphore synchronization service definitions. 37 */ 38 39 #ifndef _KERN_SYNC_SEMA_H_ 40 #define _KERN_SYNC_SEMA_H_ 41 42 #include <kern/kern_types.h> 43 #include <mach/sync_policy.h> 44 #include <mach/clock_types.h> 45 46 #ifdef MACH_KERNEL_PRIVATE 47 48 #include <kern/queue.h> 49 #include <kern/waitq.h> 50 #include <os/refcnt.h> 51 52 typedef struct semaphore { 53 queue_chain_t task_link; /* chain of semaphores owned by a task */ 54 struct waitq waitq; /* queue of blocked threads & lock */ 55 task_t owner; /* task that owns semaphore */ 56 ipc_port_t port; /* semaphore port */ 57 os_ref_atomic_t ref_count; /* reference count */ 58 int count; /* current count value */ 59 } Semaphore; 60 61 #define semaphore_lock(semaphore) waitq_lock(&(semaphore)->waitq) 62 #define semaphore_unlock(semaphore) waitq_unlock(&(semaphore)->waitq) 63 64 extern void semaphore_reference( 65 semaphore_t semaphore); 66 67 extern void semaphore_dereference( 68 semaphore_t semaphore); 69 70 #pragma GCC visibility push(hidden) 71 72 extern void semaphore_destroy_all( 73 task_t task); 74 75 extern semaphore_t convert_port_to_semaphore( 76 ipc_port_t port); 77 78 extern ipc_port_t convert_semaphore_to_port( 79 semaphore_t semaphore); 80 81 extern kern_return_t port_name_to_semaphore( 82 mach_port_name_t name, 83 semaphore_t *semaphore); 84 85 #pragma GCC visibility pop 86 #endif /* MACH_KERNEL_PRIVATE */ 87 #if XNU_KERNEL_PRIVATE 88 #pragma GCC visibility push(hidden) 89 90 #define SEMAPHORE_CONT_NULL ((semaphore_cont_t)NULL) 91 typedef void (*semaphore_cont_t)(kern_return_t); 92 93 extern kern_return_t semaphore_signal_internal_trap( 94 mach_port_name_t sema_name); 95 96 extern kern_return_t semaphore_timedwait_signal_trap_internal( 97 mach_port_name_t wait_name, 98 mach_port_name_t signal_name, 99 unsigned int sec, 100 clock_res_t nsec, 101 semaphore_cont_t); 102 103 extern kern_return_t semaphore_timedwait_trap_internal( 104 mach_port_name_t name, 105 unsigned int sec, 106 clock_res_t nsec, 107 semaphore_cont_t); 108 109 extern kern_return_t semaphore_wait_signal_trap_internal( 110 mach_port_name_t wait_name, 111 mach_port_name_t signal_name, 112 semaphore_cont_t); 113 114 extern kern_return_t semaphore_wait_trap_internal( 115 mach_port_name_t name, 116 semaphore_cont_t); 117 118 #pragma GCC visibility pop 119 #endif /* XNU_KERNEL_PRIVATE */ 120 #endif /* _KERN_SYNC_SEMA_H_ */ 121