• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
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
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 // -*- c++ -*-
19 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
20 
21 //                  OSCL_MUTEX (M U T E X  I M P L E M E N T A T I O N)
22 
23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
24 
25 
26 /*! \file oscl_mutex.cpp .This file provides MUTEX implementation that can be ported
27 to three OS LINUX, SYMBIAN & WIN32
28 */
29 
30 #include "oscl_mutex.h"
31 
32 
33 #include "oscl_assert.h"
34 
OsclMutex()35 OSCL_EXPORT_REF OsclMutex::OsclMutex()
36 {
37     bCreated = false;
38 }
39 
~OsclMutex()40 OSCL_EXPORT_REF OsclMutex::~OsclMutex()
41 {
42     //make sure it's closed
43     if (bCreated)
44         Close();
45 }
46 
47 /*
48  * Creates the Mutex
49  *
50  * @param MutexName    NULL terminated string.
51  *
52  * @return Returns the Error whether it is success or failure
53  *incase of failure it will return what is the specific error
54  */
Create()55 OSCL_EXPORT_REF OsclProcStatus::eOsclProcError OsclMutex::Create()
56 {
57     if (bCreated)
58         return OsclProcStatus::INVALID_OPERATION_ERROR;
59 
60 
61     int result = pthread_mutex_init(&ObjMutex, NULL);
62     if (result == 0)
63     {
64         bCreated = true;
65         return OsclProcStatus::SUCCESS_ERROR;
66     }
67     else
68         return ErrorMapping(result);
69 
70 
71 }
72 
73 
74 /**
75  * Locks the Mutex
76  *
77  * @param It wont take any parameters
78  *
79  * @return Returns the Error whether it is success or failure
80  *incase of failure it will return what is the specific error
81  */
Lock()82 OSCL_EXPORT_REF void OsclMutex::Lock()
83 {
84     //verify the mutex is created.
85     OSCL_ASSERT(bCreated);
86 
87 
88     pthread_mutex_lock(&ObjMutex);
89 
90 }
91 
92 
93 /**
94  * Try to lock the mutex,if the Mutex is already locked calling thread
95  * immediately returns with out blocking
96  * @param It wont take any parameters
97  *
98  * @return Returns the Error whether it is success or failure
99  *incase of failure it will return what is the specific error
100  */
TryLock()101 OSCL_EXPORT_REF OsclProcStatus::eOsclProcError OsclMutex::TryLock()
102 {
103     if (!bCreated)
104         return OsclProcStatus::INVALID_OPERATION_ERROR;
105 
106 
107     int result = pthread_mutex_trylock(&ObjMutex);
108     switch (result)
109     {
110         case 0:
111             return OsclProcStatus::SUCCESS_ERROR;
112         case EBUSY:
113             return OsclProcStatus::MUTEX_LOCKED_ERROR;
114         default:
115             return ErrorMapping(result);
116     }
117 
118 }
119 
120 /**
121  * Releases the Mutex
122  *
123  * @param It wont take any parameters
124  *
125  * @return Returns the Error whether it is success or failure
126  *incase of failure it will return what is the specific error
127  */
Unlock()128 OSCL_EXPORT_REF void OsclMutex::Unlock()
129 {
130     //verify the mutex is created.
131     OSCL_ASSERT(bCreated);
132 
133 
134     pthread_mutex_unlock(&ObjMutex);
135 
136 }
137 
138 
139 
140 /**
141  * Closes the Mutex
142  *
143  * @param It wont take any prameters
144  *
145  * @return Returns the Error whether it is success or failure
146  *incase of failure it will return what is the specific error
147  */
Close()148 OSCL_EXPORT_REF OsclProcStatus::eOsclProcError OsclMutex::Close()
149 {
150     if (!bCreated)
151         return OsclProcStatus::INVALID_OPERATION_ERROR;
152 
153 
154     int result = pthread_mutex_destroy(&ObjMutex);
155     if (result == 0)
156     {
157         bCreated = false;
158         return OsclProcStatus::SUCCESS_ERROR;
159     }
160     else
161         return ErrorMapping(result);
162 
163 }
164 
165 
166 
167 /**
168  * Error Mapping
169  *
170  * @param It will take error returned by OS specific API
171  *
172  * @return Returns specific error
173  */
ErrorMapping(int32 Error)174 OsclProcStatus::eOsclProcError OsclMutex::ErrorMapping(int32 Error)
175 {
176 
177 
178     switch (Error)
179     {
180         case 0:
181             return OsclProcStatus::SUCCESS_ERROR;
182         case EAGAIN :
183             return  OsclProcStatus::SYSTEM_RESOURCES_UNAVAILABLE_ERROR;
184         case EINVAL :
185             return  OsclProcStatus::INVALID_ARGUMENT_ERROR;
186         case ENOMEM :
187             return  OsclProcStatus::NOT_ENOUGH_MEMORY_ERROR;
188         case EFAULT  :
189             return  OsclProcStatus::INVALID_POINTER_ERROR;
190         case EBUSY:
191             return  OsclProcStatus::MUTEX_LOCKED_ERROR;
192         default:
193             return OsclProcStatus::OTHER_ERROR;
194     }
195 
196 }
197 
OsclThreadLock()198 OSCL_EXPORT_REF OsclThreadLock::OsclThreadLock()
199 {
200     iMutex.Create();
201 }
~OsclThreadLock()202 OSCL_EXPORT_REF OsclThreadLock::~OsclThreadLock()
203 {
204     iMutex.Close();
205 }
Lock()206 OSCL_EXPORT_REF void OsclThreadLock::Lock()
207 {
208     iMutex.Lock();
209 }
Unlock()210 OSCL_EXPORT_REF void OsclThreadLock::Unlock()
211 {
212     iMutex.Unlock();
213 }
214 
215