Skip to content

Commit

Permalink
The file buffer fix requires a recursive lock
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipberndt committed Nov 14, 2017
1 parent 8765f81 commit 2c37ec6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ If you'd like to compile pqiv manually, you'll need

* gtk+ 3.0 *or* gtk+ 2.6
* gdk-pixbuf 2.2 (included in gtk+)
* glib 2.8 (with gvfs for opening URLs)
* glib 2.32 (with gvfs for opening URLs)
* cairo 1.6
* gio 2.0
* gdk 2.8
Expand Down
32 changes: 16 additions & 16 deletions lib/filebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct buffered_file {
};

GHashTable *file_buffer_table = NULL;
G_LOCK_DEFINE_STATIC(file_buffer_table);
GRecMutex file_buffer_table_mutex;

#ifdef HAS_MMAP
extern GFile *gfile_for_commandline_arg(const char *);
Expand All @@ -58,7 +58,7 @@ static void buffered_file_mmap_free_helper(struct buffered_file_mmap_info *info)
#endif

GBytes *buffered_file_as_bytes(file_t *file, GInputStream *data, GError **error_pointer) {
G_LOCK(file_buffer_table);
g_rec_mutex_lock(&file_buffer_table_mutex);
if(!file_buffer_table) {
file_buffer_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
}
Expand All @@ -75,7 +75,7 @@ GBytes *buffered_file_as_bytes(file_t *file, GInputStream *data, GError **error_
}

if(!data_bytes) {
G_UNLOCK(file_buffer_table);
g_rec_mutex_unlock(&file_buffer_table_mutex);
return NULL;
}
}
Expand All @@ -89,7 +89,7 @@ GBytes *buffered_file_as_bytes(file_t *file, GInputStream *data, GError **error_
GFileInfo *file_info = g_file_query_info(input_file, G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, error_pointer);
if(!file_info) {
g_object_unref(input_file);
G_UNLOCK(file_buffer_table);
g_rec_mutex_unlock(&file_buffer_table_mutex);
return NULL;
}
goffset input_file_size = g_file_info_get_size(file_info);
Expand Down Expand Up @@ -117,7 +117,7 @@ GBytes *buffered_file_as_bytes(file_t *file, GInputStream *data, GError **error_
else if(!data) {
data = image_loader_stream_file(file, error_pointer);
if(!data) {
G_UNLOCK(file_buffer_table);
g_rec_mutex_unlock(&file_buffer_table_mutex);
return NULL;
}
data_bytes = g_input_stream_read_completely(data, image_loader_cancellable, error_pointer);
Expand All @@ -128,7 +128,7 @@ GBytes *buffered_file_as_bytes(file_t *file, GInputStream *data, GError **error_
}

if(!data_bytes) {
G_UNLOCK(file_buffer_table);
g_rec_mutex_unlock(&file_buffer_table_mutex);
return NULL;
}
}
Expand All @@ -137,19 +137,19 @@ GBytes *buffered_file_as_bytes(file_t *file, GInputStream *data, GError **error_
buffer->data = data_bytes;
}
buffer->ref_count++;
G_UNLOCK(file_buffer_table);
g_rec_mutex_unlock(&file_buffer_table_mutex);
return buffer->data;
}

char *buffered_file_as_local_file(file_t *file, GInputStream *data, GError **error_pointer) {
G_LOCK(file_buffer_table);
g_rec_mutex_lock(&file_buffer_table_mutex);
if(!file_buffer_table) {
file_buffer_table = g_hash_table_new(g_str_hash, g_str_equal);
}
struct buffered_file *buffer = g_hash_table_lookup(file_buffer_table, file->file_name);
if(buffer) {
buffer->ref_count++;
G_UNLOCK(file_buffer_table);
g_rec_mutex_unlock(&file_buffer_table_mutex);
return buffer->file_name;
}

Expand All @@ -172,7 +172,7 @@ char *buffered_file_as_local_file(file_t *file, GInputStream *data, GError **err
data = image_loader_stream_file(file, error_pointer);
if(!data) {
g_hash_table_remove(file_buffer_table, file->file_name);
G_UNLOCK(file_buffer_table);
g_rec_mutex_unlock(&file_buffer_table_mutex);
return NULL;
}
local_data = TRUE;
Expand All @@ -195,7 +195,7 @@ char *buffered_file_as_local_file(file_t *file, GInputStream *data, GError **err
g_object_unref(data);
}
g_hash_table_remove(file_buffer_table, file->file_name);
G_UNLOCK(file_buffer_table);
g_rec_mutex_unlock(&file_buffer_table_mutex);
return NULL;
}

Expand All @@ -204,7 +204,7 @@ char *buffered_file_as_local_file(file_t *file, GInputStream *data, GError **err
if(local_data) {
g_object_unref(data);
}
G_UNLOCK(file_buffer_table);
g_rec_mutex_unlock(&file_buffer_table_mutex);
return NULL;
}

Expand All @@ -219,15 +219,15 @@ char *buffered_file_as_local_file(file_t *file, GInputStream *data, GError **err
}

buffer->ref_count++;
G_UNLOCK(file_buffer_table);
g_rec_mutex_unlock(&file_buffer_table_mutex);
return buffer->file_name;
}

void buffered_file_unref(file_t *file) {
G_LOCK(file_buffer_table);
g_rec_mutex_lock(&file_buffer_table_mutex);
struct buffered_file *buffer = g_hash_table_lookup(file_buffer_table, file->file_name);
if(!buffer) {
G_UNLOCK(file_buffer_table);
g_rec_mutex_unlock(&file_buffer_table_mutex);
return;
}
if(--buffer->ref_count == 0) {
Expand All @@ -242,5 +242,5 @@ void buffered_file_unref(file_t *file) {
}
g_hash_table_remove(file_buffer_table, file->file_name);
}
G_UNLOCK(file_buffer_table);
g_rec_mutex_unlock(&file_buffer_table_mutex);
}

0 comments on commit 2c37ec6

Please sign in to comment.