Skip to content

Commit 2ea0cbb

Browse files
author
Jamie Gennis
committed
EGL: add deferred saving of the cache
This change causes any insertions into the EGL cache to trigger an attempt to save the cache contents to disk. The save operation is deferred to allow multiple cache insertions to be batched up. Change-Id: I6cfec9c0dbbef94d3f8880860e2a365dccc296c7
1 parent d211b48 commit 2ea0cbb

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

opengl/libs/EGL/egl_cache.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ static const size_t maxTotalSize = 64 * 1024;
3434
static const char* cacheFileMagic = "EGL$";
3535
static const size_t cacheFileHeaderSize = 8;
3636

37+
// The time in seconds to wait before saving newly inserted cache entries.
38+
static const unsigned int deferredSaveDelay = 4;
39+
3740
// ----------------------------------------------------------------------------
3841
namespace android {
3942
// ----------------------------------------------------------------------------
@@ -128,6 +131,30 @@ void egl_cache_t::setBlob(const void* key, EGLsizei keySize, const void* value,
128131
if (mInitialized) {
129132
sp<BlobCache> bc = getBlobCacheLocked();
130133
bc->set(key, keySize, value, valueSize);
134+
135+
if (!mSavePending) {
136+
class DeferredSaveThread : public Thread {
137+
public:
138+
DeferredSaveThread() : Thread(false) {}
139+
140+
virtual bool threadLoop() {
141+
sleep(deferredSaveDelay);
142+
egl_cache_t* c = egl_cache_t::get();
143+
Mutex::Autolock lock(c->mMutex);
144+
if (c->mInitialized) {
145+
c->saveBlobCacheLocked();
146+
}
147+
c->mSavePending = false;
148+
return false;
149+
}
150+
};
151+
152+
// The thread will hold a strong ref to itself until it has finished
153+
// running, so there's no need to keep a ref around.
154+
sp<Thread> deferredSaveThread(new DeferredSaveThread());
155+
mSavePending = true;
156+
deferredSaveThread->run();
157+
}
131158
}
132159
}
133160

opengl/libs/EGL/egl_cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ class EGLAPI egl_cache_t {
108108
// from disk.
109109
String8 mFilename;
110110

111+
// mSavePending indicates whether or not a deferred save operation is
112+
// pending. Each time a key/value pair is inserted into the cache via
113+
// setBlob, a deferred save is initiated if one is not already pending.
114+
// This will wait some amount of time and then trigger a save of the cache
115+
// contents to disk.
116+
bool mSavePending;
117+
111118
// mMutex is the mutex used to prevent concurrent access to the member
112119
// variables. It must be locked whenever the member variables are accessed.
113120
mutable Mutex mMutex;

0 commit comments

Comments
 (0)