1129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel/* Copyright (c) 2015, The Linux Foundation. All rights reserved. 2129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * 3129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * Redistribution and use in source and binary forms, with or without 4129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * modification, are permitted provided that the following conditions are 5129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * met: 6129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * * Redistributions of source code must retain the above copyright 7129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * notice, this list of conditions and the following disclaimer. 8129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * * Redistributions in binary form must reproduce the above 9129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * copyright notice, this list of conditions and the following 10129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * disclaimer in the documentation and/or other materials provided 11129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * with the distribution. 12129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * * Neither the name of The Linux Foundation, nor the names of its 13129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * contributors may be used to endorse or promote products derived 14129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * from this software without specific prior written permission. 15129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * 16129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 17129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 19129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 20129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 26129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel * 28129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel */ 29129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel#ifndef __LOC_SHARED_LOCK__ 30129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel#define __LOC_SHARED_LOCK__ 31129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel 32129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel#include <stddef.h> 33129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel#include <cutils/atomic.h> 34129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel#include <pthread.h> 35129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel 36129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel// This is a utility created for use cases such that there are more than 37129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel// one client who need to share the same lock, but it is not predictable 38129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel// which of these clients is to last to go away. This shared lock deletes 39129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel// itself when the last client calls its drop() method. To add a cient, 40129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel// this share lock's share() method has to be called, so that the obj 41129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel// can maintain an accurate client count. 42129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudelclass LocSharedLock { 43129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel volatile int32_t mRef; 44129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel pthread_mutex_t mMutex; 45129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel inline ~LocSharedLock() { pthread_mutex_destroy(&mMutex); } 46129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudelpublic: 47129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel // first client to create this LockSharedLock 48129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel inline LocSharedLock() : mRef(1) { pthread_mutex_init(&mMutex, NULL); } 49129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel // following client(s) are to *share()* this lock created by the first client 50129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel inline LocSharedLock* share() { android_atomic_inc(&mRef); return this; } 51129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel // whe a client no longer needs this shared lock, drop() shall be called. 52129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel inline void drop() { if (1 == android_atomic_dec(&mRef)) delete this; } 53129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel // locking the lock to enter critical section 54129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel inline void lock() { pthread_mutex_lock(&mMutex); } 55129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel // unlocking the lock to leave the critical section 56129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel inline void unlock() { pthread_mutex_unlock(&mMutex); } 57129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel}; 58129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel 59129edaf7d0025e2828a8bee025f7b1bac7a68da6Thierry Strudel#endif //__LOC_SHARED_LOCK__ 60