Page 1 of 1

Events improvement or misunderstanding?

Posted: Mon Aug 19, 2013 12:55 am
by recurry
Hi Giovanni and other ChibiOS experts

Events have the potential to be very powerful but the feature seems to be missing what appears to be an easy to implement but important capability - at least in the way I understand to use them which may be totally wrong so feel free to correct me if necessary.

In any case, the way I understand events is they are a 1 to N type of signaling mechanism. I also see that there can be potentially many different event sources for many different events or types of events which may need to be signaled to a thread. I understand how to create an event source. How to create listeners, register them and wait for the event to happen and it works fine.

The capability I find missing is if I have a thread that is listening to two different event sources. chEvtWaitOne (or any of the other chEvtWaitxxx functions) do not have an EventSource parameter and the only way to determine which event source a thread woke up from is via the event mask parameter. While this provides up to 32 events (which may not be a problem for most) this one event mask is shared across the entire system and requires coordinating their values in a way that has less structure than I like. it seems cleaner to me if each event mask were associated with only one event source and I could do something like the following in a thread:

Code: Select all

static msg_t   buttonsThread(void *arg)
{
   chRegSetThreadName("ButtonPoll");

   EventListener button1_el;
   EventListener button2_el;
   chEvtRegister(&buttonOnePressEvent, &button1_el, buttonsPressEvent);
   chEvtRegister(&buttonTwoButtonEvent, &button2_el,buttonsPressEvent)
   
   while (True)
   {
      switch (buttonState)
            {
            case 0:
               // Start of new sequence - Waiting for button event
               chEvtWaitOne(&buttonOnePressEvent, buttonsPressEvent);
               if (BUTTON_DRV0.state == ICU_ACTIVE)
                  .
                  .
                  .
                  .
               break;
            case 1:
               chEvtWaitOne(&buttonTwoPressEvent, buttonsPressEvent);
                  .
                  .
                  .
               break;
            case 2:
         .
         .
         .
         .
         .
   }



I may be all wet in this thought process but it allows me to have unlimited events that won't collide with each other over a single unified event mask as it current stands. Also, the code above isn't real code. I just tried to make something up quickly and it may be completely stupid so let's not get bogged down in that code. It's sole purpose was to show the type of mechanism I'm talking about.

Thanks,
Ron

Re: Events improvement or misunderstanding?

Posted: Mon Aug 19, 2013 8:34 am
by Giovanni
Hi Ron,

I understand what you propose but there are some misunderstandings that have to be addressed:

recurry wrote:the way I understand events is they are a 1 to N type of signaling mechanism


It is N to N, a source can be associated to N threads and each thread can be listening to N sources.

recurry wrote: While this provides up to 32 events (which may not be a problem for most) this one event mask is shared across the entire system and requires coordinating their values in a way that has less structure than I like.


The mask is limited to 32 source IDs (or 16 or 8, depending on architecture) but:
1) The mask is private for each thread, so each thread can have its own 32 source IDs, the ID you register using chEvtRegister() is for the invoking thread only (the listener is private).
2) Even if you are limited to 32 IDs, you can attribute multiple sources to the same ID, for example you may assign CAN TX, CAN RX and CAN ERROR sources to ID 4, when serving ID 4 then you verify which of them actually pulsed the event using the CAN API.

recurry wrote: chEvtWaitOne (or any of the other chEvtWaitxxx functions) do not have an EventSource parameter and the only way to determine which event source a thread woke up from is via the event mask parameter


The wait() functions return a mask of the event source IDs that triggered the wakeup. The mask is usually (but not necessarily) passed to chEvtDispatch() in order to have separate event handlers associated to the IDs. Alternatively you can check the individual flags in the returned mask using bit operations.
You can understand which sources caused the wakeup because you choose the IDs when registering the listeners.

Not very simple, I know, but it solves a rather complex problem.

Giovanni

Re: Events improvement or misunderstanding?

Posted: Mon Aug 19, 2013 4:27 pm
by recurry
Hi Giovanni,

My explanation was rather obtuse now that I re-read it and see your responses. The primary issue I have is related to your last response. That is that a thread will wakeup as a result of any event and it must be checked programmatically after it wakes up to determine if it was awakened by the correct event of interest. In other words, I would like to be able to issue a blocking system call that will be awakened by one and only one event and I don't have to suffer the overhead of testing whether it was the right one.

Why do I care? Imagine I have several threads that most are asleep most of the time but on a particular event (say a particular button is pushed) I'd like certain threads to wake up on only that button but not others. Unless I'm still misunderstanding the way it works, every thread will awake for every event and then have to check which source actually pulsed the event (as you describe in your kind responses).

Is there some architectural reason why the event mechansim doesn't allow waiting on only one event exclusively?

BTW, I'm very pleased with ChibiOS. You've really done a wonderful job! I even have it working with Rowley Crossworks with internal build and it's version of GCC although I've resorted to editing with Eclipse because it's editor is far superior. Debugging with Crossworks because it's debug environment and less kludgy than Eclipse and one doesn't have to jump through hoops to make it work.. (Ahhhh, if Eclipse debug environment was as clean and reliable as Crossworks it would be perfect!).

