Libuv: uv_check_t and uv_prepare_t

I read the libuv book , however the section on checking and training observers is incomplete, so the only information I found was in uv.h:

  / *
  * uv_prepare_t is a subclass of uv_handle_t.
  *
  * Every active prepare handle gets its callback called exactly once per loop
  * iteration, just before the system blocks to wait for completed i / o.
  * /

and

  / *
  * uv_check_t is a subclass of uv_handle_t.
  *
  * Every active check handle gets its callback called exactly once per loop
  * iteration, just after the system returns from blocking.
  * /

I was wondering if there was any special use of libuv validation and observer training.

I am writing a built-in binding of node.js to the C ++ library, which should handle events issued from different threads, therefore, naturally, the calling calls should be called from the main thread. I tried using uv_async_t , however libuv does not guarantee that the callback will be called once for every uv_async_send , so this does not work for me.

That's why I decided to go with my own thread-safe event queue, which I want to check periodically. Therefore, I was wondering if there would be use of verification or training an observer for this purpose.


In fact, my current solution uses the uv_async_t observer - every time I get an event, I put it in the queue and call uv_async_send - so when the callback is finally called, I process all the events in the queue.

My concern with this approach is that many events can actually queue until a callback is called and can be invalidated meanwhile (with the help of invalidation, I mean, it now makes no sense to process them in this point).

So, I want to be able to check the event queue as often as possible - which the observers check / prepare , maybe, but maybe it's an overflow for this (and mutex blocking) at each iteration of the event loop?

And, more importantly, maybe they should fulfill some special purpose than just providing a one-time callback in an iteration queue?

thanks

+4
source share
1 answer

You can use the preparation descriptor to check the queue for events, and the async descriptor only to wake the loop.

If you use only the preparation descriptor, you can enter a situation where the cycle is locked for I / O and no one will process the queue until it completes the polling. The asynchronous descriptor will "wake up" the loop, and the next time you run the handlers, you will process the queue.

+1
source

Source: https://habr.com/ru/post/1495992/


All Articles