RDK Documentation (Open Sourced RDK Components)
Mutex.hpp
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 /*****************************************************************************/
21 /*!
22 \file
23 \brief This file defines interface of Mutex class.
24 
25 */
26 /*****************************************************************************/
27 
28 
29 /**
30 * @defgroup hdmicec
31 * @{
32 * @defgroup osal
33 * @{
34 **/
35 
36 
37 #ifndef HDMI_CCEC_OSAL_MUTEX_HPP_
38 #define HDMI_CCEC_OSAL_MUTEX_HPP_
39 
40 #include "OSAL.hpp"
41 
42 CCEC_OSAL_BEGIN_NAMESPACE
43 
44 /***************************************************************************/
45 /*!
46 
47 This class provides synchronization primitive for implementing critical sections
48 and protect shared data from concurrent access. This class provides functionaliy
49 of a recurisive (same thread can take the lock multiple times) mutual exclusive lock.
50 */
51 /**************************************************************************/
52 
53 class Mutex {
54 public:
55 /***************************************************************************/
56 /*!
57 \brief Constructor.
58 Creates Mutex object.
59 
60 */
61 /**************************************************************************/
62 
63  Mutex(void);
64  Mutex(const Mutex &);
65  Mutex & operator = (const Mutex &);
66 
67 /***************************************************************************/
68 /*!
69 \brief Destructor
70 Destroys the Mutex object.
71 
72 */
73 /**************************************************************************/
74 
75  ~Mutex(void);
76 
77 /***************************************************************************/
78 /*!
79 \brief Locks the given mutex
80 
81 Calling thread will acquire the mutex lock if available and will immediately
82 return. Subsequent call by other threads before current thread releasing the
83 mutex will result in those threads to block.
84 
85 \note Current thread can call lock() for same object multiple times before
86 releasing the mutex. But for releasing the lock to other threads, same number
87 of unlock calls shall be made by the locking thread.
88 
89 */
90 /**************************************************************************/
91 
92  void lock(void);
93 /***************************************************************************/
94 /*!
95 \brief Unlocks the given mutex
96 
97 Called by a thread which already acquired lock to release the lock acquired.
98 If there are other threads waiting on the lock, one of those waiting threads
99 will be able to acquire the lock released and will come out of wait state.
100 
101 */
102 /**************************************************************************/
103 
104 
105  void unlock(void);
106 
107 /***************************************************************************/
108 /*!
109 \brief Retrieves handle to the underlying native mutex implementation.
110 
111 
112 */
113 /**************************************************************************/
114 
115 
116  void *getNativeHandle(void);
117 private:
118  void *nativeHandle;
119 };
120 
121 class AutoLock
122 {
123 public:
124  AutoLock(Mutex & mutex) : mutex(mutex) {
125  mutex.lock();
126  }
127 
128  ~AutoLock(void) {
129  mutex.unlock();
130  }
131  Mutex &mutex;
132 };
133 
134 CCEC_OSAL_END_NAMESPACE
135 
136 #endif
137 
138 
139 /** @} */
140 /** @} */
CCEC_OSAL::AutoLock
Definition: Mutex.hpp:121
CCEC_OSAL::Mutex
Definition: Mutex.hpp:53