AutoMutex _l(mLock)

来源:互联网 发布:文言文虚词乎 编辑:程序博客网 时间:2024/05/01 01:17

http://www.baidu.com/s?wd=android+AutoMutex&ie=UTF-8

http://blog.csdn.net/evilcode/article/details/8291934

http://blog.csdn.net/batoom/article/details/8314144

AutoMutex is defined in D:\linux\linuxkernel\src\cm10.1\frameworks\native\include\utils\Mutex.h

/* 
* Automatic mutex.  Declare one of these at the top of a function. 
* When the function returns, it will go out of scope, and release the 
* mutex. 
*/ 
typedef Mutex::Autolock AutoMutex;

// Manages the mutex automatically. It'll be locked when Autolock is 
// constructed and released when Autolock goes out of scope. 
class Autolock { 
public: 
    inline Autolock(Mutex& mutex) : mLock(mutex)  { mLock.lock(); } 
    inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); } 
    inline ~Autolock() { mLock.unlock(); } 
private: 
    Mutex& mLock; 
};

 

example:

D:\linux\linuxkernel\src\cm10.1\frameworks\av\include\media\IMediaDeathNotifier.h

class IMediaDeathNotifier: virtual public RefBase
{
    static  Mutex                                   sServiceLock;
D:\linux\linuxkernel\src\cm10.1\frameworks\av\media\libmedia\IMediaDeathNotifier.cpp

// client singleton for binder interface to services
Mutex IMediaDeathNotifier::sServiceLock;
IMediaDeathNotifier::getMediaPlayerService()
{
    ALOGV("getMediaPlayerService");
    Mutex::Autolock _l(sServiceLock);
.......

 

 

D:\linux\linuxkernel\src\cm10.1\frameworks\base\services\jni\com_android_server_input_InputManagerService.cpp
NativeInputManager::NativeInputManager(jobject contextObj,
        jobject serviceObj, const sp<Looper>& looper) :
        mLooper(looper) {
    JNIEnv* env = jniEnv();

    mContextObj = env->NewGlobalRef(contextObj);
    mServiceObj = env->NewGlobalRef(serviceObj);

    {
        AutoMutex _l(mLock);
        mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
        mLocked.pointerSpeed = 0;
        mLocked.pointerGesturesEnabled = true;
        mLocked.showTouches = false;
        mLocked.stylusIconEnabled = false;
    }

    sp<EventHub> eventHub = new EventHub();
    mInputManager = new InputManager(eventHub, this, this);
}


frameworks/base/libs/binder/IServiceManager.cpp

[cpp] view plaincopy

    sp<IServiceManager> defaultServiceManager()  
    {  
      
        if (gDefaultServiceManager != NULL) return gDefaultServiceManager;  
      
        {  
            AutoMutex _l(gDefaultServiceManagerLock);  
            if (gDefaultServiceManager == NULL) {  
                gDefaultServiceManager = interface_cast<IServiceManager>(  
                    ProcessState::self()->getContextObject(NULL));  
            }  
        }  
      
        return gDefaultServiceManager;  
    } 

原创粉丝点击