1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 *  Encapsulate a mutex for thread synchronization.
19 */
20
21#pragma once
22#include <pthread.h>
23
24
25class Mutex
26{
27public:
28    /*******************************************************************************
29    **
30    ** Function:        Mutex
31    **
32    ** Description:     Initialize member variables.
33    **
34    ** Returns:         None.
35    **
36    *******************************************************************************/
37    Mutex ();
38
39
40    /*******************************************************************************
41    **
42    ** Function:        ~Mutex
43    **
44    ** Description:     Cleanup all resources.
45    **
46    ** Returns:         None.
47    **
48    *******************************************************************************/
49    ~Mutex ();
50
51
52    /*******************************************************************************
53    **
54    ** Function:        lock
55    **
56    ** Description:     Block the thread and try lock the mutex.
57    **
58    ** Returns:         None.
59    **
60    *******************************************************************************/
61    void lock ();
62
63
64    /*******************************************************************************
65    **
66    ** Function:        unlock
67    **
68    ** Description:     Unlock a mutex to unblock a thread.
69    **
70    ** Returns:         None.
71    **
72    *******************************************************************************/
73    void unlock ();
74
75
76    /*******************************************************************************
77    **
78    ** Function:        tryLock
79    **
80    ** Description:     Try to lock the mutex.
81    **
82    ** Returns:         True if the mutex is locked.
83    **
84    *******************************************************************************/
85    bool tryLock ();
86
87
88    /*******************************************************************************
89    **
90    ** Function:        nativeHandle
91    **
92    ** Description:     Get the handle of the mutex.
93    **
94    ** Returns:         Handle of the mutex.
95    **
96    *******************************************************************************/
97    pthread_mutex_t* nativeHandle ();
98
99    class Autolock {
100        public:
101            inline Autolock(Mutex& mutex) : mLock(mutex)  { mLock.lock(); }
102            inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); }
103            inline ~Autolock() { mLock.unlock(); }
104        private:
105            Mutex& mLock;
106    };
107
108
109private:
110    pthread_mutex_t mMutex;
111};
112
113typedef Mutex::Autolock AutoMutex;
114