libuv / help

Need help with libuv? Post your question here.
28 stars 7 forks source link

How to run uv_run until uv_stop is called? #108

Closed YumingMa closed 5 years ago

YumingMa commented 5 years ago

Hi,

Thanks in advanced!

I need a way to meet the blew requirement. I just thought about how to make uv_run work always.

Start a thread as a timer scheduler implemented by uv_run. It has been waiting for the addition of a timer form other threads.

scheduler: uv_loop_init(&s_SchedHandler) uv_run(&s_SchedHandler, UV_RUN_DEFAULT); //loop handle timers timeout.

other threads: uv_timer_init(&s_SchedHandler, &Timer->UvTimer) v_timer_start(&Timer->UvTimer, _LW_UvTimerCb, MSecs, 0);

saghul commented 5 years ago

That won't work. The libuv API are in general not thread safe. You cannot create a timer outside of the loop thread. Well, you can, if the loop is not running.

If you want to have a loop which runs always you can add an uv_async_t handle to it. You can then use this handle to stop the loop.

If you want to perform operations from other threads you need to use async handles and uv_async_send, whis is thread-safe.

YumingMa commented 5 years ago

Thanks for your help.

could i use uv_async_t handle + uv_loop as the timer scheduler and other threads add timer into the scheduler by uv_async_send?

saghul commented 5 years ago

Yes, but that sounds inefficient to me IMHO. What exactly are you trying to accomplish?

YumingMa commented 5 years ago

Implement a set of timer APIs. the system or the independently thread as the timer scheduler.

Our timer APIs, the caller don't care the scheduler. Only make sure the timer module init function was called in the program started. then the caller use other APIs, e,g. timer_add and timer_del.

We already have the blow platform timer: the linux kernel soft BH in system as the scheduler in linux kernel. the process add the timer into timer list. a thread + epoll as the scheduler in linux user space, other threads add the timer into epoll.

What I want to have? Use libuv the timer meets other platform. for instance, e.g. IOS windows or cygwin.

Thank you very much for your help and hope to get the good solution. Thanks again!

YumingMa commented 5 years ago

I think i could add a queue, the other threads add a timer into this queue.

the schedule thread add a check handler into uv_loop and uv_run, the check handler move the timer handler into uv_run and the next loop should check the new timer.

I think so the performance is no problem.

saghul commented 5 years ago

I'm afraid the language barrier is a tad too high here :-/

Yes, you could use async handles, a queue and mutexes to achieve this, but again, I think you are better off doing something else which better matches your use-case, since you are barely using the libuv event loop.

On iOS you can use GCD for example and on Cygwin epoll / poll should work IIRC.

YumingMa commented 5 years ago

epoll can't work on iOS/cygwin/windows. So I have to use the libuv as async timer. I am no idea to implement the across platform async timer. Could you have a good idea? Anyway, I should thanks for your help!!!