return TlsAlloc();
#else
LocalStorageKey key;
- pthread_key_create(&key, 0);
+ pthread_key_create(&key, NULL);
return key;
#endif
}
#if defined(_WIN32)
TlsFree(key);
#else
- pthread_key_delete(key);
+ pthread_key_delete(key); // Using an invalid key is an error but not undefined behavior.
#endif
}
#if defined(_WIN32)
TlsSetValue(key, value);
#else
- pthread_setspecific(key, value);
+ if(key != TLS_OUT_OF_INDEXES) // Avoid undefined behavior.
+ {
+ pthread_setspecific(key, value);
+ }
#endif
}
#if defined(_WIN32)
return TlsGetValue(key);
#else
+ if(key == TLS_OUT_OF_INDEXES) // Avoid undefined behavior.
+ {
+ return nullptr;
+ }
+
return pthread_getspecific(key);
#endif
}
namespace egl
{
-void attachThread()
+Current *attachThread()
{
TRACE("()");
- Current *current = new Current;
+ if(currentTLS == TLS_OUT_OF_INDEXES)
+ {
+ currentTLS = sw::Thread::allocateLocalStorageKey();
+ }
+
+ Current *current = (Current*)sw::Thread::getLocalStorage(currentTLS);
- if(current)
+ if(!current)
{
- sw::Thread::setLocalStorage(currentTLS, current);
+ current = new Current;
- current->error = EGL_SUCCESS;
- current->API = EGL_OPENGL_ES_API;
- current->context = nullptr;
- current->drawSurface = nullptr;
- current->readSurface = nullptr;
+ sw::Thread::setLocalStorage(currentTLS, current);
}
+
+ current->error = EGL_SUCCESS;
+ current->API = EGL_OPENGL_ES_API;
+ current->context = nullptr;
+ current->drawSurface = nullptr;
+ current->readSurface = nullptr;
+
+ return current;
}
void detachThread()
}
#endif
- currentTLS = sw::Thread::allocateLocalStorageKey();
-
- if(currentTLS == TLS_OUT_OF_INDEXES)
- {
- return;
- }
-
attachThread();
}
if(!current)
{
- attachThread();
+ current = attachThread();
}
- return (Current*)sw::Thread::getLocalStorage(currentTLS);
+ return current;
}
void setCurrentError(EGLint error)