GitHub

@@ -0,0 +1,111 @@

1+

/*

2+

** task.h - Task scheduler

3+

**

4+

** See Copyright Notice in mruby.h

5+

*/

6+7+

#ifndef MRUBY_TASK_H

8+

#define MRUBY_TASK_H

9+10+

#include <mruby.h>

11+12+

#ifdef MRB_USE_TASK_SCHEDULER

13+14+

/*

15+

* Task status values (bit-mapped)

16+

*/

17+

enum {

18+

MRB_TASKSTATUS_DORMANT = 0x00, /* Not started or finished */

19+

MRB_TASKSTATUS_READY = 0x02, /* Ready to run */

20+

MRB_TASKSTATUS_RUNNING = 0x03, /* Currently executing */

21+

MRB_TASKSTATUS_WAITING = 0x04, /* Waiting for condition */

22+

MRB_TASKSTATUS_SUSPENDED = 0x08, /* Manually suspended */

23+

};

24+25+

/*

26+

* Task wait reason

27+

*/

28+

enum {

29+

MRB_TASKREASON_NONE = 0x00, /* No specific reason */

30+

MRB_TASKREASON_SLEEP = 0x01, /* Sleeping for time */

31+

MRB_TASKREASON_MUTEX = 0x02, /* Waiting for mutex (reserved) */

32+

MRB_TASKREASON_JOIN = 0x04, /* Waiting for another task */

33+

};

34+35+

/*

36+

* Task Control Block (TCB)

37+

*/

38+

typedef struct mrb_tcb {

39+

struct mrb_tcb *next; /* Linked list pointer */

40+

uint8_t priority; /* Initial priority (0-255, 0=highest) */

41+

uint8_t priority_preemption; /* Effective priority for preemption */

42+

volatile uint8_t timeslice; /* Remaining time slice ticks */

43+

uint8_t status; /* Current status (TASKSTATUS enum) */

44+

uint8_t reason; /* Wait reason (TASKREASON enum) */

45+

mrb_value name; /* Optional task name */

46+47+

union {

48+

uint32_t wakeup_tick; /* Tick count to wake up (for sleep) */

49+

void *mutex; /* Mutex pointer (reserved) */

50+

};

51+

const struct mrb_tcb *tcb_join; /* Task being waited on (for join) */

52+53+

mrb_value task; /* Ruby Task object reference */

54+

mrb_value value; /* Task return value */

55+

struct mrb_context c; /* Execution context (stack, callinfo, etc) */

56+

} mrb_tcb;

57+58+

/*

59+

* Task scheduler state (part of mrb_state)

60+

*/

61+

#define MRB_NUM_TASK_QUEUE 4

62+63+

typedef struct mrb_task_state {

64+

mrb_tcb *queues[MRB_NUM_TASK_QUEUE]; /* Task queues */

65+

volatile uint32_t tick; /* Current tick count */

66+

volatile uint32_t wakeup_tick; /* Next wakeup tick */

67+

volatile mrb_bool switching; /* Context switch pending flag */

68+

} mrb_task_state;

69+70+

/* Queue indices */

71+

#define MRB_TASK_QUEUE_DORMANT 0

72+

#define MRB_TASK_QUEUE_READY 1

73+

#define MRB_TASK_QUEUE_WAITING 2

74+

#define MRB_TASK_QUEUE_SUSPENDED 3

75+76+

/* Configuration */

77+

#ifndef MRB_TICK_UNIT

78+

#define MRB_TICK_UNIT 4 /* Tick period in milliseconds */

79+

#endif

80+81+

#ifndef MRB_TIMESLICE_TICK_COUNT

82+

#define MRB_TIMESLICE_TICK_COUNT 3 /* Number of ticks per timeslice */

83+

#endif

84+85+

#define TASK_STACK_INIT_SIZE 64 /* Initial task stack size */

86+

#define TASK_CI_INIT_SIZE 8 /* Initial task callinfo size */

87+88+

/*

89+

* HAL (Hardware Abstraction Layer) functions

90+

* Platform-specific implementations must provide these

91+

*/

92+

void mrb_task_hal_init(mrb_state *mrb);

93+

void mrb_task_enable_irq(void);

94+

void mrb_task_disable_irq(void);

95+

void mrb_task_hal_idle_cpu(mrb_state *mrb);

96+97+

/*

98+

* Core task scheduler API

99+

*/

100+

void mrb_tick(mrb_state *mrb);

101+

mrb_value mrb_tasks_run(mrb_state *mrb);

102+103+

/*

104+

* Task context status values (extends mrb_fiber_state)

105+

*/

106+

#define MRB_TASK_CREATED (MRB_FIBER_TRANSFERRED + 1)

107+

#define MRB_TASK_STOPPED (MRB_FIBER_TRANSFERRED + 2)

108+109+

#endif /* MRB_USE_TASK_SCHEDULER */

110+111+

#endif /* MRUBY_TASK_H */

Read the original on github.com ↗