Remove lock around ref count check in terminate.

Replace the global lock in terminate for the ref count check with atomics and a
local lock.

Bug: 19072064

Change-Id: I0cfd6564e227a75b8387a8788b16381d5bc2cc88
This commit is contained in:
Michael Lentine 2015-01-27 09:01:03 -08:00
parent ef1c17ae94
commit 54466bc441
2 changed files with 162 additions and 135 deletions

View File

@ -57,7 +57,7 @@ static bool findExtension(const char* exts, const char* name, size_t nameLen) {
egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
egl_display_t::egl_display_t() :
magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0) {
magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
}
egl_display_t::~egl_display_t() {
@ -120,17 +120,25 @@ EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
Mutex::Autolock _l(lock);
{
Mutex::Autolock _rf(refLock);
if (refs > 0) {
refs++;
if (refs > 1) {
if (major != NULL)
*major = VERSION_MAJOR;
if (minor != NULL)
*minor = VERSION_MINOR;
refs++;
while(!eglIsInitialized) refCond.wait(refLock);
return EGL_TRUE;
}
while(eglIsInitialized) refCond.wait(refLock);
}
{
Mutex::Autolock _l(lock);
#if EGL_TRACE
// Called both at early_init time and at this time. (Early_init is pre-zygote, so
@ -214,21 +222,27 @@ EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
traceGpuCompletion = true;
}
refs++;
if (major != NULL)
*major = VERSION_MAJOR;
if (minor != NULL)
*minor = VERSION_MINOR;
mHibernation.setDisplayValid(true);
}
{
Mutex::Autolock _rf(refLock);
eglIsInitialized = true;
refCond.broadcast();
}
return EGL_TRUE;
}
EGLBoolean egl_display_t::terminate() {
Mutex::Autolock _l(lock);
{
Mutex::Autolock _rl(refLock);
if (refs == 0) {
/*
* From the EGL spec (3.2):
@ -240,12 +254,17 @@ EGLBoolean egl_display_t::terminate() {
}
// this is specific to Android, display termination is ref-counted.
if (refs > 1) {
refs--;
if (refs > 0) {
return EGL_TRUE;
}
}
EGLBoolean res = EGL_FALSE;
{
Mutex::Autolock _l(lock);
egl_connection_t* const cnx = &gEGLImpl;
if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
@ -275,8 +294,14 @@ EGLBoolean egl_display_t::terminate() {
// this marks all object handles are "terminated"
objects.clear();
}
{
Mutex::Autolock _rl(refLock);
eglIsInitialized = false;
refCond.broadcast();
}
refs--;
return res;
}

View File

@ -131,7 +131,9 @@ private:
void leave() { return mHibernation.decWakeCount(HibernationMachine::WEAK); }
uint32_t refs;
mutable Mutex lock;
bool eglIsInitialized;
mutable Mutex lock, refLock;
mutable Condition refCond;
SortedVector<egl_object_t*> objects;
String8 mVendorString;
String8 mVersionString;