diff options
| author | Brad Fitzpatrick <bradfitz@android.com> | 2011-03-17 15:41:20 -0700 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@android.com> | 2011-03-22 14:03:08 -0700 |
| commit | 13aa8ad44570bceef73115cea749b11f30888530 (patch) | |
| tree | 156695b75e5e3ff9f88f6a165520d1ff516678db /libsysutils | |
| parent | 15d09854a8b2a1adf2fa5123ff77b9882afd6c22 (diff) | |
| download | system_core-13aa8ad44570bceef73115cea749b11f30888530.zip system_core-13aa8ad44570bceef73115cea749b11f30888530.tar.gz system_core-13aa8ad44570bceef73115cea749b11f30888530.tar.bz2 | |
SocketClient: add optional reference counting
Needed to fix a race in netd.
Bug: 3438459
Change-Id: Icd7f5f035510235f733a25c0621479d3e644b152
Diffstat (limited to 'libsysutils')
| -rw-r--r-- | libsysutils/src/SocketClient.cpp | 21 | ||||
| -rw-r--r-- | libsysutils/src/SocketListener.cpp | 4 |
2 files changed, 23 insertions, 2 deletions
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp index a6aed26..6d4dff4 100644 --- a/libsysutils/src/SocketClient.cpp +++ b/libsysutils/src/SocketClient.cpp @@ -15,8 +15,10 @@ SocketClient::SocketClient(int socket) , mPid(-1) , mUid(-1) , mGid(-1) + , mRefCount(1) { pthread_mutex_init(&mWriteMutex, NULL); + pthread_mutex_init(&mRefCountMutex, NULL); struct ucred creds; socklen_t szCreds = sizeof(creds); @@ -100,3 +102,22 @@ int SocketClient::sendData(const void* data, int len) { pthread_mutex_unlock(&mWriteMutex); return 0; } + +void SocketClient::incRef() { + pthread_mutex_lock(&mRefCountMutex); + mRefCount++; + pthread_mutex_unlock(&mRefCountMutex); +} + +void SocketClient::decRef() { + bool deleteSelf = false; + pthread_mutex_lock(&mRefCountMutex); + mRefCount--; + if (mRefCount == 0) { + deleteSelf = true; + } + pthread_mutex_unlock(&mRefCountMutex); + if (deleteSelf) { + delete this; + } +} diff --git a/libsysutils/src/SocketListener.cpp b/libsysutils/src/SocketListener.cpp index 611d5fe..dde9b55 100644 --- a/libsysutils/src/SocketListener.cpp +++ b/libsysutils/src/SocketListener.cpp @@ -55,7 +55,7 @@ SocketListener::~SocketListener() { } SocketClientCollection::iterator it; for (it = mClients->begin(); it != mClients->end();) { - delete (*it); + (*it)->decRef(); it = mClients->erase(it); } delete mClients; @@ -226,7 +226,7 @@ void SocketListener::runListener() { pthread_mutex_unlock(&mClientsLock); /* Destroy the client */ close(c->getSocket()); - delete c; + c->decRef(); } } } |
