Home |
RTKernel-32 Programming Manual Multitasking, Real-Time, and RTKernel-32 Alternate APIs for RTKernel-32 Preemptive or Cooperative Multitasking? Waiting for Several Events Starting Objects' Methods as Tasks Performance and Interrupt Response Times Task Switches in Cooperative Scheduling Using the FPU in Interrupt Handlers |
Waiting for Several EventsEven if a task must react to different types of events, polling should be avoided, if possible. The best strategy for this case is to split the task such that each task only has to wait for one event. Alternatively, events can be bundled. Given that a task must react to two different data packets passed to it through mailboxes, one - not very elegant - solution could be: typedef struct { int a, b, c; } Rec1; typedef struct { float a, b, c; } Rec2; RTKMailbox Box1, Box2; void RTKAPI PollTask(void * p) { Rec1 S1; Rec2 S2; while (1) { if (RTKGetCond(Box1, &S1)) /* process S1 */ ; if (RTKGetCond(Box2, &S2)) /* process S2 */ ; } } Even if no data is ready for processing, this task would use up all CPU time. A better solution would be to combine types Rec1 and Rec2, and use only one mailbox: typedef enum { Rec_1, Rec_2 } DataKind; typedef struct { DataKind Typ; union { Rec1 S1; Rec2 S2; } Data; } MultiData; RTKMailbox BigBox; void RTKAPI MBWaitTask(void * p) { MultiData S; while (1) { RTKGet(BigBox, &S); switch (S.Typ) { case Rec_1: /* process S.Data.S1 */ break; case Rec_2: /* process S.Data.S2 */ break; } } } In this example, no CPU time is wasted. Preemptive or Cooperative Multitasking?
|