14035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
24035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin *
34035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * Redistribution and use in source and binary forms, with or without
44035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * modification, are permitted provided that the following conditions are
54035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * met:
64035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin *     * Redistributions of source code must retain the above copyright
74035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin *       notice, this list of conditions and the following disclaimer.
84035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin *     * Redistributions in binary form must reproduce the above
94035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin *       copyright notice, this list of conditions and the following
104035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin *       disclaimer in the documentation and/or other materials provided
114035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin *       with the distribution.
124035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin *     * Neither the name of The Linux Foundation, nor the names of its
134035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin *       contributors may be used to endorse or promote products derived
144035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin *       from this software without specific prior written permission.
154035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin *
164035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
174035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
184035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
194035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
204035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
214035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
224035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
234035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
244035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
254035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
264035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin *
284035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin */
294035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin#ifndef __LOC_SHARED_LOCK__
304035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin#define __LOC_SHARED_LOCK__
314035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin
324035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin#include <stddef.h>
3360c72c0a4cec04c4a85255c6965d6493337b3c80Kevin Tang#include <cutils/atomic.h>
344035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin#include <pthread.h>
354035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin
364035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin// This is a utility created for use cases such that there are more than
374035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin// one client who need to share the same lock, but it is not predictable
384035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin// which of these clients is to last to go away. This shared lock deletes
394035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin// itself when the last client calls its drop() method. To add a cient,
404035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin// this share lock's share() method has to be called, so that the obj
414035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin// can maintain an accurate client count.
424035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjinclass LocSharedLock {
4360c72c0a4cec04c4a85255c6965d6493337b3c80Kevin Tang    volatile int32_t mRef;
444035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin    pthread_mutex_t mMutex;
454035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin    inline ~LocSharedLock() { pthread_mutex_destroy(&mMutex); }
464035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjinpublic:
474035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin    // first client to create this LockSharedLock
484035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin    inline LocSharedLock() : mRef(1) { pthread_mutex_init(&mMutex, NULL); }
494035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin    // following client(s) are to *share()* this lock created by the first client
5060c72c0a4cec04c4a85255c6965d6493337b3c80Kevin Tang    inline LocSharedLock* share() { android_atomic_inc(&mRef); return this; }
514035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin    // whe a client no longer needs this shared lock, drop() shall be called.
5260c72c0a4cec04c4a85255c6965d6493337b3c80Kevin Tang    inline void drop() { if (1 == android_atomic_dec(&mRef)) delete this; }
534035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin    // locking the lock to enter critical section
544035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin    inline void lock() { pthread_mutex_lock(&mMutex); }
554035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin    // unlocking the lock to leave the critical section
564035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin    inline void unlock() { pthread_mutex_unlock(&mMutex); }
574035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin};
584035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin
594035be28a255eaa5605dbd9abeb2340db584249cPatrick Tjin#endif //__LOC_SHARED_LOCK__
60