Description: Use a lock to lock the render thread while printing.
 If the render thread is working while pages are rendered for printing, zathura
 might crash.
Origin: http://git.pwmt.org/?p=zathura.git;a=commit;h=290eefde
Last-Update: 2012-06-11

diff --git a/print.c b/print.c
index e6d683f..7297f72 100644
--- a/print.c
+++ b/print.c
@@ -89,5 +89,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 55fdd5e..4b0d182 100644
--- a/render.c
+++ b/render.c
@@ -18,6 +18,7 @@ static bool render(zathura_t* zathura, zathura_page_t* page);
 struct render_thread_s
 {
   GThreadPool* pool; /**< Pool of threads */
+  GStaticMutex mutex; /**< Render lock */
 };
 
 static void
@@ -44,6 +45,7 @@ render_init(zathura_t* zathura)
   if (render_thread->pool == NULL) {
     goto error_free;
   }
+  g_static_mutex_init(&render_thread->mutex);
 
   return render_thread;
 
@@ -63,6 +65,7 @@ render_free(render_thread_t* render_thread)
   if (render_thread->pool) {
     g_thread_pool_free(render_thread->pool, TRUE, TRUE);
   }
+  g_static_mutex_free(&render_thread->mutex);
 
   g_free(render_thread);
 }
@@ -114,12 +117,15 @@ render(zathura_t* zathura, zathura_page_t* page)
     cairo_scale(cairo, zathura->document->scale, zathura->document->scale);
   }
 
+  render_lock(zathura->sync.render_thread);
   if (zathura_page_render(page, cairo, false) != ZATHURA_PLUGIN_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);
 
@@ -184,3 +190,23 @@ render_all(zathura_t* zathura)
     gtk_widget_queue_resize(page->drawing_area);
   }
 }
+
+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
