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