Home |
RTKernel-32 Programming Manual Multitasking, Real-Time, and RTKernel-32 Alternate APIs for RTKernel-32 Preemptive or Cooperative Multitasking? Avoid Large Message Types Starting Objects' Methods as Tasks Performance and Interrupt Response Times Task Switches in Cooperative Scheduling Using the FPU in Interrupt Handlers |
Avoid Large Message TypesThe length of the data packets for communication via mailboxes or message passing is not limited. However, large data packets can result in performance problems, since the data is copied in every inter-task communication. Instead of channelling the data itself through a mailbox, logical packet numbers or pointers to the data can be used. In the following example, 10 data buffers are allocated. Unused buffers are contained in mailbox Pool. ProducerTask retrieves buffers from Pool as required and passes them to ConsumerTask using mailbox NewData. Since only pointers - not the buffers themselves - are copied to the mailbox, performance is independent of the data packet size. #define MaxBuffer 10 typedef struct { char x[5000]; /* more fields... */ } BufferType; RTKMailbox Pool, NewData; void RTKAPI ProducerTask(void*p) { BufferType * Ptr; while (1) { RTKGet(Pool, &Ptr); /* get new buffer */ /* Ptr-> is filled with data */ RTKPut(NewData, &Ptr); /* pass on buffer */ } } void RTKAPI ConsumerTask(void * p) { BufferType * Ptr; while (1) { RTKGet(NewData, &Ptr); /* wait for new data */ /* process Ptr-> */ RTKPut(Pool, &Ptr); /* release buffer */ } } void main(void) { int i; BufferType * Ptr; RTKernelInit(7); Pool = RTKCreateMailbox(sizeof(BufferType*), MaxBuffer, "Pool"); NewData = RTKCreateMailbox(sizeof(BufferType*), MaxBuffer, "New"); /* allocate and make available all buffers */ for (i = 1; i <= MaxBuffer; i++) { Ptr = RTKAlloc(sizeof(BufferType), "a buffer"); RTKPut(Pool, &Ptr); } /* ... */ } The same technique can be used for message passing.
|