]> git.saurik.com Git - apple/xnu.git/blame_incremental - osfmk/kern/ipc_sync.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_sync.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000 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#include <kern/sync_sema.h>
36#include <kern/sync_lock.h>
37#include <kern/ipc_kobject.h>
38#include <kern/ipc_sync.h>
39#include <ipc/port.h>
40#include <ipc/ipc_space.h>
41#include <ipc/ipc_port.h>
42#include <mach/semaphore.h>
43#include <mach/lock_set_server.h>
44#include <mach/mach_port_server.h>
45#include <mach/port.h>
46
47
48kern_return_t
49port_name_to_semaphore(
50 mach_port_name_t name,
51 semaphore_t *semaphorep)
52{
53 semaphore_t semaphore;
54 ipc_port_t kern_port;
55 kern_return_t kr;
56
57 if (!MACH_PORT_VALID(name)) {
58 *semaphorep = SEMAPHORE_NULL;
59 return KERN_INVALID_NAME;
60 }
61
62 kr = ipc_object_translate(current_space(), name, MACH_PORT_RIGHT_SEND,
63 (ipc_object_t *) &kern_port);
64 if (kr != KERN_SUCCESS) {
65 *semaphorep = SEMAPHORE_NULL;
66 return kr;
67 }
68 /* have the port locked */
69 assert(IP_VALID(kern_port));
70
71 if (!ip_active(kern_port) || (ip_kotype(kern_port) != IKOT_SEMAPHORE)) {
72 ip_unlock(kern_port);
73 *semaphorep = SEMAPHORE_NULL;
74 return KERN_INVALID_ARGUMENT;
75 }
76
77 semaphore = (semaphore_t) kern_port->ip_kobject;
78 assert(semaphore != SEMAPHORE_NULL);
79 semaphore_reference(semaphore);
80 ip_unlock(kern_port);
81
82 *semaphorep = semaphore;
83 return KERN_SUCCESS;
84}
85
86semaphore_t
87convert_port_to_semaphore (ipc_port_t port)
88{
89 semaphore_t semaphore = SEMAPHORE_NULL;
90
91 if (IP_VALID (port)) {
92 ip_lock(port);
93 if (ip_active(port) && (ip_kotype(port) == IKOT_SEMAPHORE)) {
94 semaphore = (semaphore_t) port->ip_kobject;
95 semaphore_reference(semaphore);
96 }
97 ip_unlock(port);
98 }
99
100 return (semaphore);
101}
102
103
104ipc_port_t
105convert_semaphore_to_port (semaphore_t semaphore)
106{
107 ipc_port_t port;
108
109 if (semaphore != SEMAPHORE_NULL)
110 port = ipc_port_make_send(semaphore->port);
111 else
112 port = IP_NULL;
113
114 return (port);
115}
116
117lock_set_t
118convert_port_to_lock_set (ipc_port_t port)
119{
120 lock_set_t lock_set = LOCK_SET_NULL;
121
122 if (IP_VALID (port)) {
123 ip_lock(port);
124 if (ip_active(port) && (ip_kotype(port) == IKOT_LOCK_SET)) {
125 lock_set = (lock_set_t) port->ip_kobject;
126 lock_set_reference(lock_set);
127 }
128 ip_unlock(port);
129 }
130
131 return (lock_set);
132}
133
134ipc_port_t
135convert_lock_set_to_port (lock_set_t lock_set)
136{
137 ipc_port_t port;
138
139 if (lock_set != LOCK_SET_NULL)
140 port = ipc_port_make_send(lock_set->port);
141 else
142 port = IP_NULL;
143
144 return (port);
145}
146