|
|
< Previous PageNext Page > |
Communication between tasks is an important element of the Mach philosophy. Mach supports a client/server system structure in which tasks (clients) access services by making requests of other tasks (servers) via messages sent over a communication channel.
The endpoints of these communication channels in Mach are called ports, while port rights denote permission to use the channel. The forms of IPC provided by Mach include
The type of IPC object denoted by the port determines the operations permissible on that port, and how (and whether) data transfer occurs.
Important: The IPC facilities in Mac OS X are in a state of transition. In early versions of the system, not all of these IPC types may be implemented.
There are two fundamentally different Mach APIs for raw manipulation
of ports—the mach_ipc
family
and the mach_msg
family.
Within reason, both families may be used with any IPC object; however,
the mach_ipc
calls are
preferred in new code. The mach_ipc
calls maintain
state information where appropriate in order to support the notion
of a transaction. The mach_msg
calls
are supported for legacy code but deprecated; they are stateless.
IPC Transactions and Event Dispatching
Message Queues
Semaphores
Notifications
Locks
Remote Procedure Call (RPC) Objects
When a thread calls mach_ipc_dispatch
,
it repeatedly processes events coming in on the registered port
set. These events could be an argument block from an RPC
object (as the results of a client’s call), a lock object being
taken (as a result of some other thread’s releasing the lock),
a notification or semaphore being posted, or a message coming in
from a traditional message queue.
These events are handled via callouts from mach_msg_dispatch
.
Some events imply a transaction during the lifetime of the callout.
In the case of a lock, the state is the ownership of the lock. When
the callout returns, the lock is released. In the case of remote
procedure calls, the state is the client’s identity, the argument
block, and the reply port. When the callout returns, the reply is
sent.
When the callout returns, the transaction (if any) is completed,
and the thread waits for the next event. The mach_ipc_dispatch
facility
is intended to support work loops.
Originally, the sole style of interprocess communication in Mach was the message queue. Only one task can hold the receive right for a port denoting a message queue. This one task is allowed to receive (read) messages from the port queue. Multiple tasks can hold rights to the port that allow them to send (write) messages into the queue.
A task communicates with another task by building a data structure that contains a set of data elements and then performing a message-send operation on a port for which it holds send rights. At some later time, the task with receive rights to that port will perform a message-receive operation.
A message may consist of some or all of the following:
pure data
copies of memory ranges
port rights
kernel implicit attributes, such as the sender’s security token
The message transfer is an asynchronous operation. The message is logically copied into the receiving task, possibly with copy-on-write optimizations. Multiple threads within the receiving task can be attempting to receive messages from a given port, but only one thread can receive any given message.
Semaphore IPC objects support wait, post, and post all operations. These are counting semaphores, in that posts are saved (counted) if there are no threads currently waiting in that semaphore’s wait queue. A post all operation wakes up all currently waiting threads.
Like semaphores, notification objects also support post and wait operations, but with the addition of a state field. The state is a fixed-size, fixed-format field that is defined when the notification object is created. Each post updates the state field; there is a single state that is overwritten by each post.
A lock is an object that provides mutually exclusive access to a critical section. The primary interfaces to locks are transaction oriented (see “IPC Transactions and Event Dispatching”). During the transaction, the thread holds the lock. When it returns from the transaction, the lock is released.
As the name implies, an RPC object is designed to facilitate and optimize remote procedure calls. The primary interfaces to RPC objects are transaction oriented (see “IPC Transactions and Event Dispatching”)
When an RPC object is created, a set of argument block formats is defined. When an RPC (a send on the object) is made by a client, it causes a message in one of the predefined formats to be created and queued on the object, then eventually passed to the server (the receiver). When the server returns from the transaction, the reply is returned to the sender. Mach tries to optimize the transaction by executing the server using the client’s resources; this is called thread migration.
< Previous PageNext Page > |
Last updated: 2006-11-07
|
Get information on Apple products.
Visit the Apple Store online or at retail locations. 1-800-MY-APPLE Copyright © 2007 Apple Inc. All rights reserved. | Terms of use | Privacy Notice |