RDK Documentation (Open Sourced RDK Components)
Mutex.cpp
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 /**
23 * @defgroup hdmicec
24 * @{
25 * @defgroup osal
26 * @{
27 **/
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <pthread.h>
32 #include <string.h>
33 #include "safec_lib.h"
34 #include "osal/Mutex.hpp"
35 #include "osal/Util.hpp"
36 
37 CCEC_OSAL_BEGIN_NAMESPACE
38 
39 Mutex::Mutex(void) : nativeHandle(NULL)
40 {
41  pthread_mutexattr_t attr;
42  nativeHandle = Malloc(sizeof(pthread_mutex_t));
43  pthread_mutexattr_init( &attr);
44  pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE_NP);
45  pthread_mutex_init( (pthread_mutex_t *) nativeHandle, &attr );
46  pthread_mutexattr_destroy(&attr);
47 }
48 
49 Mutex::Mutex(const Mutex &rhs)
50 {
51  nativeHandle = Malloc(sizeof(pthread_mutex_t));
52  MEMCPY_S(nativeHandle,sizeof(pthread_mutex_t), rhs.nativeHandle, sizeof(pthread_mutex_t));
53 }
54 
55 Mutex & Mutex::operator = (const Mutex &rhs)
56 {
57  Mutex temp(rhs);
58  void *tempHandle = temp.nativeHandle;
59  temp.nativeHandle = nativeHandle;
60  nativeHandle = tempHandle;
61 
62  return *this;
63 }
64 
66 {
67  if (nativeHandle != NULL) {
68  pthread_mutex_destroy((pthread_mutex_t *) nativeHandle);
69  Free(nativeHandle);
70  }
71 }
72 
73 void Mutex::lock(void)
74 {
75  pthread_mutex_lock((pthread_mutex_t *)nativeHandle);
76 }
77 
78 void Mutex::unlock(void)
79 {
80  pthread_mutex_unlock((pthread_mutex_t *)nativeHandle);
81 }
82 
84 {
85  return nativeHandle;
86 }
87 
88 CCEC_OSAL_END_NAMESPACE
89 
90 
91 /** @} */
92 /** @} */
CCEC_OSAL::Mutex::lock
void lock(void)
Locks the given mutex.
Definition: Mutex.cpp:73
CCEC_OSAL::Mutex
Definition: Mutex.hpp:53
CCEC_OSAL::Mutex::~Mutex
~Mutex(void)
Destructor Destroys the Mutex object.
Definition: Mutex.cpp:65
CCEC_OSAL::Mutex::getNativeHandle
void * getNativeHandle(void)
Retrieves handle to the underlying native mutex implementation.
Definition: Mutex.cpp:83
CCEC_OSAL::Mutex::unlock
void unlock(void)
Unlocks the given mutex.
Definition: Mutex.cpp:78
Mutex.hpp
This file defines interface of Mutex class.
CCEC_OSAL::Mutex::Mutex
Mutex(void)
Constructor. Creates Mutex object.
Definition: Mutex.cpp:39