From 290eefde5ccd5f49d0cfeda597c9ddf9ea36eacd Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sun, 3 Jun 2012 22:06:57 +0200 Subject: [PATCH] Use a lock to lock the render thread for printing. --- print.c | 2 ++ render.c | 27 +++++++++++++++++++++++++++ render.h | 15 +++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/print.c b/print.c index 247352b..e1c8739 100644 --- a/print.c +++ b/print.c @@ -93,5 +93,7 @@ cb_print_draw_page(GtkPrintOperation* UNUSED(print_operation), GtkPrintContext* return; } + render_lock(zathura->sync.render_thread); zathura_page_render(page, cairo, true); + render_unlock(zathura->sync.render_thread); } diff --git a/render.c b/render.c index a709f64..3a0146b 100644 --- a/render.c +++ b/render.c @@ -20,6 +20,7 @@ static gint render_thread_sort(gconstpointer a, gconstpointer b, gpointer data); struct render_thread_s { GThreadPool* pool; /**< Pool of threads */ + GStaticMutex mutex; /**< Render lock */ }; static void @@ -31,6 +32,7 @@ render_job(void* data, void* user_data) return; } + girara_debug("rendring page %d\n", zathura_page_get_index(page)); if (render(zathura, page) != true) { girara_error("Rendering failed (page %d)\n", zathura_page_get_index(page)); } @@ -48,6 +50,7 @@ render_init(zathura_t* zathura) } g_thread_pool_set_sort_function(render_thread->pool, render_thread_sort, zathura); + g_static_mutex_init(&render_thread->mutex); return render_thread; @@ -68,6 +71,7 @@ render_free(render_thread_t* render_thread) g_thread_pool_free(render_thread->pool, TRUE, TRUE); } + g_static_mutex_free(&render_thread->mutex); g_free(render_thread); } @@ -119,12 +123,15 @@ render(zathura_t* zathura, zathura_page_t* page) cairo_scale(cairo, scale, scale); } + render_lock(zathura->sync.render_thread); if (zathura_page_render(page, cairo, false) != ZATHURA_ERROR_OK) { + render_unlock(zathura->sync.render_thread); cairo_destroy(cairo); cairo_surface_destroy(surface); return false; } + render_unlock(zathura->sync.render_thread); cairo_restore(cairo); cairo_destroy(cairo); @@ -221,3 +228,23 @@ render_thread_sort(gconstpointer a, gconstpointer b, gpointer data) return 0; } + +void +render_lock(render_thread_t* render_thread) +{ + if (render_thread == NULL) { + return; + } + + g_static_mutex_lock(&render_thread->mutex); +} + +void +render_unlock(render_thread_t* render_thread) +{ + if (render_thread == NULL) { + return; + } + + g_static_mutex_unlock(&render_thread->mutex); +} diff --git a/render.h b/render.h index 435ef83..2d7a12c 100644 --- a/render.h +++ b/render.h @@ -44,4 +44,19 @@ bool render_page(render_thread_t* render_thread, zathura_page_t* page); */ void render_all(zathura_t* zathura); +/** + * Lock the render thread. This is useful if you want to render on your own (e.g + * for printing). + * + * @param render_thread The render thread object. + */ +void render_lock(render_thread_t* render_thread); + +/** + * Unlock the render thread. + * + * @param render_thread The render thread object. + */ +void render_unlock(render_thread_t* render_thread); + #endif // RENDER_H