mruby/c VM Source Code master (2026/08/06)
Loading...
Searching...
No Matches
rrt0.c File Reference

Realtime multitask monitor for mruby/c. More...

#include "mrubyc.h"
#include "_autogen_class_rrt0.h"
Include dependency graph for rrt0.c:

Go to the source code of this file.

Macros

#define MRBC_SCHEDULER_EXIT   0
#define MRBC_MUTEX_TRACE(...)
#define DEPRECATED(msg)
#define NUM_TCB_QUEUE   4
#define q_dormant_   (tcb_queue_[0])
#define q_ready_   (tcb_queue_[1])
#define q_waiting_   (tcb_queue_[2])
#define q_suspended_   (tcb_queue_[3])

Functions

void mrbc_task_q_insert (mrbc_tcb *p_tcb)
void mrbc_task_q_delete (mrbc_tcb *p_tcb)
mrbc_tcbmrbc_task_q_waiting_head (void)
static void preempt_running_task (void)
static int get_timed_wakeup_tick (const mrbc_tcb *t, uint32_t *out)
void mrbc_tick (void)
mrbc_tcbmrbc_tcb_new (int regs_size, enum MrbcTaskState task_state, int priority)
mrbc_tcbmrbc_create_task (const void *byte_code, mrbc_tcb *tcb)
int mrbc_delete_task (mrbc_tcb *tcb)
void mrbc_set_task_name (mrbc_tcb *tcb, const char *name)
mrbc_tcbmrbc_find_task (const char *name)
int mrbc_start_task (mrbc_tcb *tcb)
static void terminate_task (mrbc_tcb *tcb)
int mrbc_run (void)
void mrbc_sleep_ms (mrbc_tcb *tcb, uint32_t ms)
uint32_t mrbc_deadline_after_ms (mrbc_int_t ms, int *p_overflow)
int mrbc_deadline_reached (uint32_t deadline)
void mrbc_register_wakeup (uint32_t wakeup_tick)
void mrbc_wakeup_task (mrbc_tcb *tcb)
void mrbc_relinquish (mrbc_tcb *tcb)
void mrbc_change_priority (mrbc_tcb *tcb, int priority)
void mrbc_suspend_task (mrbc_tcb *tcb)
void mrbc_resume_task (mrbc_tcb *tcb)
void mrbc_terminate_task (mrbc_tcb *tcb)
void mrbc_join_task (mrbc_tcb *tcb, const mrbc_tcb *tcb_join)
mrbc_mutexmrbc_mutex_init (mrbc_mutex *mutex)
int mrbc_mutex_lock (mrbc_mutex *mutex, mrbc_tcb *tcb)
int mrbc_mutex_unlock (mrbc_mutex *mutex, mrbc_tcb *tcb)
int mrbc_mutex_trylock (mrbc_mutex *mutex, mrbc_tcb *tcb)
void mrbc_cleanup (void)
static void c_sleep (mrbc_vm *vm, mrbc_value v[], int argc)
static mrbc_value sub_task_get (mrbc_vm *vm, mrbc_tcb *tcb)
void mrbc_init (void *heap_ptr, unsigned int size)

Variables

static mrbc_tcbtcb_queue_ [NUM_TCB_QUEUE]
static volatile uint32_t tick_
static volatile uint32_t wakeup_tick_ = ((uint32_t)1 << 16)

Detailed Description

Realtime multitask monitor for mruby/c.

Copyright (C) 2015-      Kyushu Institute of Technology.
Copyright (C) 2015-2026  Shimane IT Open-Innovation Center.
Copyright (C) 2026-      Shimane Institute for Industrial Technology.

This file is distributed under BSD 3-Clause License.

Definition in file rrt0.c.

Macro Definition Documentation

◆ DEPRECATED

#define DEPRECATED ( msg)
Value:
do { \
static const char msg1[] = "Warning: "; \
static const char msg2[] = " method will be removed in future version.\n"; \
mrbc_hal_write(2, msg1, sizeof(msg1)-1); \
mrbc_hal_write(2, msg, strlen(msg)); \
mrbc_hal_write(2, msg2, sizeof(msg2)-1); \
} while(0)

