[OpenWrt-Devel] Question about conflict between ubus_invoke and uloop_run
Alexandru Ardelean
ardeleanalex at gmail.com
Fri Apr 3 02:19:40 EDT 2015
Hello Kevin,
As far as my understanding goes regarding ubus and uloop, they were not
designed with threading in mind.
Also, as a general rule when using uloop, you wouldn't use threads, because
races could occur (especially when doing ubus calls inside a thread) and
the main uloop loop would terminate.
One general rule would be: you should not use ubus call in threads.
You could use ubus_invoke_async() instead of ubus_invoke().
It is a bit more work than just using ubus_invoke() but if you want
thread-like behaviour, it could replace all the threads you're using.
So, is there a way to re-design your code without using threads and using
ubus async calls ?
On Fri, Apr 3, 2015 at 5:24 AM, XiaoFengMeng <xiaofeng.meng at pfsw.com> wrote:
> HI !
>
> I am Kevin and learning the ubus code and got something that is confusing
> me very much in the code.
>
>
>
> My question is regarding code in ubus_invoke -> ubus_complete_request,
>
>
>
> I can see that there are lots of uloop related code in
> “ubus_complete_request”
>
> if (!registered) {
>
> uloop_init();
>
> ubus_add_uloop(ctx);
>
> }
>
> ….
>
> while (!req->status_msg) {
>
> bool cancelled = uloop_cancelled;
>
>
>
> uloop_cancelled = false;
>
> if (req_timeout) {
>
> timeout = time_end -
> get_time_msec();
>
> if (timeout <= 0) {
>
>
> ubus_set_req_status(req, UBUS_STATUS_TIMEOUT);
>
> break;
>
> }
>
> }
>
> ubus_poll_data(ctx, (unsigned int)
> timeout);
>
>
>
> uloop_cancelled = cancelled;
>
> }
>
>
>
> My question is why is uloop involved here? uloop could be working in
> another thread and also listen on the same socket.
>
> I idea that, before the ubus_invoke sends the request messge, it should
> disable the uloop for the current socket, so that code in uloop_run will
> not receive anything from the same socket.
>
>
>
> Because after calling ubus_add_uloop(ctx), the uloop_run could also be
> receiving the response data
>
> And this function could cause the uloop_run to quit the while loop.
>
> How should I understand the logic here?
>
>
>
> And How should I do if I want to call ubus_invoke as a client and use
> uloop_run as a server at the same process?
>
>
>
> Thanks very much!
>
>
>
> Best regards
>
> Kevin
>
>
>
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel at lists.openwrt.org
> https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.infradead.org/pipermail/openwrt-devel/attachments/20150403/eb0d41d4/attachment.htm>
-------------- next part --------------
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
More information about the openwrt-devel
mailing list