This more to start discussion on protect shared data. I am
not really familiar with js data so what this patch tried
to protect is the js object, if thread is running hold
the main thread to wait until the js thread finish. mean thread
only block once want to destroy and create new js object.
---
 src/mozjs.c     |    9 ++++++++-
 src/pacrunner.h |    3 +++
 src/proxy.c     |   31 +++++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+), 1 deletions(-)

diff --git a/src/mozjs.c b/src/mozjs.c
index de40c3f..015108e 100644
--- a/src/mozjs.c
+++ b/src/mozjs.c
@@ -214,8 +214,12 @@ const char *__pacrunner_mozjs_execute(const char *url, 
const char *host)
 
        DBG("url %s host %s", url, host);
 
-       if (jsctx == NULL)
+       pacrunner_proxy_lock(current_proxy);
+
+       if (jsctx == NULL){
+               pacrunner_proxy_unlock(current_proxy);
                return "DIRECT";
+       }
 
        tmpurl = JS_strdup(jsctx, url);
        tmphost = JS_strdup(jsctx, host);
@@ -223,6 +227,7 @@ const char *__pacrunner_mozjs_execute(const char *url, 
const char *host)
        if (tmpurl == NULL || tmphost == NULL) {
                JS_free(jsctx, tmphost);
                JS_free(jsctx, tmpurl);
+               pacrunner_proxy_unlock(current_proxy);
                return NULL;
        }
 
@@ -242,8 +247,10 @@ const char *__pacrunner_mozjs_execute(const char *url, 
const char *host)
 
        if (result) {
                answer = JS_GetStringBytes(JS_ValueToString(jsctx, rval));
+               pacrunner_proxy_unlock(current_proxy);
                return answer;
        }
+       pacrunner_proxy_unlock(current_proxy);
 
        return NULL;
 }
diff --git a/src/pacrunner.h b/src/pacrunner.h
index 0dce86d..429174b 100644
--- a/src/pacrunner.h
+++ b/src/pacrunner.h
@@ -81,6 +81,9 @@ void pacrunner_proxy_unref(struct pacrunner_proxy *proxy);
 const char *pacrunner_proxy_get_interface(struct pacrunner_proxy *proxy);
 const char *pacrunner_proxy_get_script(struct pacrunner_proxy *proxy);
 
+void pacrunner_proxy_lock(struct pacrunner_proxy *proxy);
+void pacrunner_proxy_unlock(struct pacrunner_proxy *proxy);
+
 int pacrunner_proxy_set_method(struct pacrunner_proxy *proxy,
                                        enum pacrunner_proxy_method method);
 int pacrunner_proxy_set_direct(struct pacrunner_proxy *proxy);
diff --git a/src/proxy.c b/src/proxy.c
index 1838eba..7b91187 100644
--- a/src/proxy.c
+++ b/src/proxy.c
@@ -35,6 +35,7 @@ struct pacrunner_proxy {
        char *url;
        char *script;
        char *server;
+       GMutex *mutex;
 };
 
 struct pacrunner_proxy *pacrunner_proxy_create(const char *interface)
@@ -51,6 +52,7 @@ struct pacrunner_proxy *pacrunner_proxy_create(const char 
*interface)
 
        proxy->interface = g_strdup(interface);
        proxy->method = PACRUNNER_PROXY_METHOD_UNKNOWN;
+       proxy->mutex = g_mutex_new();
 
        DBG("proxy %p", proxy);
 
@@ -93,10 +95,17 @@ void pacrunner_proxy_unref(struct pacrunner_proxy *proxy)
        if (g_atomic_int_dec_and_test(&proxy->refcount) == FALSE)
                return;
 
+       g_mutex_lock(proxy->mutex);
+
        __pacrunner_mozjs_set_proxy(NULL);
 
        reset_proxy(proxy);
 
+       g_mutex_unlock(proxy->mutex);
+
+       g_mutex_free(proxy->mutex);
+       proxy->mutex = NULL;
+
        g_free(proxy->interface);
        g_free(proxy);
 }
@@ -160,11 +169,15 @@ static void download_callback(char *content, void 
*user_data)
                goto done;
        }
 
+       g_mutex_lock(proxy->mutex);
+
        g_free(proxy->script);
        proxy->script = content;
 
        __pacrunner_mozjs_set_proxy(proxy);
 
+       g_mutex_unlock(proxy->mutex);
+
 done:
        pacrunner_proxy_unref(proxy);
 }
@@ -182,11 +195,13 @@ int pacrunner_proxy_set_auto(struct pacrunner_proxy 
*proxy, const char *url)
        if (err < 0)
                return err;
 
+       g_mutex_lock(proxy->mutex);
        g_free(proxy->url);
        proxy->url = g_strdup(url);
 
        g_free(proxy->script);
        proxy->script = NULL;
+       g_mutex_unlock(proxy->mutex);
 
        pacrunner_proxy_ref(proxy);
 
@@ -220,10 +235,12 @@ int pacrunner_proxy_set_script(struct pacrunner_proxy 
*proxy,
        g_free(proxy->url);
        proxy->url = NULL;
 
+       g_mutex_lock(proxy->mutex);     
        g_free(proxy->script);
        proxy->script = g_strdup(script);
 
        __pacrunner_mozjs_set_proxy(proxy);
+       g_mutex_unlock(proxy->mutex);
 
        return 0;
 }
@@ -245,14 +262,28 @@ int pacrunner_proxy_set_server(struct pacrunner_proxy 
*proxy,
        if (err < 0)
                return err;
 
+       g_mutex_lock(proxy->mutex);
        g_free(proxy->server);
        proxy->server = g_strdup(server);
 
        __pacrunner_mozjs_set_proxy(proxy);
+       g_mutex_lock(proxy->mutex);
 
        return 0;
 }
 
+void pacrunner_proxy_lock(struct pacrunner_proxy *proxy)
+{
+       if (proxy!= NULL)
+               g_mutex_lock(proxy->mutex);
+}
+
+void pacrunner_proxy_unlock(struct pacrunner_proxy *proxy)
+{
+       if (proxy != NULL)
+               g_mutex_unlock(proxy->mutex);
+}
+
 int __pacrunner_proxy_init(void)
 {
        DBG("");
-- 
1.7.2.3

_______________________________________________
connman mailing list
connman@connman.net
http://lists.connman.net/listinfo/connman

Reply via email to