95 lines
2.9 KiB
C
95 lines
2.9 KiB
C
// demo.c -- Demonstration of the cooperative task switching library
|
|
//
|
|
// Shows priority scheduling, round-robin, pausing/resuming, and
|
|
// dynamic priority changes.
|
|
|
|
#include <stdio.h>
|
|
#include "taskswitch.h"
|
|
|
|
// Forward declarations
|
|
static void pauseTarget(void *arg);
|
|
static void worker(void *arg);
|
|
|
|
|
|
static void pauseTarget(void *arg) {
|
|
(void)arg;
|
|
for (int32_t i = 0; i < 10; i++) {
|
|
printf(" [pauser] iteration %d\n", (int)i);
|
|
tsYield();
|
|
}
|
|
printf(" [pauser] finished\n");
|
|
}
|
|
|
|
|
|
static void worker(void *arg) {
|
|
const char *name = (const char *)arg;
|
|
int32_t pri = tsGetPriority(tsCurrentId());
|
|
|
|
for (int32_t i = 0; i < 5; i++) {
|
|
printf(" [%s] step %d (priority %d)\n", name, (int)i, (int)pri);
|
|
tsYield();
|
|
}
|
|
printf(" [%s] finished\n", name);
|
|
}
|
|
|
|
|
|
int main(void) {
|
|
printf("=== Cooperative Task Switching Demo ===\n\n");
|
|
|
|
if (tsInit(8) != TS_OK) {
|
|
printf("ERROR: failed to initialize task system\n");
|
|
return 1;
|
|
}
|
|
|
|
int32_t tHigh = tsCreate("high", worker, "high", 0, TS_PRIORITY_HIGH);
|
|
int32_t tNorm = tsCreate("normal", worker, "normal", 0, TS_PRIORITY_NORMAL);
|
|
int32_t tLow = tsCreate("low", worker, "low", 0, TS_PRIORITY_LOW);
|
|
int32_t tPauser = tsCreate("pauser", pauseTarget, NULL, 0, TS_PRIORITY_NORMAL);
|
|
|
|
if (tHigh < 0 || tNorm < 0 || tLow < 0 || tPauser < 0) {
|
|
printf("ERROR: failed to create tasks\n");
|
|
tsShutdown();
|
|
return 1;
|
|
}
|
|
|
|
printf("Created: high=%d normal=%d low=%d pauser=%d\n\n", (int)tHigh, (int)tNorm, (int)tLow, (int)tPauser);
|
|
|
|
// Phase 1: all tasks run, but high-priority tasks get more turns per round.
|
|
// Low (priority 0) gets 1 turn, normal (5) gets 6, high (10) gets 11.
|
|
printf("--- Phase 1: Priority scheduling ---\n");
|
|
for (int32_t i = 0; i < 6; i++) {
|
|
printf("[main] yield %d\n", (int)i);
|
|
tsYield();
|
|
}
|
|
|
|
// Phase 2: pause the pauser task so it stops being scheduled
|
|
printf("\n--- Phase 2: Pause 'pauser' ---\n");
|
|
tsPause((uint32_t)tPauser);
|
|
printf("[main] pauser state: %d (expect %d=paused)\n", tsGetState((uint32_t)tPauser), TaskStatePaused);
|
|
for (int32_t i = 0; i < 4; i++) {
|
|
printf("[main] yield %d (pauser is paused)\n", (int)i);
|
|
tsYield();
|
|
}
|
|
|
|
// Phase 3: resume pauser
|
|
printf("\n--- Phase 3: Resume 'pauser' ---\n");
|
|
tsResume((uint32_t)tPauser);
|
|
for (int32_t i = 0; i < 4; i++) {
|
|
printf("[main] yield %d\n", (int)i);
|
|
tsYield();
|
|
}
|
|
|
|
// Phase 4: boost low to the highest priority so it gets more turns
|
|
printf("\n--- Phase 4: Boost 'low' to highest ---\n");
|
|
tsSetPriority((uint32_t)tLow, TS_PRIORITY_HIGH + 5);
|
|
for (int32_t i = 0; i < 6; i++) {
|
|
printf("[main] yield %d\n", (int)i);
|
|
tsYield();
|
|
}
|
|
|
|
printf("\nActive tasks: %u\n", (unsigned)tsActiveCount());
|
|
printf("=== Demo complete ===\n");
|
|
|
|
tsShutdown();
|
|
return 0;
|
|
}
|