0

OS X has the NSRunLoop which just sits around waiting for timers and sources to fire. Then Apple switch to Grand Central Dispatch (GCD) where you have dispatch_main() to keep the app alive and a bunch of dispatch_source_'s to schedule stuff or to get notifications (from sockets or user actions). If nothing is happening, the app is just idle and doesn't use any CPU power.

Now I want to learn how to write a driver with c++. So I have a raspberry pi (so linux) and once in a while data is coming in from a socket or interrupt.

Instead of polling I want to work with events. So I am searching for the equivalent of NSRunLoop for c++ and linux.

Though I would also like to learn how something like that is or can be implemented. In pseudocode the run loop acts something like this, as far as I know,

timeout = 0
while (true) {
   wait(timeout) || wait for source event other than timer

   loop all timers
      if timer fired
         run timer action

   loop all timers
       timeout = min(timeout, timer.timeNextEvent)

   loop all sources
        if source hasData
           run source action      
}

The thing I don't get is the wait function at the top. How do you wait for a source lets say a timer to fire without going into sleep mode?

I have found many examples of polling and timers that go into sleep mode. But I want to avoid sleep and just wait on interrupts or signals or a user generated event like keyboard input on the command line.

Any pointers on how to proceed?

pasine
  • 11,311
  • 10
  • 49
  • 81
user965972
  • 2,489
  • 2
  • 23
  • 39
  • have you considered looking at [libuv](https://github.com/libuv/libuv) ? – Oleg Bogdanov Jan 12 '17 at 17:55
  • Thanks for the link. That's a big project (also to understand the inner workings). Not sure I would need all of that? Anyway, still trying to understand what the basic principles are behind the "wait" call. – user965972 Jan 12 '17 at 19:38

1 Answers1

0

Probably it's late for this answer but in case someone ends up here, epoll should be the answer. https://man7.org/linux/man-pages/man7/epoll.7.html

Rishabh Agrawal
  • 122
  • 1
  • 8