Definition at line 37 of file rrt0.c.

◆ MRBC_MUTEX_TRACE

#define MRBC_MUTEX_TRACE ( ...)
Value:
((void)0)

Definition at line 32 of file rrt0.c.

◆ MRBC_SCHEDULER_EXIT

#define MRBC_SCHEDULER_EXIT   0

Definition at line 29 of file rrt0.c.

◆ NUM_TCB_QUEUE

#define NUM_TCB_QUEUE   4

Definition at line 48 of file rrt0.c.

◆ q_dormant_

#define q_dormant_   (tcb_queue_[0])

Definition at line 50 of file rrt0.c.

◆ q_ready_

#define q_ready_   (tcb_queue_[1])

Definition at line 51 of file rrt0.c.

◆ q_suspended_

#define q_suspended_   (tcb_queue_[3])

Definition at line 53 of file rrt0.c.

◆ q_waiting_

#define q_waiting_   (tcb_queue_[2])

Definition at line 52 of file rrt0.c.

Function Documentation

◆ c_sleep()

void c_sleep ( mrbc_vm * vm,
mrbc_value v[],
int argc )
static

(method) sleep for a specified number of seconds (CRuby compatible)

Definition at line 975 of file rrt0.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_timed_wakeup_tick()

int get_timed_wakeup_tick ( const mrbc_tcb * t,
uint32_t * out )
static

Get the timed wakeup tick of a waiting task, if it has one.

A SLEEP task always has a deadline; a QUEUE task has one only when popped with a timeout (queue.wakeup_tick != MRBC_WAIT_FOREVER). Other wait reasons have no timed wakeup.

Parameters
ttask control block.
outreceives the wakeup tick when the function returns non-zero.
Returns
non-zero if the task has a timed wakeup.

Definition at line 191 of file rrt0.c.

Here is the caller graph for this function:

◆ mrbc_change_priority()

void mrbc_change_priority ( mrbc_tcb * tcb,
int priority )

change task priority.

Parameters
tcbtarget task.
prioritypriority value. between 1 and 255.

Definition at line 702 of file rrt0.c.

Here is the call graph for this function:

◆ mrbc_cleanup()

void mrbc_cleanup ( void )

clenaup all resources.

Definition at line 961 of file rrt0.c.

Here is the call graph for this function:

◆ mrbc_create_task()

mrbc_tcb * mrbc_create_task ( const void * byte_code,
mrbc_tcb * tcb )

Create a task specifying bytecode to be executed.

Parameters
byte_codepointer to VM byte code.
tcbTask control block with parameter, or NULL.
Returns
Pointer to mrbc_tcb or NULL.

Definition at line 298 of file rrt0.c.

Here is the call graph for this function:

◆ mrbc_deadline_after_ms()

uint32_t mrbc_deadline_after_ms ( mrbc_int_t ms,
int * p_overflow )

Convert a millisecond timeout into an absolute wakeup tick (deadline).

Parameters
mstimeout in milliseconds (must be >= 0).
p_overflowset to non-zero when the deadline is too far ahead to be expressed in the 31-bit signed tick window.
Returns
absolute deadline tick, normalized so that it never equals the MRBC_WAIT_FOREVER sentinel.

Definition at line 603 of file rrt0.c.

◆ mrbc_deadline_reached()

int mrbc_deadline_reached ( uint32_t deadline)

Test whether an absolute deadline tick has been reached.

Parameters
deadlineabsolute deadline tick from mrbc_deadline_after_ms().
Returns
non-zero if the current tick is at or past the deadline.

Definition at line 625 of file rrt0.c.

◆ mrbc_delete_task()

int mrbc_delete_task ( mrbc_tcb * tcb)

Delete a task.

Parameters
tcbtarget task.
Returns
0 on success, or a negative value on error.

Definition at line 332 of file rrt0.c.

