gallium: Avoid atomic ops / locking when src is dst.
authorJosé Fonseca <jfonseca@vmware.com>
Tue, 16 Jun 2009 12:05:25 +0000 (13:05 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Tue, 16 Jun 2009 12:05:25 +0000 (13:05 +0100)
src/gallium/include/pipe/p_refcnt.h

index 1f89453e09a809a9265aa0bac5c0475f582daaae..1f9088b3e9c87f4b5efc26731cae3933aa89697e 100644 (file)
@@ -62,29 +62,29 @@ pipe_is_referenced(struct pipe_reference *reference)
  * Set 'ptr' to point to 'reference' and update reference counting.
  * The old thing pointed to, if any, will be unreferenced first.
  * 'reference' may be NULL.
- *
- * XXX: thread safety issues!
  */
 static INLINE bool
 pipe_reference(struct pipe_reference **ptr, struct pipe_reference *reference)
 {
    bool destroy = FALSE;
 
-   /* bump the reference.count first */
-   if (reference) {
-      assert(pipe_is_referenced(reference));
-      p_atomic_inc(&reference->count);
-   }
-
-   if (*ptr) {
-      assert(pipe_is_referenced(*ptr));
-      if (p_atomic_dec_zero(&(*ptr)->count)) {
-         destroy = TRUE;
+   if(*ptr != reference) {
+      /* bump the reference.count first */
+      if (reference) {
+         assert(pipe_is_referenced(reference));
+         p_atomic_inc(&reference->count);
       }
+   
+      if (*ptr) {
+         assert(pipe_is_referenced(*ptr));
+         if (p_atomic_dec_zero(&(*ptr)->count)) {
+            destroy = TRUE;
+         }
+      }
+   
+      *ptr = reference;
    }
 
-   *ptr = reference;
-
    return destroy;
 }