Anyhow, thank you so much for your response and all that you do and have done to give us this awesome product!

Best,
Ron

Re: Events improvement or misunderstanding?

Posted: Mon Aug 19, 2013 4:45 pm
by recurry
So, to add to my previous post. Perhaps I'm going about this all wrong. Let me describe what I want to do and perhaps there is a better OS mechanism to address it.

The device I'm working on has multiple pushbuttons. One button is for user input. The interface is designed to perform different functions depending on how the button is pushed - one long push, one short push, double tap, triple tap. I've got the following architecture:

- An ICU driver which uses the "width" call-back to send an event to a thread which sleeps until this event comes in. This thread does the long, short, double, triple decoding.

- I'd like that decoding thread to broadcast two types of events: 1. Long push decoded and 2. All other types of pushes or taps.

- On the receiving end I'd like to have a certain subset of threads wakeup on the long push and certain threads wakeup on the "others".

- Having every thread wakeup to check every event for relevancy adds unnecessary overhead and power consumption therefore I'd like to have only those threads which specifically need to wakeup do so.

Is there a better way to accomplish this than with events?

THanks again!
Ron

Re: Events improvement or misunderstanding?

Posted: Mon Aug 19, 2013 5:37 pm
by Giovanni
Hi,

You should be able to do exactly what you need: associate threads only to the event sources they need and serve one event at time:

Code: Select all

chEvtRegister(&source0, &el0, 0);
chEvtRegister(&source1, &el1, 1);
...
chEvtRegister(&sourceN, &elN, N);
...
chEvtDispatch(handlers_array, chEvtWaitAny(ALL_EVENTS));


The above code will jump into an event-specific handler each time there is something to do.

The limit is 32 unique source per thread, but each thread has its own 32 unique sources.

There is a small guide regarding events attached in this thread, give it a try.

Giovanni

Re: Events improvement or misunderstanding?

Posted: Mon Aug 19, 2013 7:15 pm
by recurry
Thanks Giovanni,

That's not going to work. I'm not building a driver. I've got various threads that all need to take input from the buttons and only need to wake up when a certain button is pressed. Obviously you had a different purpose in mind for "events" than I'm thinking about how they are intended to be used and I'm sorry for not getting that and taking your time. I've studied everything I can get on events and I still don't totally understand how they are supposed to work because it seems contradictory. I've found some code out there that people have put together but it doesn't seem to be using events correctly either.

For instance. I don't understand this system call:

chEvtRegister(&eventImuIrq, &self_el1, EVT_IMU_IRQ);

If I'm registering with the event source why must I also tell it the event mask for that source? If it's already identifyed by the "EventSource" why does it need the mask? Perhaps does this mean there are 32 events per event source? That would be awesome but all the documentation I read says there are essentially only 32 types of events (for Arm anyway) for the entire system so while you could have multiple event sources with the same event mask that is likely not useful.

I guess I'm going to have to dig down into the code to see how it works. Was trying to avoid that but I'm too dense to understand your explanations. :roll:

Cheers,
Ron

Re: Events improvement or misunderstanding?

Posted: Mon Aug 19, 2013 7:31 pm
by Giovanni
If the events were identified by their sources there could be only 32 sources in all the system, as I tried to explain before, each thread can listen to a set of 32 events and each thread can have its own set of events.

This why there can be more than 32 unique sources in the system and this is why you specify a "local mask" when registering.

recurry wrote:chEvtRegister(&eventImuIrq, &self_el1, EVT_IMU_IRQ);


EVT_IMU_IRQ is the ID that the registering thread is assigning to the source eventImuIrq. self_el1 is the proxy object called event listener.

Giovanni

Re: Events improvement or misunderstanding?

Posted: Mon Aug 19, 2013 11:04 pm
by recurry
Ok, I took a look at the kernel code and I think I have it. You used the "event mask" concept to enable the "any" and "all" capability without huge code complexity. Here's my understanding now it simple (to me) terms.

1. Because "event listeners" can be created at will, any number of listener threads can be created.
2. Potentially there can also be any number of event sources however the total number of absolutely unique events can only be the number of bits in the "event mask".
3. Because an event will only be rescheduled from a specific "wait" call via the event listener/source it has registered with it is possible to have event masks duplicated ( i.e. the same mask value used in conjunction with multiple event listener/sources) without interference as long as a thread has only registered with one event listener/source.
4.In the case where a thread is registered with multiple event listener/sources the thread will not be able to determine which event source woke the thread up unless a unique event mask is used for each source. In which case the thread will need to test the event mask returned from the "wait" call to make that determination.
5. Although not absolutely required, inadvertent collisions in threads registered with multiple event listener/sources will be avoided by centralize the definitions of all event mask definitions. In an ARM device this is likely not much of a restriction although in 8 bit processor it could be.

It's actually beginning to look quite simple to me - if the above is correct. I think I do and, in looking at the scheduler code, I think I understand why you are using the event mask construct.

Do I have this correct now?

Thanks again,
Ron

Re: Events improvement or misunderstanding?

Posted: Tue Aug 20, 2013 7:52 am
by Giovanni
It is correct, as long you get that the event IDs are private of each thread then it is all OK.

just write the code now :)

Giovanni