Here is the call graph for this function:

◆ mrbc_find_task()

mrbc_tcb * mrbc_find_task ( const char * name)

find task by name

Parameters
nametask name
Returns
pointer to mrbc_tcb or NULL

Definition at line 370 of file rrt0.c.

◆ mrbc_init()

void mrbc_init ( void * heap_ptr,
unsigned int size )

initialize

Parameters
heap_ptrheap memory buffer.
sizeits size.

Definition at line 1634 of file rrt0.c.

Here is the call graph for this function:

◆ mrbc_join_task()

void mrbc_join_task ( mrbc_tcb * tcb,
const mrbc_tcb * tcb_join )

join the task.

Parameters
tcbtarget task.
tcb_joinjoin task.

Definition at line 789 of file rrt0.c.

Here is the call graph for this function:

◆ mrbc_mutex_init()

mrbc_mutex * mrbc_mutex_init ( mrbc_mutex * mutex)

mutex initialize

Parameters
mutexpointer to mrbc_mutex or NULL.

Definition at line 814 of file rrt0.c.

Here is the call graph for this function:

◆ mrbc_mutex_lock()

int mrbc_mutex_lock ( mrbc_mutex * mutex,
mrbc_tcb * tcb )

mutex lock

Parameters
mutexpointer to mutex.
tcbpointer to TCB.

Definition at line 833 of file rrt0.c.

Here is the call graph for this function:

◆ mrbc_mutex_trylock()

int mrbc_mutex_trylock ( mrbc_mutex * mutex,
mrbc_tcb * tcb )

mutex trylock

Parameters
mutexpointer to mutex.
tcbpointer to TCB.

Definition at line 934 of file rrt0.c.

◆ mrbc_mutex_unlock()

int mrbc_mutex_unlock ( mrbc_mutex * mutex,
mrbc_tcb * tcb )

mutex unlock

Parameters
mutexpointer to mutex.
tcbpointer to TCB.

Definition at line 877 of file rrt0.c.

Here is the call graph for this function:

◆ mrbc_register_wakeup()

void mrbc_register_wakeup ( uint32_t wakeup_tick)

Lower the global next-wakeup tick to account for a new deadline.

The caller must hold the IRQ lock; this only updates wakeup_tick_ when the given deadline is earlier than the currently scheduled one.

Parameters
wakeup_tickabsolute deadline tick to register.

Definition at line 639 of file rrt0.c.

◆ mrbc_relinquish()

void mrbc_relinquish ( mrbc_tcb * tcb)

Relinquish control to other tasks.

Parameters
tcbtarget task.

Definition at line 689 of file rrt0.c.

◆ mrbc_resume_task()

void mrbc_resume_task ( mrbc_tcb * tcb)

resume the task

Parameters
tcbtarget task.

Definition at line 741 of file rrt0.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrbc_run()

int mrbc_run ( void )

execute

Definition at line 448 of file rrt0.c.

Here is the call graph for this function:

◆ mrbc_set_task_name()

void mrbc_set_task_name ( mrbc_tcb * tcb,
const char * name )

set the task name.

Parameters
tcbtarget task.
nametask name

Definition at line 352 of file rrt0.c.

◆ mrbc_sleep_ms()

void mrbc_sleep_ms ( mrbc_tcb * tcb,
uint32_t ms )

Alternative to mrbc_run for Wasm build

sleep for a specified number of milliseconds.

Parameters
tcbtarget task.
mssleep milliseconds.

Definition at line 575 of file rrt0.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrbc_start_task()

int mrbc_start_task ( mrbc_tcb * tcb)

Start execution of dormant task.

Parameters
tcbtarget task.
Returns
0 on success, or a negative value on error.

Definition at line 393 of file rrt0.c.

Here is the call graph for this function:

◆ mrbc_suspend_task()

void mrbc_suspend_task ( mrbc_tcb * tcb)

Suspend the task.

Parameters
tcbtarget task.

Definition at line 722 of file rrt0.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrbc_task_q_delete()

