1e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti/* Copyright (c) 2015, The Linux Foundation. All rights reserved. 2e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * 3e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * Redistribution and use in source and binary forms, with or without 4e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * modification, are permitted provided that the following conditions are 5e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * met: 6e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * * Redistributions of source code must retain the above copyright 7e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * notice, this list of conditions and the following disclaimer. 8e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * * Redistributions in binary form must reproduce the above 9e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * copyright notice, this list of conditions and the following 10e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * disclaimer in the documentation and/or other materials provided 11e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * with the distribution. 12e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * * Neither the name of The Linux Foundation, nor the names of its 13e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * contributors may be used to endorse or promote products derived 14e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * from this software without specific prior written permission. 15e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * 16e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 17e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 19e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 20e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 26e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti * 28e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti */ 29e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti#ifndef __LOC_SHARED_LOCK__ 30e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti#define __LOC_SHARED_LOCK__ 31e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti 32e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti#include <stddef.h> 3337d684cf8b2fb9144b27413183f310ac5dd408beKevin Tang#include <cutils/atomic.h> 34e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti#include <pthread.h> 35e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti 36e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti// This is a utility created for use cases such that there are more than 37e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti// one client who need to share the same lock, but it is not predictable 38e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti// which of these clients is to last to go away. This shared lock deletes 39e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti// itself when the last client calls its drop() method. To add a cient, 40e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti// this share lock's share() method has to be called, so that the obj 41e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti// can maintain an accurate client count. 42e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleticlass LocSharedLock { 4337d684cf8b2fb9144b27413183f310ac5dd408beKevin Tang volatile int32_t mRef; 44e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti pthread_mutex_t mMutex; 45e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti inline ~LocSharedLock() { pthread_mutex_destroy(&mMutex); } 46e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuletipublic: 47e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti // first client to create this LockSharedLock 48e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti inline LocSharedLock() : mRef(1) { pthread_mutex_init(&mMutex, NULL); } 49e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti // following client(s) are to *share()* this lock created by the first client 5037d684cf8b2fb9144b27413183f310ac5dd408beKevin Tang inline LocSharedLock* share() { android_atomic_inc(&mRef); return this; } 51e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti // whe a client no longer needs this shared lock, drop() shall be called. 5237d684cf8b2fb9144b27413183f310ac5dd408beKevin Tang inline void drop() { if (1 == android_atomic_dec(&mRef)) delete this; } 53e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti // locking the lock to enter critical section 54e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti inline void lock() { pthread_mutex_lock(&mMutex); } 55e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti // unlocking the lock to leave the critical section 56e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti inline void unlock() { pthread_mutex_unlock(&mMutex); } 57e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti}; 58e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti 59e7c98642e1e156ea6cde1238cd0006f669cfb696Uday Kishore Pasupuleti#endif //__LOC_SHARED_LOCK__ 60