[java-atk-wrapper: 1/2] Protect access to typeTable and objectTable



commit 0f7728c5b81eee357e429deb8047351a0a7c48bb
Author: Samuel Thibault <samuel thibault ens-lyon org>
Date:   Mon Apr 1 23:15:41 2019 +0200

    Protect access to typeTable and objectTable
    
    We will need this to access them both from the application and from the glib
    thread.

 jni/src/jawimpl.c   | 23 +++++++++++++++++++++++
 jni/src/jawimpl.h   |  1 +
 jni/src/jawobject.c |  6 ++++--
 3 files changed, 28 insertions(+), 2 deletions(-)
---
diff --git a/jni/src/jawimpl.c b/jni/src/jawimpl.c
index d63e689..f259f4c 100644
--- a/jni/src/jawimpl.c
+++ b/jni/src/jawimpl.c
@@ -92,7 +92,9 @@ typedef struct _JawInterfaceInfo {
 
 static gpointer jaw_impl_parent_class = NULL;
 
+static GMutex typeTableMutex;
 static GHashTable *typeTable = NULL;
+static GMutex objectTableMutex;
 static GHashTable *objectTable = NULL;
 static gboolean jaw_debug = FALSE;
 
@@ -106,7 +108,9 @@ object_table_insert (JNIEnv *jniEnv, jobject ac, JawImpl* jaw_impl)
                                           "hashCode",
                                           "()I");
   gint hash_key = (gint)(*jniEnv)->CallIntMethod(jniEnv, ac, jmid);
+  g_mutex_lock(&objectTableMutex);
   g_hash_table_insert(objectTable, GINT_TO_POINTER(hash_key), GINT_TO_POINTER(jaw_impl));
+  g_mutex_unlock(&objectTableMutex);
 }
 
 static JawImpl*
@@ -120,10 +124,15 @@ object_table_lookup (JNIEnv *jniEnv, jobject ac)
                                           "()I" );
   gint hash_key = (gint)(*jniEnv)->CallIntMethod( jniEnv, ac, jmid );
   gpointer value = NULL;
+  g_mutex_lock(&objectTableMutex);
   if (objectTable==NULL)
+  {
     return NULL;
+    g_mutex_unlock(&objectTableMutex);
+  }
 
   value = g_hash_table_lookup(objectTable, GINT_TO_POINTER(hash_key));
+  g_mutex_unlock(&objectTableMutex);
   return (JawImpl*)value;
 }
 
@@ -138,7 +147,9 @@ object_table_remove(JNIEnv *jniEnv, jobject ac)
                                           "()I" );
   gint hash_key = (gint)(*jniEnv)->CallIntMethod( jniEnv, ac, jmid );
 
+  g_mutex_lock(&objectTableMutex);
   g_hash_table_remove(objectTable, GINT_TO_POINTER(hash_key));
+  g_mutex_unlock(&objectTableMutex);
 }
 
 GHashTable*
@@ -147,6 +158,12 @@ jaw_impl_get_object_hash_table(void)
   return objectTable;
 }
 
+GMutex*
+jaw_impl_get_object_hash_table_mutex(void)
+{
+  return &objectTableMutex;
+}
+
 static void
 aggregate_interface(JNIEnv *jniEnv, JawObject *jaw_obj, guint tflag)
 {
@@ -265,8 +282,10 @@ jaw_impl_get_instance (JNIEnv *jniEnv, jobject ac)
   if (jniEnv == NULL)
     return NULL;
 
+  g_mutex_lock(&objectTableMutex);
   if (objectTable == NULL)
     objectTable = g_hash_table_new (NULL, NULL);
+  g_mutex_unlock(&objectTableMutex);
 
   jaw_impl = object_table_lookup(jniEnv, ac);
 
@@ -411,11 +430,13 @@ jaw_impl_get_type (guint tflag)
     NULL
   };
 
+  g_mutex_lock(&typeTableMutex);
   if (typeTable == NULL) {
     typeTable = g_hash_table_new( NULL, NULL );
   }
 
   type = GPOINTER_TO_GTYPE(g_hash_table_lookup(typeTable, GUINT_TO_POINTER(tflag)));
+  g_mutex_unlock(&typeTableMutex);
   if (type == 0) {
     GTypeInfo tinfo = {
       sizeof(JawImplClass),
@@ -465,7 +486,9 @@ jaw_impl_get_type (guint tflag)
     if (tflag & INTERFACE_TABLE_CELL)
       g_type_add_interface_static (type, ATK_TYPE_TABLE_CELL, &atk_table_cell_info);
 
+    g_mutex_lock(&typeTableMutex);
     g_hash_table_insert(typeTable, GINT_TO_POINTER(tflag), GTYPE_TO_POINTER(type));
+    g_mutex_unlock(&typeTableMutex);
   }
 
   return type;
diff --git a/jni/src/jawimpl.h b/jni/src/jawimpl.h
index 8c801a1..8984ead 100644
--- a/jni/src/jawimpl.h
+++ b/jni/src/jawimpl.h
@@ -52,6 +52,7 @@ struct _JawImpl
 JawImpl* jaw_impl_get_instance(JNIEnv*, jobject);
 JawImpl* jaw_impl_find_instance(JNIEnv*, jobject);
 GHashTable* jaw_impl_get_object_hash_table(void);
+GMutex* jaw_impl_get_object_hash_table_mutex(void);
 
 GType jaw_impl_get_type (guint);
 AtkRelationType jaw_impl_get_atk_relation_type(JNIEnv *jniEnv, jstring jrel_key);
diff --git a/jni/src/jawobject.c b/jni/src/jawobject.c
index bef6fe3..c64fb70 100644
--- a/jni/src/jawobject.c
+++ b/jni/src/jawobject.c
@@ -56,7 +56,6 @@ static AtkRelationSet *jaw_object_ref_relation_set (AtkObject *atk_obj);
 static AtkObject *jaw_object_ref_child(AtkObject *atk_obj, gint i);
 
 static gpointer parent_class = NULL;
-static GHashTable *object_table = NULL;
 static JawObject* jaw_object_table_lookup (JNIEnv *jniEnv, jobject ac);
 
 enum {
@@ -650,7 +649,8 @@ jaw_object_ref_child(AtkObject *atk_obj, gint i)
 static JawObject*
 jaw_object_table_lookup (JNIEnv *jniEnv, jobject ac)
 {
-  object_table = jaw_impl_get_object_hash_table();
+  GHashTable *object_table = jaw_impl_get_object_hash_table();
+  GMutex *object_table_mutex = jaw_impl_get_object_hash_table_mutex();
   jclass classAccessibleContext = (*jniEnv)->FindClass( jniEnv,
                                                        "javax/accessibility/AccessibleContext" );
   jmethodID jmid = (*jniEnv)->GetMethodID(jniEnv,
@@ -662,7 +662,9 @@ jaw_object_table_lookup (JNIEnv *jniEnv, jobject ac)
   if (object_table == NULL)
     return NULL;
 
+  g_mutex_lock(object_table_mutex);
   value = g_hash_table_lookup(object_table, GINT_TO_POINTER(hash_key));
+  g_mutex_unlock(object_table_mutex);
   return (JawObject*)value;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]