lib: made API multi thread safe(untested)
authorAmarnath Valluri <amarnath.valluri@linux.intel.com>
Fri, 18 Oct 2013 08:39:48 +0000 (11:39 +0300)
committerAmarnath Valluri <amarnath.valluri@linux.intel.com>
Fri, 18 Oct 2013 08:39:48 +0000 (11:39 +0300)
lib/Makefile.am
lib/message-port.c
lib/msgport-factory.c [new file with mode: 0644]
lib/msgport-factory.h [new file with mode: 0644]
lib/msgport-manager.c
lib/msgport-manager.h

index c2e11e7..16aa133 100644 (file)
@@ -8,6 +8,8 @@ libmessage_port_la_SOURCES = \
     msgport-service.c \
     msgport-manager.h \
     msgport-manager.c \
+    msgport-factory.h \
+    msgport-factory.c \
     $(NULL)
 
 libmessage_port_la_includedir = $(includedir)/
index a5d1744..f24eab8 100644 (file)
@@ -1,4 +1,5 @@
 #include "message-port.h"
+#include "msgport-factory.h"
 #include "msgport-manager.h"
 #include "msgport-utils.h"
 #include "common/log.h"
@@ -8,10 +9,9 @@ _messageport_register_port (const char *name, gboolean is_trusted, messageport_m
 {
     int port_id = 0; /* id of the port created */
     messageport_error_e res;
-    MsgPortManager *manager = msgport_get_manager ();
+    MsgPortManager *manager = msgport_factory_get_manager ();
 
-    g_assert (manager);
-    g_assert (MSGPORT_IS_MANAGER (manager));
+    if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
 
     res = msgport_manager_register_service (manager, name, is_trusted, cb, &port_id);
 
@@ -22,7 +22,9 @@ static messageport_error_e
 _messageport_check_remote_port (const char *app_id, const char *port, gboolean is_trusted, gboolean *exists)
 {
     messageport_error_e res;
-    MsgPortManager *manager = msgport_get_manager ();
+    MsgPortManager *manager = msgport_factory_get_manager ();
+
+    if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
 
     res = msgport_manager_check_remote_service (manager, app_id, port, is_trusted, NULL);
 
@@ -34,7 +36,9 @@ _messageport_check_remote_port (const char *app_id, const char *port, gboolean i
 static messageport_error_e
 _messageport_send_message (const char *app_id, const char *port, gboolean is_trusted, bundle *message)
 {
-    MsgPortManager *manager = msgport_get_manager ();
+    MsgPortManager *manager = msgport_factory_get_manager ();
+
+    if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
 
     GVariant *v_data = bundle_to_variant_map (message);
 
@@ -44,7 +48,9 @@ _messageport_send_message (const char *app_id, const char *port, gboolean is_tru
 messageport_error_e
 _messageport_send_bidirectional_message (int id, const gchar *remote_app_id, const gchar *remote_port, gboolean is_trusted, bundle *message)
 {
-    MsgPortManager *manager = msgport_get_manager ();
+    MsgPortManager *manager = msgport_factory_get_manager ();
+
+    if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
 
     GVariant *v_data = bundle_to_variant_map (message);
 
@@ -104,20 +110,22 @@ messageport_send_bidirectional_trusted_message (int id, const char *remote_app_i
 }
 
 messageport_error_e
-messageport_get_local_port_name(int port_id, char **name_out)
+messageport_get_local_port_name(int id, char **name_out)
 {
-    MsgPortManager *manager = msgport_get_manager ();
+    MsgPortManager *manager = msgport_factory_get_manager ();
+
+    if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
 
-    return msgport_manager_get_service_name (manager, port_id, name_out);
+    return msgport_manager_get_service_name (manager, id, name_out);
 }
 
 messageport_error_e
-messageport_check_trusted_local_port (int id, gboolean *trusted)
+messageport_check_trusted_local_port (int id, gboolean *is_trusted_out)
 {
-    (void) id;
+    MsgPortManager *manager = msgport_factory_get_manager ();
 
-    if (trusted) *trusted = FALSE;
+    if (!manager) return MESSAGEPORT_ERROR_IO_ERROR;
 
-    return MESSAGEPORT_ERROR_NONE;
+    return msgport_manager_get_service_is_trusted (manager, id, is_trusted_out);
 }
 
diff --git a/lib/msgport-factory.c b/lib/msgport-factory.c
new file mode 100644 (file)
index 0000000..7247e09
--- /dev/null
@@ -0,0 +1,54 @@
+#include "msgport-factory.h"
+#include "msgport-manager.h"
+#include <glib.h>
+
+GHashTable *__managers ; /* GThread:MsgPortManager */
+G_LOCK_DEFINE(managers);
+
+static 
+void msgport_factory_init ()
+{
+    G_LOCK(managers);
+
+    if (!__managers)
+        __managers = g_hash_table_new_full (g_direct_hash, g_direct_equal,
+                              NULL, (GDestroyNotify)g_object_unref);
+
+    G_UNLOCK(managers);
+}
+
+void msgport_factory_uninit ()
+{
+    G_LOCK(managers);
+
+    if (__managers) {
+        g_hash_table_destroy (__managers);
+        __managers = NULL;
+    }
+
+    G_UNLOCK(managers);
+}
+
+MsgPortManager * msgport_factory_get_manager () 
+{
+    MsgPortManager *manager = NULL;
+    GThread *self_thread = g_thread_self ();
+
+    if (!__managers) msgport_factory_init ();
+
+    G_LOCK(managers);
+
+    manager = MSGPORT_MANAGER (g_hash_table_lookup (__managers, self_thread));
+
+    if (!manager) {
+        manager = msgport_manager_new ();
+
+        if (manager) 
+            g_hash_table_insert (__managers, (gpointer)self_thread, manager);
+    }
+
+    G_UNLOCK(managers);
+
+    return manager;
+}
+
diff --git a/lib/msgport-factory.h b/lib/msgport-factory.h
new file mode 100644 (file)
index 0000000..8fd5677
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef __MSGPORT_FACTORY_H
+#define __MSGPORT_FACTORY_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef struct _MsgPortManager MsgPortManager;
+
+MsgPortManager * msgport_factory_get_manager ();
+
+G_END_DECLS
+
+#endif /* __MSGPORT_FACTORY_H */
index 38cbee5..cac8229 100644 (file)
@@ -103,17 +103,6 @@ MsgPortManager * msgport_manager_new ()
     return g_object_new (MSGPORT_TYPE_MANAGER, NULL);
 }
 
-static MsgPortManager *__manager;
-
-MsgPortManager * msgport_get_manager () 
-{
-    if (!__manager) {
-        __manager = msgport_manager_new ();
-    }
-
-    return __manager;
-}
-
 static messageport_error_e
 _create_and_cache_service (MsgPortManager *manager, gchar *object_path, messageport_message_cb cb, int *service_id)
 {
@@ -246,7 +235,22 @@ msgport_manager_get_service_name (MsgPortManager *manager, int service_id, gchar
     if (!service) return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
 
     *name_out = g_strdup (msgport_service_name (service));
-    DBG ("PORT NAME : %s", *name_out);
+
+    return MESSAGEPORT_ERROR_NONE;
+}
+
+messageport_error_e
+msgport_manager_get_service_is_trusted (MsgPortManager *manager, int service_id, gboolean *is_trusted_out)
+{
+    MsgPortService *service = NULL;
+    g_return_val_if_fail (manager && MSGPORT_IS_MANAGER (manager), MESSAGEPORT_ERROR_IO_ERROR);
+    g_return_val_if_fail (manager->proxy, MESSAGEPORT_ERROR_IO_ERROR);
+    g_return_val_if_fail (service_id && is_trusted_out, MESSAGEPORT_ERROR_INVALID_PARAMETER);
+
+    service = _get_local_port (manager, service_id);
+    if (!service) return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
+
+    *is_trusted_out = msgport_service_is_trusted (service);
 
     return MESSAGEPORT_ERROR_NONE;
 }
index 5718fd0..30ca590 100644 (file)
@@ -25,7 +25,7 @@ struct _MsgPortManagerClass
 GType msgport_manager_get_type (void);
 
 MsgPortManager *
-msgport_get_manager ();
+msgport_manager_new ();
 
 messageport_error_e
 msgport_manager_register_service (MsgPortManager *manager, const gchar *port_name, gboolean is_trusted, messageport_message_cb cb, int *service_id_out);
@@ -37,6 +37,9 @@ messageport_error_e
 msgport_manager_get_service_name (MsgPortManager *manager, int port_id, gchar **name_out);
 
 messageport_error_e
+msgport_manager_get_service_is_trusted (MsgPortManager *manager, int port_id, gboolean *is_trusted_out);
+
+messageport_error_e
 msgport_manager_unregister_service (MsgPortManager *manager, int service_id);
 
 messageport_error_e