summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/shared.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mesa/main/shared.c')
-rw-r--r--src/mesa/main/shared.c48
1 files changed, 30 insertions, 18 deletions
diff --git a/src/mesa/main/shared.c b/src/mesa/main/shared.c
index c3e93b5a583..c07ce823846 100644
--- a/src/mesa/main/shared.c
+++ b/src/mesa/main/shared.c
@@ -388,28 +388,40 @@ free_shared_state(struct gl_context *ctx, struct gl_shared_state *shared)
/**
- * Decrement shared state object reference count and potentially free it
- * and all children structures.
- *
- * \param ctx GL context.
- * \param shared shared state pointer.
- *
- * \sa free_shared_state().
+ * gl_shared_state objects are ref counted.
+ * If ptr's refcount goes to zero, free the shared state.
*/
void
-_mesa_release_shared_state(struct gl_context *ctx,
- struct gl_shared_state *shared)
+_mesa_reference_shared_state(struct gl_context *ctx,
+ struct gl_shared_state **ptr,
+ struct gl_shared_state *state)
{
- GLint RefCount;
-
- _glthread_LOCK_MUTEX(shared->Mutex);
- RefCount = --shared->RefCount;
- _glthread_UNLOCK_MUTEX(shared->Mutex);
+ if (*ptr == state)
+ return;
+
+ if (*ptr) {
+ /* unref old state */
+ struct gl_shared_state *old = *ptr;
+ GLboolean delete;
+
+ _glthread_LOCK_MUTEX(old->Mutex);
+ assert(old->RefCount >= 1);
+ old->RefCount--;
+ delete = (old->RefCount == 0);
+ _glthread_UNLOCK_MUTEX(old->Mutex);
+
+ if (delete) {
+ free_shared_state(ctx, old);
+ }
- assert(RefCount >= 0);
+ *ptr = NULL;
+ }
- if (RefCount == 0) {
- /* free shared state */
- free_shared_state( ctx, shared );
+ if (state) {
+ /* reference new state */
+ _glthread_LOCK_MUTEX(state->Mutex);
+ state->RefCount++;
+ *ptr = state;
+ _glthread_UNLOCK_MUTEX(state->Mutex);
}
}