summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fitz/base_memory.c19
1 files changed, 17 insertions, 2 deletions
diff --git a/fitz/base_memory.c b/fitz/base_memory.c
index 32c7ff84..a30d75db 100644
--- a/fitz/base_memory.c
+++ b/fitz/base_memory.c
@@ -264,8 +264,23 @@ static int find_context(fz_context *ctx)
return i;
if (fz_lock_debug_contexts[i] == NULL)
{
- fz_lock_debug_contexts[i] = ctx;
- return i;
+ int gottit;
+ /* We've not locked on this context before, so use
+ * this one for this new context. We might have other
+ * threads trying here too though so, so claim it
+ * atomically. No one has locked on this context
+ * before, so we are safe to take the ALLOC lock. */
+ ctx->locks->lock(ctx->locks->user, FZ_LOCK_ALLOC);
+ /* If it's still free, then claim it as ours,
+ * otherwise we'll keep hunting. */
+ if (fz_lock_debug_contexts[i] == NULL)
+ {
+ gottit = 1;
+ fz_lock_debug_contexts[i] = ctx;
+ }
+ ctx->locks->unlock(ctx->locks->user, FZ_LOCK_ALLOC);
+ if (gottit)
+ return i;
}
}
return -1;