kern_return_t semaphore_timedwait_signal_trap_internal( mach_port_name_t wait_name, mach_port_name_t signal_name, unsigned int sec, clock_res_t nsec, void (*caller_cont)(kern_return_t)) { semaphore_t wait_semaphore; semaphore_t signal_semaphore; mach_timespec_t wait_time; kern_return_t kr; wait_time.tv_sec = sec; wait_time.tv_nsec = nsec; if(BAD_MACH_TIMESPEC(&wait_time)) return KERN_INVALID_VALUE; kr = port_name_to_semaphore(signal_name, &signal_semaphore); if (kr == KERN_SUCCESS) { kr = port_name_to_semaphore(wait_name, &wait_semaphore); if (kr == KERN_SUCCESS) { kr = semaphore_wait_internal(wait_semaphore, signal_semaphore, &wait_time, caller_cont); semaphore_dereference(wait_semaphore); } semaphore_dereference(signal_semaphore); } return kr; }
/* * Routine: semaphore_destroy * * Destroys a semaphore and consume the caller's reference on the * semaphore. */ kern_return_t semaphore_destroy( task_t task, semaphore_t semaphore) { spl_t spl_level; if (semaphore == SEMAPHORE_NULL) return KERN_INVALID_ARGUMENT; if (task == TASK_NULL) { semaphore_dereference(semaphore); return KERN_INVALID_ARGUMENT; } task_lock(task); spl_level = splsched(); semaphore_lock(semaphore); if (semaphore->owner != task) { semaphore_unlock(semaphore); splx(spl_level); task_unlock(task); return KERN_INVALID_ARGUMENT; } semaphore_destroy_internal(task, semaphore); /* semaphore unlocked */ splx(spl_level); task_unlock(task); semaphore_dereference(semaphore); return KERN_SUCCESS; }
/* * Routine: semaphore_wait_continue * * Common continuation routine after waiting on a semphore. * It returns directly to user space. */ void semaphore_wait_continue(void) { thread_t self = current_thread(); int wait_result = self->wait_result; void (*caller_cont)(kern_return_t) = self->sth_continuation; assert(self->sth_waitsemaphore != SEMAPHORE_NULL); semaphore_dereference(self->sth_waitsemaphore); if (self->sth_signalsemaphore != SEMAPHORE_NULL) semaphore_dereference(self->sth_signalsemaphore); assert(caller_cont != (void (*)(kern_return_t))0); (*caller_cont)(semaphore_convert_wait_result(wait_result)); }
/* * Routine: semaphore_signal_thread_trap * * Trap interface to the semaphore_signal_thread function. */ kern_return_t semaphore_signal_thread_trap( struct semaphore_signal_thread_trap_args *args) { mach_port_name_t sema_name = args->signal_name; mach_port_name_t thread_name = args->thread_name; semaphore_t semaphore; thread_t thread; kern_return_t kr; /* * MACH_PORT_NULL is not an error. It means that we want to * select any one thread that is already waiting, but not to * pre-post the semaphore. */ if (thread_name != MACH_PORT_NULL) { thread = port_name_to_thread(thread_name); if (thread == THREAD_NULL) return KERN_INVALID_ARGUMENT; } else thread = THREAD_NULL; kr = port_name_to_semaphore(sema_name, &semaphore); if (kr == KERN_SUCCESS) { kr = semaphore_signal_internal(semaphore, thread, SEMAPHORE_OPTION_NONE); semaphore_dereference(semaphore); } if (thread != THREAD_NULL) { thread_deallocate(thread); } return kr; }
/* * Routine: semaphore_destroy * * Destroys a semaphore. This call will only succeed if the * specified task is the SAME task name specified at the semaphore's * creation. * * All threads currently blocked on the semaphore are awoken. These * threads will return with the KERN_TERMINATED error. */ kern_return_t semaphore_destroy( task_t task, semaphore_t semaphore) { int old_count; spl_t spl_level; if (task == TASK_NULL || semaphore == SEMAPHORE_NULL) return KERN_INVALID_ARGUMENT; /* * Disown semaphore */ task_lock(task); if (semaphore->owner != task) { task_unlock(task); return KERN_INVALID_ARGUMENT; } remqueue(&task->semaphore_list, (queue_entry_t) semaphore); semaphore->owner = TASK_NULL; task->semaphores_owned--; task_unlock(task); spl_level = splsched(); semaphore_lock(semaphore); /* * Deactivate semaphore */ assert(semaphore->active); semaphore->active = FALSE; /* * Wakeup blocked threads */ old_count = semaphore->count; semaphore->count = 0; if (old_count < 0) { wait_queue_wakeup64_all_locked(&semaphore->wait_queue, SEMAPHORE_EVENT, THREAD_RESTART, TRUE); /* unlock? */ } else { semaphore_unlock(semaphore); } splx(spl_level); /* * Deallocate * * Drop the semaphore reference, which in turn deallocates the * semaphore structure if the reference count goes to zero. */ ipc_port_dealloc_kernel(semaphore->port); semaphore_dereference(semaphore); return KERN_SUCCESS; }
kern_return_t semaphore_timedwait_trap_internal( mach_port_name_t name, unsigned int sec, clock_res_t nsec, void (*caller_cont)(kern_return_t)) { semaphore_t semaphore; mach_timespec_t wait_time; kern_return_t kr; wait_time.tv_sec = sec; wait_time.tv_nsec = nsec; if(BAD_MACH_TIMESPEC(&wait_time)) return KERN_INVALID_VALUE; kr = port_name_to_semaphore(name, &semaphore); if (kr == KERN_SUCCESS) { int option = SEMAPHORE_OPTION_NONE; uint64_t deadline = 0; if (sec == 0 && nsec == 0) option = SEMAPHORE_TIMEOUT_NOBLOCK; else deadline = semaphore_deadline(sec, nsec); kr = semaphore_wait_internal(semaphore, SEMAPHORE_NULL, deadline, option, caller_cont); semaphore_dereference(semaphore); } return kr; }
/* * Routine: semaphore_create * * Creates a semaphore. * The port representing the semaphore is returned as a parameter. */ kern_return_t semaphore_create( task_t task, semaphore_t *new_semaphore, int policy, int value) { semaphore_t s = SEMAPHORE_NULL; if (task == TASK_NULL || value < 0 || policy > SYNC_POLICY_MAX) { *new_semaphore = SEMAPHORE_NULL; return KERN_INVALID_ARGUMENT; } s = (semaphore_t) zalloc (semaphore_zone); if (s == SEMAPHORE_NULL) { *new_semaphore = SEMAPHORE_NULL; return KERN_RESOURCE_SHORTAGE; } wait_queue_init(&s->wait_queue, policy); /* also inits lock */ s->count = value; s->ref_count = 1; /* * Create and initialize the semaphore port */ s->port = ipc_port_alloc_kernel(); if (s->port == IP_NULL) { /* This will deallocate the semaphore */ semaphore_dereference(s); *new_semaphore = SEMAPHORE_NULL; return KERN_RESOURCE_SHORTAGE; } ipc_kobject_set (s->port, (ipc_kobject_t) s, IKOT_SEMAPHORE); /* * Associate the new semaphore with the task by adding * the new semaphore to the task's semaphore list. * * Associate the task with the new semaphore by having the * semaphores task pointer point to the owning task's structure. */ task_lock(task); enqueue_head(&task->semaphore_list, (queue_entry_t) s); task->semaphores_owned++; s->owner = task; s->active = TRUE; task_unlock(task); *new_semaphore = s; return KERN_SUCCESS; }
/* * Routine: semaphore_notify * Purpose: * Called whenever the Mach port system detects no-senders * on the semaphore port. * * When a send-right is first created, a no-senders * notification is armed (and a semaphore reference is donated). * * A no-senders notification will be posted when no one else holds a * send-right (reference) to the semaphore's port. This notification function * will consume the semaphore reference donated to the extant collection of * send-rights. */ void semaphore_notify(mach_msg_header_t *msg) { mach_no_senders_notification_t *notification = (void *)msg; ipc_port_t port = notification->not_header.msgh_remote_port; semaphore_t semaphore; assert(ip_active(port)); assert(IKOT_SEMAPHORE == ip_kotype(port)); semaphore = (semaphore_t)port->ip_kobject; semaphore_dereference(semaphore); }
kern_return_t semaphore_wait_signal_trap_internal( mach_port_name_t wait_name, mach_port_name_t signal_name, void (*caller_cont)(kern_return_t)) { semaphore_t wait_semaphore; semaphore_t signal_semaphore; kern_return_t kr; kr = port_name_to_semaphore(signal_name, &signal_semaphore); if (kr == KERN_SUCCESS) { kr = port_name_to_semaphore(wait_name, &wait_semaphore); if (kr == KERN_SUCCESS) { kr = semaphore_wait_internal(wait_semaphore, signal_semaphore, (mach_timespec_t *)0, caller_cont); semaphore_dereference(wait_semaphore); } semaphore_dereference(signal_semaphore); } return kr; }
kern_return_t semaphore_wait_signal_trap_internal( mach_port_name_t wait_name, mach_port_name_t signal_name, void (*caller_cont)(kern_return_t)) { semaphore_t wait_semaphore; semaphore_t signal_semaphore; kern_return_t kr; kr = port_name_to_semaphore(signal_name, &signal_semaphore); if (kr == KERN_SUCCESS) { kr = port_name_to_semaphore(wait_name, &wait_semaphore); if (kr == KERN_SUCCESS) { kr = semaphore_wait_internal(wait_semaphore, signal_semaphore, 0ULL, SEMAPHORE_OPTION_NONE, caller_cont); semaphore_dereference(wait_semaphore); } semaphore_dereference(signal_semaphore); } return kr; }
kern_return_t semaphore_signal_internal_trap(mach_port_name_t sema_name) { semaphore_t semaphore; kern_return_t kr; kr = port_name_to_semaphore(sema_name, &semaphore); if (kr == KERN_SUCCESS) { kr = semaphore_signal_internal(semaphore, THREAD_NULL, SEMAPHORE_SIGNAL_PREPOST); semaphore_dereference(semaphore); if (kr == KERN_NOT_WAITING) kr = KERN_SUCCESS; } return kr; }
kern_return_t semaphore_wait_trap_internal( mach_port_name_t name, void (*caller_cont)(kern_return_t)) { semaphore_t semaphore; kern_return_t kr; kr = port_name_to_semaphore(name, &semaphore); if (kr == KERN_SUCCESS) { kr = semaphore_wait_internal(semaphore, SEMAPHORE_NULL, (mach_timespec_t *)0, caller_cont); semaphore_dereference(semaphore); } return kr; }
/* * Routine: convert_semaphore_to_port * Purpose: * Convert a semaphore reference to a send right to a * semaphore port. * * Consumes the semaphore reference. If the semaphore * port currently has no send rights (or doesn't exist * yet), the reference is donated to the port to represent * all extant send rights collectively. */ ipc_port_t convert_semaphore_to_port (semaphore_t semaphore) { ipc_port_t port, send; if (semaphore == SEMAPHORE_NULL) return (IP_NULL); /* caller is donating a reference */ port = semaphore->port; if (!IP_VALID(port)) { port = ipc_port_alloc_kernel(); assert(IP_VALID(port)); ipc_kobject_set_atomically(port, (ipc_kobject_t) semaphore, IKOT_SEMAPHORE); /* If we lose the race, deallocate and pick up the other guy's port */ if (!OSCompareAndSwapPtr(IP_NULL, port, &semaphore->port)) { ipc_port_dealloc_kernel(port); port = semaphore->port; assert(ip_kotype(port) == IKOT_SEMAPHORE); assert(port->ip_kobject == (ipc_kobject_t)semaphore); } } ip_lock(port); assert(ip_active(port)); send = ipc_port_make_send_locked(port); if (1 == port->ip_srights) { ipc_port_t old_notify; /* transfer our ref to the port, and arm the no-senders notification */ assert(IP_NULL == port->ip_nsrequest); ipc_port_nsrequest(port, port->ip_mscount, ipc_port_make_sonce_locked(port), &old_notify); /* port unlocked */ assert(IP_NULL == old_notify); } else { /* piggyback on the existing port reference, so consume ours */ ip_unlock(port); semaphore_dereference(semaphore); } return (send); }
/* * Routine: semaphore_signal_all_trap * * Trap interface to the semaphore_signal_all function. */ kern_return_t semaphore_signal_all_trap( struct semaphore_signal_all_trap_args *args) { mach_port_name_t sema_name = args->signal_name; semaphore_t semaphore; kern_return_t kr; kr = port_name_to_semaphore(sema_name, &semaphore); if (kr == KERN_SUCCESS) { kr = semaphore_signal_internal(semaphore, THREAD_NULL, SEMAPHORE_SIGNAL_ALL); semaphore_dereference(semaphore); if (kr == KERN_NOT_WAITING) kr = KERN_SUCCESS; } return kr; }