Improve safety of link to event loop 71/19871/3 guppy 6.99.5 7.0.0 7.0.1 7.0.2 7.0.3 7.0.4 guppy/6.99.5 guppy/7.0.0 guppy/7.0.1 guppy/7.0.2 guppy/7.0.3 guppy/7.0.4 guppy_6.99.5 guppy_7.0.0 guppy_7.0.1 guppy_7.0.2 guppy_7.0.3 guppy_7.0.4
authorJosé Bollo <jose.bollo@iot.bzh>
Sat, 5 Jan 2019 20:22:20 +0000 (21:22 +0100)
committerJosé Bollo <jose.bollo@iot.bzh>
Thu, 7 Feb 2019 12:18:35 +0000 (12:18 +0000)
Instead of using the default one that depends on the
current thread and that might be shared with other
items, use an explicit new one running in a dedicated
thread.

Bug-AGL: SPEC-2130

Change-Id: Ie609d19157a5dcaf6e5fa3896dc91d88ba5f214a
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
(cherry picked from commit 11e5020569efe21957c3079c20ffd5f69f514d7a)

src/libhomescreen.cpp

index 84e3472..121def7 100644 (file)
@@ -25,6 +25,7 @@
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
+#include <thread>
 
 #include <libhomescreen.hpp>
 #include "hmi-debug.h"
@@ -87,6 +88,12 @@ static void _on_reply_static(void *closure, struct afb_wsj1_msg *msg)
 }
 
 
+static void event_loop_run(struct sd_event* loop){
+       sd_event_loop(loop);
+       sd_event_unref(loop);
+}
+
+
 /**
  * constructor
  */
@@ -99,14 +106,14 @@ LibHomeScreen::LibHomeScreen()
  */
 LibHomeScreen::~LibHomeScreen()
 {
-       if(mploop)
-       {
-               sd_event_unref(mploop);
-       }
        if(sp_websock != NULL)
        {
                afb_wsj1_unref(sp_websock);
        }
+       if(mploop)
+       {
+               sd_event_exit(mploop, 0);
+       }
 }
 
 /**
@@ -177,13 +184,19 @@ int LibHomeScreen::initialize_websocket()
        mploop = NULL;
        onEvent = nullptr;
        onReply = nullptr;
-       int ret = sd_event_default(&mploop);
+       int ret = sd_event_new(&mploop);
        if(ret < 0)
        {
                HMI_ERROR("libhomescreen","Failed to create event loop");
                goto END;
        }
 
+       {
+               // enforce context to avoid initialization/goto error
+               std::thread th(event_loop_run, mploop);
+               th.detach();
+       }
+
        /* Initialize interface from websocket */
        minterface.on_hangup = _on_hangup_static;
        minterface.on_call = _on_call_static;
@@ -201,10 +214,6 @@ int LibHomeScreen::initialize_websocket()
 
        return 0;
 END:
-       if(mploop)
-       {
-               sd_event_unref(mploop);
-       }
        return -1;
 }