kiishor / UML-State-Machine-in-C

A minimalist UML State machine framework for finite state machine and hierarchical state machine in C
MIT License
165 stars 46 forks source link

Event handling and protection #41

Open KammutierSpule opened 6 months ago

KammutierSpule commented 6 months ago

Consider the pseudo code:

Task Dispatch:
{
    sem_wait(&Semaphore);   // Wait for event
    dispatch_event(State_Machines, 1)
}

Task Generate Events:
{
    Machine.Event = SOME_EVENT;
    sem_post(&Semaphore);
    Sleep(random());
}

Task Generate Other Events:
{
    Machine.Event = OTHER_EVENT;
    sem_post(&Semaphore);
    Sleep(random());
}

Should the set of event be protected from other tasks?

  1. Since dispatch_event will clear internally Machine.Event,
  2. Since more than one task can generate events,
  3. Since there is no queue of events, once an event is created, no other can be created until the previous is cleared.

What is your opinion on this? Could this be improved?

KammutierSpule commented 6 months ago

Regarding 3. this is already explained in the README.md file.

kiishor commented 6 months ago

Hi Mario, Yes, in the current implementation the state machine handles only one event at a time. This is to keep the implementation as minimal as possible. However, in complex application, this may not be sufficient. The current framework could be improved to support event queue.

I will start working on it soon. I had implemented this feature in the past but it was tightly coupled to my implementation of queue. I don't want to have such dependency in this framework.

Feel free if you also have any suggestion to implement this feature.