NewtonOS Ports and Messages

Ports and messages are the main method for tasks to communicate. It is possible to access other tasks’ memory directly, and use semaphores for synchronizing access, but ports and messages offer a cleaner interface.

On the lowest level, communication is handled via the PortSendSWI and PortReceiveSWI interrupts, but the higher level user space interface is defined in the TUPort, TUAsyncMessage, TUMsgToken, and TUSharedMemMsg classes.

Classes

TUSharedMemMsg and TUSharedMem

Any data sent between tasks via ports is handled by the TUSharedMem and TUSharedMemMsg classes. The TUSharedMem class handles access to the data, whereas TUSharedMemMsg adds functionality for specifying additional message parameters such as timeouts and handling message completion.

Is is important to note that the memory which is shared via the TUSharedMem class is not copied into the kernel space, instead, it is merely made available to other tasks. This means that if the shared memory goes out of scope in the originating task, receiving tasks will get invalid data. Any data which is shared via messages must therefore be kept valid until all receiving tasks have processed the data.

TUAsyncMessage

The TUAsyncMessage class encapsulates the data to be send (fMsg) as well as an optional reply as shared memory objects. It offers convenience functions for message completion and synchronization, but for the purpose of sending data, only the fMsg instance variable is used.

TUPort

The TUPort class offers sending and receiving of messages, both synchronously and asynchronously. Transmission is in both cases realized via a TUSharedMemMsg instance, either shared (object ID kBuiltInSMemMsgId) for synchronous communication, or explicitly allocated as part of a TUAsyncMessage instance.

TUPort also allows connecting a reply to an initial message, termed “RPC”. In this case, another shared memory object is allocated to host the reply data.

Asynchronous communication

Data can be send asynchronously by passing a TUAsyncMessage parameter to the TUPort::Send function. The content parameter for that function will be used to set the data buffer of the TUSharedMem instance variable of the message. The content parameter must be valid until the message has been received and the data has been processed by the receiving task.

Synchronous communication

Synchronous message sending is a special form of asynchronous communication, where a single shared TUSharedMemMsg instance is used to hold the data, and the sender is blocked until the message is received.