diff options
author | Sunita Nadampalli | 2013-09-26 16:26:59 -0500 |
---|---|---|
committer | Sunita Nadampalli | 2013-09-26 16:26:59 -0500 |
commit | ffb5c1de22eefd33ec0694c5590eb6fb50bd1771 (patch) | |
tree | 969befa5ddf3228a430d161e704292c32eba3923 | |
parent | 4f74566a123093d4454c1d969ce340b33552e78b (diff) | |
download | repo-libdce-ffb5c1de22eefd33ec0694c5590eb6fb50bd1771.tar.gz repo-libdce-ffb5c1de22eefd33ec0694c5590eb6fb50bd1771.tar.xz repo-libdce-ffb5c1de22eefd33ec0694c5590eb6fb50bd1771.zip |
[LIBDCE] Replace Semaphore with Mutex
This patch reverts the below commit:
"abd5152ca071e552954571862635666297221177
[LIBDCE] Mutual exclusion of Engine_open and Engine_close"
Description:
-----------
(1) The current semaphore logic doesn't protect the engine open/close
calls across processes. It is confirmed on QNX that two instances of
dce test app is creating two different instances of semaphore.
Hence, other than the delay the current semaphore doesn't add
any protection for the mutli process <-> multi instance scenarios.
(2) For the single process <-> multi instance scenario, it does protect
the engine open/close calls, but it fails due to lack of reference counting
And also, it is not sure why we need to protect the entire engine_open/close.
Hence the current semaphore is replaced with mutex to protect the ipc handle
create/close.
Change-Id: Ibdad089bdd39cd5afc813373e84bf91fa8920860
Signed-off-by: Sunita Nadampalli <sunitan@ti.com>
-rw-r--r-- | libdce.c | 91 | ||||
-rw-r--r-- | libdce.h | 3 |
2 files changed, 20 insertions, 74 deletions
@@ -33,10 +33,8 @@ | |||
33 | #include <stdlib.h> | 33 | #include <stdlib.h> |
34 | #include <string.h> | 34 | #include <string.h> |
35 | #include <stdio.h> | 35 | #include <stdio.h> |
36 | #include <pthread.h> | ||
36 | #include <errno.h> | 37 | #include <errno.h> |
37 | #include <fcntl.h> /* For O_* constants */ | ||
38 | #include <sys/stat.h> /* For mode constants */ | ||
39 | #include <semaphore.h> | ||
40 | 38 | ||
41 | /* IPC Headers */ | 39 | /* IPC Headers */ |
42 | #include <MmRpc.h> | 40 | #include <MmRpc.h> |
@@ -51,8 +49,8 @@ | |||
51 | /********************* GLOBALS ***********************/ | 49 | /********************* GLOBALS ***********************/ |
52 | /* Hande used for Remote Communication */ | 50 | /* Hande used for Remote Communication */ |
53 | MmRpc_Handle MmRpcHandle = NULL; | 51 | MmRpc_Handle MmRpcHandle = NULL; |
54 | sem_t *dce_semaphore = NULL; | 52 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
55 | static int count = 0; | 53 | static int __ClientCount = 0; |
56 | int dce_debug = DCE_DEBUG_LEVEL; | 54 | int dce_debug = DCE_DEBUG_LEVEL; |
57 | 55 | ||
58 | /****************** INLINE FUNCTIONS ********************/ | 56 | /****************** INLINE FUNCTIONS ********************/ |
@@ -112,55 +110,6 @@ void dce_free(void *ptr) | |||
112 | memplugin_free(ptr); | 110 | memplugin_free(ptr); |
113 | } | 111 | } |
114 | 112 | ||
115 | static inline int dce_sem_init(sem_t * *sem) | ||
116 | { | ||
117 | if( *sem ) { | ||
118 | return (DCE_EOK); | ||
119 | } | ||
120 | |||
121 | #ifdef BUILDOS_ANDROID | ||
122 | *sem = malloc(sizeof(sem_t)); | ||
123 | int ret = sem_init(*sem, 0, 1); | ||
124 | return (ret); | ||
125 | #else | ||
126 | *sem = sem_open("/dce_semaphore", O_CREAT, S_IRWXU | S_IRWXO | S_IRWXG, 1); | ||
127 | return ((*sem) ? DCE_EOK : SEM_FAILED); | ||
128 | #endif | ||
129 | } | ||
130 | |||
131 | static inline int dce_sem_wait(sem_t *sem) | ||
132 | { | ||
133 | if( NULL == sem ) { | ||
134 | return (DCE_EINVALID_INPUT); | ||
135 | } | ||
136 | |||
137 | return (sem_wait(sem)); | ||
138 | } | ||
139 | |||
140 | static inline int dce_sem_post(sem_t *sem) | ||
141 | { | ||
142 | if( NULL == sem ) { | ||
143 | return (DCE_EINVALID_INPUT); | ||
144 | } | ||
145 | |||
146 | return (sem_post(sem)); | ||
147 | } | ||
148 | |||
149 | static inline void dce_sem_close(sem_t * *sem) | ||
150 | { | ||
151 | if( NULL == *sem ) { | ||
152 | return; | ||
153 | } | ||
154 | |||
155 | #ifdef BUILDOS_ANDROID | ||
156 | sem_destroy(*sem); | ||
157 | free(*sem); | ||
158 | #else | ||
159 | sem_close(*sem); | ||
160 | #endif | ||
161 | *sem = NULL; | ||
162 | } | ||
163 | |||
164 | /*=====================================================================================*/ | 113 | /*=====================================================================================*/ |
165 | /** dce_ipc_init : Initialize MmRpc. This function is called within Engine_open(). | 114 | /** dce_ipc_init : Initialize MmRpc. This function is called within Engine_open(). |
166 | * | 115 | * |
@@ -173,16 +122,18 @@ static int dce_ipc_init(void) | |||
173 | 122 | ||
174 | DEBUG(" >> dce_ipc_init\n"); | 123 | DEBUG(" >> dce_ipc_init\n"); |
175 | 124 | ||
176 | count++; | 125 | __ClientCount++; |
177 | /* Check if already Initialized */ | 126 | /* Check if already Initialized */ |
178 | _ASSERT(count == 1, DCE_EOK); | 127 | if (__ClientCount > 1) { |
128 | goto EXIT; | ||
129 | } | ||
179 | 130 | ||
180 | /* Create remote server insance */ | 131 | /* Create remote server insance */ |
181 | MmRpc_Params_init(&args); | 132 | MmRpc_Params_init(&args); |
182 | 133 | ||
183 | eError = MmRpc_create(DCE_DEVICE_NAME, &args, &MmRpcHandle); | 134 | eError = MmRpc_create(DCE_DEVICE_NAME, &args, &MmRpcHandle); |
184 | 135 | ||
185 | _ASSERT_AND_EXECUTE(eError == DCE_EOK, DCE_EIPC_CREATE_FAIL, count--); | 136 | _ASSERT_AND_EXECUTE(eError == DCE_EOK, DCE_EIPC_CREATE_FAIL, __ClientCount--); |
186 | 137 | ||
187 | DEBUG("open(/dev/" DCE_DEVICE_NAME ") -> 0x%x\n", (int)MmRpcHandle); | 138 | DEBUG("open(/dev/" DCE_DEVICE_NAME ") -> 0x%x\n", (int)MmRpcHandle); |
188 | 139 | ||
@@ -196,8 +147,8 @@ EXIT: | |||
196 | */ | 147 | */ |
197 | static void dce_ipc_deinit() | 148 | static void dce_ipc_deinit() |
198 | { | 149 | { |
199 | count--; | 150 | __ClientCount--; |
200 | if( count > 0 ) { | 151 | if( __ClientCount > 0 ) { |
201 | goto EXIT; | 152 | goto EXIT; |
202 | } | 153 | } |
203 | 154 | ||
@@ -229,12 +180,10 @@ Engine_Handle Engine_open(String name, Engine_Attrs *attrs, Engine_Error *ec) | |||
229 | 180 | ||
230 | _ASSERT(name != '\0', DCE_EINVALID_INPUT); | 181 | _ASSERT(name != '\0', DCE_EINVALID_INPUT); |
231 | 182 | ||
232 | _ASSERT(dce_sem_init(&dce_semaphore) == DCE_EOK, DCE_ESEMAPHORE_FAIL); | 183 | pthread_mutex_lock(&mutex); |
233 | /* Lock dce_ipc_init() and Engine_open() IPU call to prevent hang*/ | ||
234 | _ASSERT(dce_sem_wait(dce_semaphore) == DCE_EOK, DCE_ESEMAPHORE_FAIL); | ||
235 | 184 | ||
236 | /* Initialize IPC. In case of Error Deinitialize them */ | 185 | /* Initialize IPC. In case of Error Deinitialize them */ |
237 | _ASSERT_AND_EXECUTE(dce_ipc_init() == DCE_EOK, DCE_EIPC_CREATE_FAIL, dce_sem_close(&dce_semaphore)); | 186 | _ASSERT(dce_ipc_init() == DCE_EOK, DCE_EIPC_CREATE_FAIL); |
238 | 187 | ||
239 | INFO(">> Engine_open Params::name = %s size = %d\n", name, strlen(name)); | 188 | INFO(">> Engine_open Params::name = %s size = %d\n", name, strlen(name)); |
240 | /* Allocate Shared memory for the engine_open rpc msg structure*/ | 189 | /* Allocate Shared memory for the engine_open rpc msg structure*/ |
@@ -266,15 +215,15 @@ Engine_Handle Engine_open(String name, Engine_Attrs *attrs, Engine_Error *ec) | |||
266 | if( ec ) { | 215 | if( ec ) { |
267 | *ec = engine_open_msg->error_code; | 216 | *ec = engine_open_msg->error_code; |
268 | } | 217 | } |
269 | |||
270 | EXIT: | 218 | EXIT: |
271 | /* Unlock dce_ipc_init() and Engine_open() IPU call */ | ||
272 | _ASSERT(dce_sem_post(dce_semaphore) == DCE_EOK, DCE_ESEMAPHORE_FAIL); | ||
273 | 219 | ||
274 | memplugin_free(engine_open_msg); | 220 | memplugin_free(engine_open_msg); |
221 | |||
275 | if( engine_attrs ) { | 222 | if( engine_attrs ) { |
276 | memplugin_free(engine_attrs); | 223 | memplugin_free(engine_attrs); |
277 | } | 224 | } |
225 | pthread_mutex_unlock(&mutex); | ||
226 | |||
278 | return ((Engine_Handle)engine_handle); | 227 | return ((Engine_Handle)engine_handle); |
279 | } | 228 | } |
280 | 229 | ||
@@ -289,10 +238,9 @@ Void Engine_close(Engine_Handle engine) | |||
289 | int32_t fxnRet; | 238 | int32_t fxnRet; |
290 | dce_error_status eError = DCE_EOK; | 239 | dce_error_status eError = DCE_EOK; |
291 | 240 | ||
292 | _ASSERT(engine != NULL, DCE_EINVALID_INPUT); | 241 | pthread_mutex_lock(&mutex); |
293 | 242 | ||
294 | /* Lock dce_ipc_deinit() and Engine_close() IPU call */ | 243 | _ASSERT(engine != NULL, DCE_EINVALID_INPUT); |
295 | _ASSERT(dce_sem_wait(dce_semaphore) == DCE_EOK, DCE_ESEMAPHORE_FAIL); | ||
296 | 244 | ||
297 | /* Marshall function arguments into the send buffer */ | 245 | /* Marshall function arguments into the send buffer */ |
298 | Fill_MmRpc_fxnCtx(&fxnCtx, DCE_RPC_ENGINE_CLOSE, 1, 0, NULL); | 246 | Fill_MmRpc_fxnCtx(&fxnCtx, DCE_RPC_ENGINE_CLOSE, 1, 0, NULL); |
@@ -306,9 +254,8 @@ Void Engine_close(Engine_Handle engine) | |||
306 | EXIT: | 254 | EXIT: |
307 | dce_ipc_deinit(); | 255 | dce_ipc_deinit(); |
308 | 256 | ||
309 | /* Unlock dce_ipc_deinit() and Engine_close() IPU call */ | 257 | pthread_mutex_unlock(&mutex); |
310 | _ASSERT(dce_sem_post(dce_semaphore) == DCE_EOK, DCE_ESEMAPHORE_FAIL); | 258 | |
311 | dce_sem_close(&dce_semaphore); | ||
312 | return; | 259 | return; |
313 | } | 260 | } |
314 | 261 | ||
@@ -54,8 +54,7 @@ typedef enum dce_error_status { | |||
54 | DCE_EIPC_CREATE_FAIL = -4, | 54 | DCE_EIPC_CREATE_FAIL = -4, |
55 | DCE_EIPC_CALL_FAIL = -5, | 55 | DCE_EIPC_CALL_FAIL = -5, |
56 | DCE_EINVALID_INPUT = -6, | 56 | DCE_EINVALID_INPUT = -6, |
57 | DCE_EOMAPDRM_FAIL = -7, | 57 | DCE_EOMAPDRM_FAIL = -7 |
58 | DCE_ESEMAPHORE_FAIL = -8 | ||
59 | } dce_error_status; | 58 | } dce_error_status; |
60 | 59 | ||
61 | /***************************** Memory Allocation/Free APIs *****************************/ | 60 | /***************************** Memory Allocation/Free APIs *****************************/ |