- 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);
- }