Implemented proper multithreading locking.
authorshort <>
Fri, 22 Aug 2003 07:13:31 +0000 (07:13 +0000)
committershort <>
Fri, 22 Aug 2003 07:13:31 +0000 (07:13 +0000)
src/client/lufs/captivefs-file.c
src/client/lufs/captivefs-vfs.c

index 75dabac..2f25261 100644 (file)
@@ -203,23 +203,31 @@ fail:
 
 /* map: (const gchar *) -> (CaptiveFileObject *) */
 static GHashTable *FileHandle_hash;
+G_LOCK_DEFINE_STATIC(FileHandle_hash);
 
 static void FileHandle_hash_value_destroy_func(CaptiveFileObject *captive_file_object)
 {
        g_return_if_fail(CAPTIVE_FILE_IS_OBJECT(captive_file_object));
 
+       G_LOCK(libcaptive);
        g_object_unref(captive_file_object);
+       G_UNLOCK(libcaptive);
 }
 
 static void FileHandle_hash_init(void)
 {
-       if (FileHandle_hash)
-               return;
-       FileHandle_hash=g_hash_table_new_full(g_str_hash,g_str_equal,
-                       (GDestroyNotify)g_free, /* key_destroy_func */
-                       (GDestroyNotify)FileHandle_hash_value_destroy_func);    /* value_destroy_func */
+       G_LOCK(FileHandle_hash);
+       if (!FileHandle_hash) {
+               FileHandle_hash=g_hash_table_new_full(g_str_hash,g_str_equal,
+                               (GDestroyNotify)g_free, /* key_destroy_func */
+                               (GDestroyNotify)FileHandle_hash_value_destroy_func);    /* value_destroy_func */
+               }
+       G_UNLOCK(FileHandle_hash);
 }
 
+/* FIXME: g_object_ref() the resulting 'CaptiveFileObject'.
+ * Currently we assume open/ops/release is done in single thread.
+ */
 static CaptiveFileObject *FileHandle_lookup(const char *name)
 {
 CaptiveFileObject *captive_file_object;
@@ -227,7 +235,11 @@ CaptiveFileObject *captive_file_object;
        g_return_val_if_fail(name!=NULL,NULL);
 
        FileHandle_hash_init();
-       if (!(captive_file_object=g_hash_table_lookup(FileHandle_hash,name))) {
+       G_LOCK(FileHandle_hash);
+       captive_file_object=g_hash_table_lookup(FileHandle_hash,name);
+       G_UNLOCK(FileHandle_hash);
+
+       if (!captive_file_object) {
                g_warning("FileHandle_lookup: FileHandle not found of: %s",name);
                return NULL;
                }
@@ -267,7 +279,10 @@ GnomeVFSResult errvfsresult;
                return -1;
 
        FileHandle_hash_init();
-       if ((captive_file_object=g_hash_table_lookup(FileHandle_hash,file))) {
+       G_LOCK(FileHandle_hash);
+       captive_file_object=g_hash_table_lookup(FileHandle_hash,file);
+       G_UNLOCK(FileHandle_hash);
+       if (captive_file_object) {
                g_assert(CAPTIVE_FILE_IS_OBJECT(captive_file_object));
                g_warning("captivefs_open: FileHandle already exists of: %s",file);
                return -1;
@@ -301,6 +316,8 @@ GnomeVFSResult errvfsresult;
 
 int captivefs_release(struct captivefs_vfs *captivefs_vfs,const char *file)
 {
+gboolean errbool;
+
        g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
        g_return_val_if_fail(file!=NULL,-1);
 
@@ -308,7 +325,11 @@ int captivefs_release(struct captivefs_vfs *captivefs_vfs,const char *file)
                g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_release: file=%s",file);
 
        FileHandle_hash_init();
-       if (!g_hash_table_remove(FileHandle_hash,file)) {
+       G_LOCK(FileHandle_hash);
+       errbool=g_hash_table_remove(FileHandle_hash,file);
+       G_UNLOCK(FileHandle_hash);
+
+       if (!errbool) {
                g_warning("captivefs_release: FileHandle not found of: %s",file);
                return -1;
                }
index dc769fa..3943766 100644 (file)
@@ -35,19 +35,27 @@ gboolean captivefs_vfs_validate(struct captivefs_vfs *captivefs_vfs)
 {
        g_return_val_if_fail(captivefs_vfs!=NULL,FALSE);
 
+       G_LOCK(libcaptive);
        if (!captivefs_vfs->captive_vfs_object) {
 GnomeVFSResult errvfsresult;
 
-               if (captivefs_vfs->parent_pid==getpid())
+               if (captivefs_vfs->parent_pid==getpid()) {
+                       G_UNLOCK(libcaptive);
                        return FALSE;
+                       }
 
-               G_LOCK(libcaptive);
                errvfsresult=captive_vfs_new(&captivefs_vfs->captive_vfs_object,&captivefs_vfs->options);
-               G_UNLOCK(libcaptive);
 
-               g_return_val_if_fail(errvfsresult==GNOME_VFS_OK,FALSE);
+               if (errvfsresult!=GNOME_VFS_OK) {
+                       G_UNLOCK(libcaptive);
+                       g_return_val_if_reached(FALSE);
+                       }
                }
-       g_return_val_if_fail(CAPTIVE_VFS_IS_OBJECT(captivefs_vfs->captive_vfs_object),FALSE);
+       if (!CAPTIVE_VFS_IS_OBJECT(captivefs_vfs->captive_vfs_object)) {
+               G_UNLOCK(libcaptive);
+               g_return_val_if_reached(FALSE);
+               }
+       G_UNLOCK(libcaptive);
 
        return TRUE;
 }