egl/display: make platform detection thread-safe
authorEric Engestrom <eric@engestrom.ch>
Thu, 15 Jun 2017 22:53:55 +0000 (23:53 +0100)
committerEric Engestrom <eric.engestrom@imgtec.com>
Fri, 16 Jun 2017 10:02:06 +0000 (11:02 +0100)
commit311c09165881111c4a596ca7e7b4bce89b059e0f
treebe935fe7fb1f35ff106198a2829cd685f609225f
parent4ca9ae587c083b6f03feb65b4ce84929109d5d59
egl/display: make platform detection thread-safe

Imagine there are 2 threads that both call _eglGetNativePlatform()
simultaneously:
- thread 1 completes the first "if (native_platform ==
  _EGL_INVALID_PLATFORM)" check and is preempted to do something else
- thread 2 executes the whole function, does "native_platform =
  _EGL_NATIVE_PLATFORM" and just before returning it's preempted
- thread 1 wakes up and calls _eglGetNativePlatformFromEnv() which
  returns _EGL_INVALID_PLATFORM because no env vars are set, updates
  native_platform and then gets preempted again
- thread 2 wakes up and returns wrong _EGL_INVALID_PLATFORM

Solve this by doing the detection in a local var and only overwriting
the global one at the end, if no other thread has updated it since.

This means the platform detected in the thread might not be the platform
returned by the function, but this is a different issue that will need
to be discussed when this becomes possible.

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=101252
Signed-off-by: Eric Engestrom <eric@engestrom.ch>
Reviewed-by: Grazvydas Ignotas <notasas@gmail.com>
Acked-by: Emil Velikov <emil.l.velikov@gmail.com>
src/egl/main/egldisplay.c