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