From 40f4ed22806b88ba0e26c458915d4695f1f7c201 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Wed, 18 Jan 2012 15:49:09 +0000 Subject: Multi-threading support for MuPDF When we moved over to a context based system, we laid the foundation for a thread-safe mupdf. This commit should complete that process. Firstly, fz_clone_context is properly implemented so that it makes a new context, but shares certain sections (currently just the allocator, and the store). Secondly, we add locking (to parts of the code that have previously just had placeholder LOCK/UNLOCK comments). Functions to lock and unlock a mutex are added to the allocator structure; omit these (as is the case today) and no multithreading is (safely) possible. The context will refuse to clone if these are not provided. Finally we flesh out the LOCK/UNLOCK comments to be real calls of the functions - unfortunately this requires us to plumb fz_context into the fz_keep_storable function (and all the fz_keep_xxx functions that call it). This is the largest section of the patch. No changes expected to any test files. --- fitz/fitz.h | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'fitz/fitz.h') diff --git a/fitz/fitz.h b/fitz/fitz.h index ccfa0bd6..3971f24c 100644 --- a/fitz/fitz.h +++ b/fitz/fitz.h @@ -120,6 +120,8 @@ struct fz_alloc_context_s void *(*malloc)(void *, unsigned int); void *(*realloc)(void *, void *, unsigned int); void (*free)(void *, void *); + void (*lock)(void *); + void (*unlock)(void *); }; /* Default allocator */ @@ -376,6 +378,20 @@ void fz_free_context(fz_context *ctx); void fz_new_aa_context(fz_context *ctx); void fz_free_aa_context(fz_context *ctx); +static inline void +fz_lock(fz_context *ctx) +{ + if (ctx->alloc->lock) + ctx->alloc->lock(ctx->alloc->user); +} + +static inline void +fz_unlock(fz_context *ctx) +{ + if (ctx->alloc->unlock) + ctx->alloc->unlock(ctx->alloc->user); +} + /* * Basic runtime and utility functions */ @@ -737,9 +753,10 @@ enum { void fz_new_store_context(fz_context *ctx, unsigned int max); void fz_free_store_context(fz_context *ctx); +fz_store *fz_store_keep(fz_context *ctx); void fz_debug_store(fz_context *ctx); -void *fz_keep_storable(fz_storable *); +void *fz_keep_storable(fz_context *, fz_storable *); void fz_drop_storable(fz_context *, fz_storable *); void fz_store_item(fz_context *ctx, fz_obj *key, void *val, unsigned int itemsize); @@ -921,7 +938,7 @@ fz_pixmap *fz_new_pixmap_with_data(fz_context *ctx, fz_colorspace *colorspace, i fz_pixmap *fz_new_pixmap_with_rect(fz_context *ctx, fz_colorspace *, fz_bbox bbox); fz_pixmap *fz_new_pixmap_with_rect_and_data(fz_context *ctx, fz_colorspace *, fz_bbox bbox, unsigned char *samples); fz_pixmap *fz_new_pixmap(fz_context *ctx, fz_colorspace *, int w, int h); -fz_pixmap *fz_keep_pixmap(fz_pixmap *pix); +fz_pixmap *fz_keep_pixmap(fz_context *ctx, fz_pixmap *pix); void fz_drop_pixmap(fz_context *ctx, fz_pixmap *pix); void fz_free_pixmap_imp(fz_context *ctx, fz_storable *pix); void fz_clear_pixmap(fz_pixmap *pix); @@ -1009,7 +1026,7 @@ struct fz_colorspace_s }; fz_colorspace *fz_new_colorspace(fz_context *ctx, char *name, int n); -fz_colorspace *fz_keep_colorspace(fz_colorspace *colorspace); +fz_colorspace *fz_keep_colorspace(fz_context *ctx, fz_colorspace *colorspace); void fz_drop_colorspace(fz_context *ctx, fz_colorspace *colorspace); void fz_free_colorspace_imp(fz_context *ctx, fz_storable *colorspace); @@ -1250,7 +1267,7 @@ struct fz_shade_s float *mesh; /* [x y 0], [x y r], [x y t] or [x y c1 ... cn] */ }; -fz_shade *fz_keep_shade(fz_shade *shade); +fz_shade *fz_keep_shade(fz_context *ctx, fz_shade *shade); void fz_drop_shade(fz_context *ctx, fz_shade *shade); void fz_free_shade_imp(fz_context *ctx, fz_storable *shade); void fz_debug_shade(fz_shade *shade); -- cgit v1.2.3