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
28class Mutex {
29 public:
30  /*******************************************************************************
31  **
32  ** Function:        Mutex
33  **
34  ** Description:     Initialize member variables.
35  **
36  ** Returns:         None.
37  **
38  *******************************************************************************/
39  Mutex();
40
41  /*******************************************************************************
42  **
43  ** Function:        ~Mutex
44  **
45  ** Description:     Cleanup all resources.
46  **
47  ** Returns:         None.
48  **
49  *******************************************************************************/
50  ~Mutex();
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  ** Function:        unlock
66  **
67  ** Description:     Unlock a mutex to unblock a thread.
68  **
69  ** Returns:         None.
70  **
71  *******************************************************************************/
72  void unlock();
73
74  /*******************************************************************************
75  **
76  ** Function:        tryLock
77  **
78  ** Description:     Try to lock the mutex.
79  **
80  ** Returns:         True if the mutex is locked.
81  **
82  *******************************************************************************/
83  bool tryLock();
84
85  /*******************************************************************************
86  **
87  ** Function:        nativeHandle
88  **
89  ** Description:     Get the handle of the mutex.
90  **
91  ** Returns:         Handle of the mutex.
92  **
93  *******************************************************************************/
94  pthread_mutex_t* nativeHandle();
95
96 private:
97  pthread_mutex_t mMutex;
98};
99