X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fgallium%2Finclude%2Fpipe%2Fp_refcnt.h;h=1f9088b3e9c87f4b5efc26731cae3933aa89697e;hb=6fd8b9b550713302566bb4c28e49c219870ccfec;hp=60844e40a577d7b751adbe3ee5b2f7321ad8e890;hpb=407e8ae5b167b0193e1e5b1266a5d61ed836dfb5;p=mesa.git diff --git a/src/gallium/include/pipe/p_refcnt.h b/src/gallium/include/pipe/p_refcnt.h index 60844e40a57..1f9088b3e9c 100644 --- a/src/gallium/include/pipe/p_refcnt.h +++ b/src/gallium/include/pipe/p_refcnt.h @@ -51,36 +51,44 @@ pipe_reference_init(struct pipe_reference *reference, unsigned count) } +static INLINE bool +pipe_is_referenced(struct pipe_reference *reference) +{ + return p_atomic_read(&reference->count) != 0; +} + + /** * 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(p_atomic_read(&reference->count) != 0); - p_atomic_inc(&reference->count); - } - - if (*ptr) { - assert(p_atomic_read(&(*ptr)->count) != 0); - 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; } + #ifdef __cplusplus } #endif