Ensure memory ordering around libagl and EGL refcount operations
The android_atomic_inc/android_atomic_dec functions don't impose
sufficient memory ordering. Using them for object refcounting could
allow an object to be destroyed prior to writes by a different thread
being visible.
Bug: 28820690
Change-Id: Ie018091035174255a22ebc52852528cdaec2d648
diff --git a/opengl/libs/EGL/egl_object.h b/opengl/libs/EGL/egl_object.h
index 673b7da..8f3b9cb 100644
--- a/opengl/libs/EGL/egl_object.h
+++ b/opengl/libs/EGL/egl_object.h
@@ -17,7 +17,7 @@
#ifndef ANDROID_EGL_OBJECT_H
#define ANDROID_EGL_OBJECT_H
-
+#include <atomic>
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
@@ -41,7 +41,7 @@
class egl_object_t {
egl_display_t *display;
- mutable volatile int32_t count;
+ mutable std::atomic_size_t count;
protected:
virtual ~egl_object_t();
@@ -51,8 +51,8 @@
egl_object_t(egl_display_t* display);
void destroy();
- inline int32_t incRef() { return android_atomic_inc(&count); }
- inline int32_t decRef() { return android_atomic_dec(&count); }
+ inline void incRef() { count.fetch_add(1, std::memory_order_relaxed); }
+ inline size_t decRef() { return count.fetch_sub(1, std::memory_order_acq_rel); }
inline egl_display_t* getDisplay() const { return display; }
private: