Module: Mesa
Branch: main
Commit: 5cee8434fd281ddae949883738e58e83cd577bfc
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=5cee8434fd281ddae949883738e58e83cd577bfc

Author: Ian Romanick <[email protected]>
Date:   Fri Jul 30 10:57:18 2021 -0700

mesa: Fix tiny race condition in _mesa_debug_get_id

Two threads enter and see *id == 0.  Both threads update the value.
Upon returning, one of the threads might see the overwritten value some
of the time and the updated value other times.  Use cmpxchg to ensure
that there's only ever one value written to *id.

Reviewed-by: Matt Turner <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12136>

---

 src/mesa/main/debug_output.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/mesa/main/debug_output.c b/src/mesa/main/debug_output.c
index 6527aea5771..d47756c9b50 100644
--- a/src/mesa/main/debug_output.c
+++ b/src/mesa/main/debug_output.c
@@ -193,7 +193,8 @@ void
 _mesa_debug_get_id(GLuint *id)
 {
    if (!(*id)) {
-      *id = p_atomic_inc_return(&PrevDynamicID);
+      /* Don't update *id if we raced with some other thread. */
+      p_atomic_cmpxchg(id, 0, p_atomic_inc_return(&PrevDynamicID));
    }
 }
 

Reply via email to