Skip to content

Commit

Permalink
Reset is_present at the start of each time step
Browse files Browse the repository at this point in the history
  • Loading branch information
erlingrj committed May 20, 2024
1 parent 411ba58 commit 0c8e3c8
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## May 20
- Implemented a priority queue for the event quue and a simple FIFO for the reaction queue
- Integrated with Unity test framework.
- Add triggers, children and reactions to the generic Reactor struct.

## May 19
- Added timer.h and timer.c which is a trivial addition and hello_timer.
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ model-of-computation target at embedded and resource-constrained 32 bit systems.
- CMake-based
- Optimized for single-threaded runtime, but support for enclaves which enable
real-time scheduling.
- Avoid malloc as much as possible
- Avoid malloc as much as possible (or entirely?)

## References
`reactor-uc` draws inspiration from the following existing open-source projects:
Expand All @@ -20,12 +20,12 @@ real-time scheduling.
- qpc
- ssm-runtime

## TODO:
## TODO for the MVP:
- [x] Basic support for timers
- [x] Basic support for ports
- [x] Basic connections
- [ ] Implement Event Queue and Reaction Queue
- [ ] Port the platform abstraction from reactor-c
- [ ] Level assignment algorithm
- [x] Implement Event Queue and Reaction Queue
- [ ] Level assignment algorithm (Includes the more elaborate connection setup)
- [ ] Casuality cycle algorithm
- [ ] Physical actions
- [ ] Physical actions
- [ ] Platform abstraction for native/linux/posix, zephyr, flexpret
7 changes: 3 additions & 4 deletions examples/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ int startup_handler(Reaction *_self) {

void MyReaction_ctor(MyReaction *self, Reactor *parent) {
Reaction_ctor(&self->super, parent, startup_handler, NULL, 0);
self->super.level = 0;
}

void MyReactor_ctor(MyReactor *self, Environment *env) {
Expand All @@ -47,12 +48,10 @@ void MyReactor_ctor(MyReactor *self, Environment *env) {
self->super.register_startup(&self->super, &self->startup.super);
}

MyReactor my_reactor;
Reactor *reactors[] = {(Reactor *)&my_reactor};

int main() {
Environment env;
Environment_ctor(&env, reactors, 1);
MyReactor my_reactor;
Environment_ctor(&env, (Reactor *)&my_reactor);
MyReactor_ctor(&my_reactor, &env);
env.assemble(&env);
env.start(&env);
Expand Down
9 changes: 4 additions & 5 deletions examples/hello_action.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ int action_handler(Reaction *_self) {
printf("Action = %d\n", self->my_action.value);
self->my_action.next_value = self->cnt++;
tag_t tag = {.time = self->super.env->current_tag.time + 1, .microstep = 0};
self->my_action.super.schedule_at((Trigger *)&self->my_action.super, tag);
self->my_action.super.schedule_at(&self->my_action.super, tag);
return 0;
}

void MyReaction_ctor(MyReaction *self, Reactor *parent) {
Reaction_ctor(&self->super, parent, action_handler, self->effects, 1);
self->super.level = 0;
}

void MyReactor_ctor(struct MyReactor *self, Environment *env) {
self->_reactions[0] = (Reaction *)&self->my_reaction;
self->_triggers[0] = (Trigger *)&self->my_reaction;
self->_triggers[0] = (Trigger *)&self->startup;
self->_triggers[1] = (Trigger *)&self->my_action;
Reactor_ctor(&self->super, env, NULL, 0, self->_reactions, 1, self->_triggers, 2);
MyAction_ctor(&self->my_action, &self->super);
Expand All @@ -66,11 +67,9 @@ void MyReactor_ctor(struct MyReactor *self, Environment *env) {
}

struct MyReactor my_reactor;
Reactor *reactors[] = {(Reactor *)&my_reactor};
Environment env;

int main() {
Environment_ctor(&env, reactors, 1);
Environment_ctor(&env, (Reactor *)&my_reactor);
MyReactor_ctor(&my_reactor, &env);
env.assemble(&env);
env.start(&env);
Expand Down
13 changes: 7 additions & 6 deletions examples/hello_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ int timer_handler(Reaction *_self) {
return 0;
}

void MyReaction_ctor(MyReaction *self, Reactor *parent) { Reaction_ctor(&self->super, parent, timer_handler, NULL, 0); }
void MyReaction_ctor(MyReaction *self, Reactor *parent) {
Reaction_ctor(&self->super, parent, timer_handler, NULL, 0);
self->super.level = 0;
}

void MyReactor_ctor(struct MyReactor *self, Environment *env) {
self->_reactions[0] = (Reaction *)&self->my_reaction;
Expand All @@ -38,12 +41,10 @@ void MyReactor_ctor(struct MyReactor *self, Environment *env) {
self->timer.super.super.register_effect(&self->timer.super.super, &self->my_reaction.super);
}

struct MyReactor my_reactor;
Reactor *reactors[] = {(Reactor *)&my_reactor};
Environment env;

int main() {
Environment_ctor(&env, reactors, 1);
struct MyReactor my_reactor;
Environment env;
Environment_ctor(&env, (Reactor *)&my_reactor);
MyReactor_ctor(&my_reactor, &env);
env.assemble(&env);
env.start(&env);
Expand Down
5 changes: 2 additions & 3 deletions include/reactor-uc/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
typedef struct Environment Environment;

struct Environment {
Reactor **reactors;
size_t reactors_size;
Reactor *main;
Scheduler scheduler;
tag_t current_tag;
bool keep_alive;
Expand All @@ -17,7 +16,7 @@ struct Environment {
int (*wait_until)(Environment *self, instant_t wakeup_time);
};

void Environment_ctor(Environment *self, Reactor **reactors, size_t reactors_size);
void Environment_ctor(Environment *self, Reactor *main);
void Environment_assemble(Environment *self);
void Environment_start(Environment *self);

Expand Down
4 changes: 0 additions & 4 deletions include/reactor-uc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
#include <stdlib.h>

#define CAT(a, b) a##b

#define PASTE(a, b) CAT(a, b)

#define JOIN(prefix, name) PASTE(prefix, PASTE(_, name))

#define SWAP(TYPE, a, b) \
{ \
TYPE temp = *(a); \
Expand All @@ -20,7 +17,6 @@
}

#define foreach(a, b, c) for (JOIN(a, it) c = JOIN(JOIN(a, it), each)(b); !c.done; c.step(&c))

#define len(a) (sizeof(a) / sizeof(*(a)))

#endif
5 changes: 2 additions & 3 deletions src/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ int Environment_wait_until(Environment *self, instant_t wakeup_time) {
return 0;
}

void Environment_ctor(Environment *self, Reactor **reactors, size_t reactors_size) {
self->reactors = reactors;
self->reactors_size = reactors_size;
void Environment_ctor(Environment *self, Reactor *main) {
self->main = main;
self->assemble = Environment_assemble;
self->start = Environment_start;
self->keep_alive = false;
Expand Down
6 changes: 4 additions & 2 deletions src/queues.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ Reaction *ReactionQueue_pop(ReactionQueue *self) {
ret = self->array[self->curr_level][self->curr_index];
self->curr_index++;
} else if (self->curr_level < self->max_active_level) {
self->level_size[self->curr_level] = 0;
self->curr_level++;
self->curr_index = 0;
ret = ReactionQueue_pop(self);
} else {
ret = NULL;
Expand All @@ -107,13 +105,17 @@ bool ReactionQueue_empty(ReactionQueue *self) {
void ReactionQueue_reset(ReactionQueue *self) {
self->curr_index = 0;
self->curr_level = 0;
for (int i = 0; i <= self->max_active_level; i++) {
self->level_size[i] = 0;
}
self->max_active_level = -1;
}

void ReactionQueue_ctor(ReactionQueue *self) {
self->insert = ReactionQueue_insert;
self->pop = ReactionQueue_pop;
self->empty = ReactionQueue_empty;
self->reset = ReactionQueue_reset;
self->curr_index = 0;
self->curr_level = 0;
self->max_active_level = -1;
Expand Down
15 changes: 9 additions & 6 deletions src/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
#include "reactor-uc/reactor-uc.h"
#include "reactor-uc/scheduler.h"

// static void _reset_is_present_recursive(Reactor *reactor) {
// (void)reactor;
// // FIXME: How?
// }
static void _reset_is_present_recursive(Reactor *reactor) {
for (size_t i = 0; i < reactor->triggers_size; i++) {
reactor->triggers[i]->is_present = false;
}
for (size_t i = 0; i < reactor->children_size; i++) {
_reset_is_present_recursive(reactor->children[i]);
}
}

void Scheduler_prepare_timestep(Scheduler *self) {
self->reaction_queue.reset(&self->reaction_queue);

// FIXME: Improve this expensive resetting of all `is_present` fields of triggers.

Environment *env = self->env;
for (size_t i = 0; i < env->reactors_size; i++) {
}
_reset_is_present_recursive(env->main);
}
void Scheduler_run(Scheduler *self) {
while (!self->event_queue.empty(&self->event_queue)) {
Expand Down
2 changes: 2 additions & 0 deletions src/trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ void Trigger_ctor(Trigger *self, TriggerType type, Reactor *parent, Reaction **e
self->parent = parent;
self->effects = effects;
self->effects_size = effects_size;
self->effects_registered = 0;
self->sources = sources;
self->sources_size = sources_size;
self->sources_registered = 0;
self->update_value = update_value_func;

self->schedule_at = Trigger_schedule_at;
Expand Down

0 comments on commit 0c8e3c8

Please sign in to comment.