void mrbc_task_q_delete ( mrbc_tcb * p_tcb)

Delete task(TCB) from task queue

Parameters
p_tcbPointer to target TCB

Definition at line 128 of file rrt0.c.

Here is the caller graph for this function:

◆ mrbc_task_q_insert()

void mrbc_task_q_insert ( mrbc_tcb * p_tcb)

Insert task(TCB) to task queue

Parameters
p_tcbPointer to target TCB

Put the task (TCB) into a queue by each state. TCB must be free. (must not be in another queue) The queue is sorted in priority_preemption order. If the same priority_preemption value is in the TCB and queue, it will be inserted at the end of the same value in queue.

Definition at line 94 of file rrt0.c.

Here is the caller graph for this function:

◆ mrbc_task_q_waiting_head()

mrbc_tcb * mrbc_task_q_waiting_head ( void )

get the head of the WAITING task queue.

Provided so that out-of-file features (e.g. Task::Queue) can scan the tasks that are currently in the WAITING state.

Returns
Pointer to the first TCB in the waiting queue, or NULL.

Definition at line 163 of file rrt0.c.

Here is the caller graph for this function:

◆ mrbc_tcb_new()

mrbc_tcb * mrbc_tcb_new ( int regs_size,
enum MrbcTaskState task_state,
int priority )

create (allocate) TCB.

Parameters
regs_sizenum of allocated registers.
task_statetask initial state.
prioritytask priority.
Returns
pointer to TCB.

Code example

// If you want specify default value, see below.
// regs_size: MAX_REGS_SIZE (in vm_config.h)
// task_state: MRBC_TASK_DEFAULT_STATE
// priority: MRBC_TASK_DEFAULT_PRIORITY
mrbc_tcb *tcb;
mrbc_create_task( byte_code, tcb );
mrbc_tcb * mrbc_tcb_new(int regs_size, enum MrbcTaskState task_state, int priority)
Definition rrt0.c:273
mrbc_tcb * mrbc_create_task(const void *byte_code, mrbc_tcb *tcb)
Definition rrt0.c:298
struct RTcb mrbc_tcb
Task control block.
static const int MRBC_TASK_DEFAULT_STATE
Definition rrt0.h:61
static const int MRBC_TASK_DEFAULT_PRIORITY
Definition rrt0.h:60
#define MAX_REGS_SIZE
Definition vm_config.h:25

Definition at line 273 of file rrt0.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrbc_terminate_task()

void mrbc_terminate_task ( mrbc_tcb * tcb)

terminate the task.

Parameters
tcbtarget task.
Note
This API simply ends the task. note that this does not affect the lock status of mutex.

Definition at line 774 of file rrt0.c.

Here is the call graph for this function:

◆ mrbc_tick()

void mrbc_tick ( void )

Tick timer interrupt handler.

Definition at line 213 of file rrt0.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrbc_wakeup_task()

void mrbc_wakeup_task ( mrbc_tcb * tcb)

wake up the task.

Parameters
tcbtarget task.

Definition at line 652 of file rrt0.c.

Here is the call graph for this function:

◆ preempt_running_task()

void preempt_running_task ( void )
inlinestatic

preempt running task

Definition at line 172 of file rrt0.c.

Here is the caller graph for this function:

◆ sub_task_get()

mrbc_value sub_task_get ( mrbc_vm * vm,
mrbc_tcb * tcb )
static

Definition at line 1030 of file rrt0.c.

Here is the call graph for this function:

◆ terminate_task()

void terminate_task ( mrbc_tcb * tcb)
static

Definition at line 414 of file rrt0.c.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ tcb_queue_

mrbc_tcb* tcb_queue_[NUM_TCB_QUEUE]
static

Definition at line 49 of file rrt0.c.

◆ tick_

volatile uint32_t tick_
static

Definition at line 54 of file rrt0.c.

◆ wakeup_tick_

volatile uint32_t wakeup_tick_ = ((uint32_t)1 << 16)
static

Definition at line 55 of file rrt0.c.