Home |
RTKernel-32 Programming Manual Multitasking, Real-Time, and RTKernel-32 Message Passing Alternate APIs for RTKernel-32 |
Message PassingIn addition to mailboxes, RTKernel-32 offers message passing as a mechanism for inter-task communication. With message passing, no data objects like semaphores or mailboxes are required for intermediate data storage; data is copied directly between tasks. Message passing may be thought of as a mailbox of size 0. A fundamental difference to mailboxes is that the sending task explicitly addresses the receiving task. A receiving task, on the other hand, can accept data from any other task. With mailboxes, any task can use any mailbox; a sending task cannot determine who is to receive the data, and a receiving task does not know who sent the data. Another difference is the absence of a data buffer; both the sending and the receiving task must be ready for the transfer before data can be copied. Thus, if two tasks want to exchange data using message passing, the first task is blocked when it reaches its RTKSend or RTKReceive until the second task in turn reaches its respective RTKReceive or RTKSend. The data transfer then takes place and both tasks can continue. Mailboxes do not provide such a tight coupling, e.g., a task can immediately continue running after a Put operation, even if there was no task ready to receive the data. Message passing can also be used solely for synchronization. In this case, the receiving task specifies a data length of 0 and the pointers to the data may be NULL, since RTKernel-32 does not perform a data transfer. The tight coupling between tasks and the lack of data buffering normally disqualifies this mechanism for use by interrupt handlers; however, it can also be used here. The little mailbox demo program can now be modified for message passing. Instead of storing the data in a mailbox, it is passed directly to another task: #include <stdio.h> #include <Rtk32.h> void RTKAPI TaskA(void * p) { int i; printf("Task A: waiting for data\n"); RTKReceive(&i, sizeof(int)); printf("Task A: have received number %i\n", i); } void main(void) { int i; RTKTaskHandle HandleA; printf("\n"); RTKernelInit(3); printf("Main : creating Task A\n"); HandleA = RTKCreateThread(TaskA, 4, 0, 0, NULL, "Task A"); printf("Main : please enter a number: "); fflush(stdin); scanf("%i", &i); RTKSend(HandleA, &i); printf("Main : done.\n"); } Please note that, with mailboxes, the mailbox concerned must always be passed as a parameter. In message passing, on the other hand, the sending task specifies the receiver as a parameter. The receiving task does not specify the source of data; it does not know who sent the data. All message passing functions of RTKernel-32 are documented in the RTKernel-32 Reference Manual.
|