[Mesa-dev] [PATCH 4/5] mesa/samplerobj: use atomics for reference counting

Bartosz Tomczyk bartosz.tomczyk86 at gmail.com
Mon Apr 10 20:08:55 UTC 2017


---
 src/mesa/main/mtypes.h     |  1 -
 src/mesa/main/samplerobj.c | 16 ++++------------
 2 files changed, 4 insertions(+), 13 deletions(-)

diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h
index d37a60d61c..5a1be17a92 100644
--- a/src/mesa/main/mtypes.h
+++ b/src/mesa/main/mtypes.h
@@ -968,7 +968,6 @@ typedef enum
  */
 struct gl_sampler_object
 {
-   mtx_t Mutex;
    GLuint Name;
    GLint RefCount;
    GLchar *Label;               /**< GL_KHR_debug */
diff --git a/src/mesa/main/samplerobj.c b/src/mesa/main/samplerobj.c
index 183f1d2a86..fa826b5bef 100644
--- a/src/mesa/main/samplerobj.c
+++ b/src/mesa/main/samplerobj.c
@@ -38,6 +38,7 @@
 #include "main/macros.h"
 #include "main/mtypes.h"
 #include "main/samplerobj.h"
+#include "util/u_atomic.h"
 
 
 struct gl_sampler_object *
@@ -61,7 +62,6 @@ static void
 delete_sampler_object(struct gl_context *ctx,
                       struct gl_sampler_object *sampObj)
 {
-   mtx_destroy(&sampObj->Mutex);
    free(sampObj->Label);
    free(sampObj);
 }
@@ -78,16 +78,11 @@ _mesa_reference_sampler_object_(struct gl_context *ctx,
 
    if (*ptr) {
       /* Unreference the old sampler */
-      GLboolean deleteFlag = GL_FALSE;
       struct gl_sampler_object *oldSamp = *ptr;
 
-      mtx_lock(&oldSamp->Mutex);
       assert(oldSamp->RefCount > 0);
-      oldSamp->RefCount--;
-      deleteFlag = (oldSamp->RefCount == 0);
-      mtx_unlock(&oldSamp->Mutex);
 
-      if (deleteFlag)
+      if (p_atomic_dec_zero(&oldSamp->RefCount))
          delete_sampler_object(ctx, oldSamp);
 
       *ptr = NULL;
@@ -96,18 +91,16 @@ _mesa_reference_sampler_object_(struct gl_context *ctx,
 
    if (samp) {
       /* reference new sampler */
-      mtx_lock(&samp->Mutex);
-      if (samp->RefCount == 0) {
+      if (p_atomic_read(&samp->RefCount) == 0) {
          /* this sampler's being deleted (look just above) */
          /* Not sure this can every really happen.  Warn if it does. */
          _mesa_problem(NULL, "referencing deleted sampler object");
          *ptr = NULL;
       }
       else {
-         samp->RefCount++;
+         p_atomic_inc(&samp->RefCount);
          *ptr = samp;
       }
-      mtx_unlock(&samp->Mutex);
    }
 }
 
@@ -118,7 +111,6 @@ _mesa_reference_sampler_object_(struct gl_context *ctx,
 static void
 _mesa_init_sampler_object(struct gl_sampler_object *sampObj, GLuint name)
 {
-   mtx_init(&sampObj->Mutex, mtx_plain);
    sampObj->Name = name;
    sampObj->RefCount = 1;
    sampObj->WrapS = GL_REPEAT;
-- 
2.12.2



More information about the mesa-dev mailing list