aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSaurabh Bipin Chandra2013-09-03 04:50:45 -0500
committerSaurabh Bipin Chandra2013-09-05 10:47:20 -0500
commitabd5152ca071e552954571862635666297221177 (patch)
tree5344174df9cbb79fbc259911bdc2e48a3cf75976 /libdce.c
parent2dee585f2102f284d7babcfe5f0e126e32dde36d (diff)
downloadrepo-libdce-abd5152ca071e552954571862635666297221177.tar.gz
repo-libdce-abd5152ca071e552954571862635666297221177.tar.xz
repo-libdce-abd5152ca071e552954571862635666297221177.zip
[LIBDCE] Mutual exclusion of Engine_open and Engine_close
This patch uses semaphores to mutually exclude: 1. dce_init() and Engine_open() IPU call. This solves a hang on IPU side during multiple instances. pthread mutex used within dce_init is replaced with semaphores as mutex is valid only across threads and not processes. 2. dce_deinit() and Engine_close(). This solves a hang on IPU side during multiple instances. Change-Id: Ia4d31582ebf0536142dee389e9601eae0169fb22 Signed-off-by: Saurabh Bipin Chandra <a0131926@ti.com>
Diffstat (limited to 'libdce.c')
-rw-r--r--libdce.c30
1 files changed, 22 insertions, 8 deletions
diff --git a/libdce.c b/libdce.c
index cd10b71..f791e18 100644
--- a/libdce.c
+++ b/libdce.c
@@ -66,8 +66,10 @@
66#include <stdlib.h> 66#include <stdlib.h>
67#include <string.h> 67#include <string.h>
68#include <stdio.h> 68#include <stdio.h>
69#include <pthread.h>
70#include <errno.h> 69#include <errno.h>
70#include <fcntl.h> /* For O_* constants */
71#include <sys/stat.h> /* For mode constants */
72#include <semaphore.h>
71 73
72#include <xdc/std.h> 74#include <xdc/std.h>
73 75
@@ -84,7 +86,7 @@
84/********************* GLOBALS ***********************/ 86/********************* GLOBALS ***********************/
85/* Hande used for Remote Communication */ 87/* Hande used for Remote Communication */
86static MmRpc_Handle MmRpcHandle = NULL; 88static MmRpc_Handle MmRpcHandle = NULL;
87static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 89sem_t *dce_semaphore = NULL;
88static int count = 0; 90static int count = 0;
89 91
90 92
@@ -157,8 +159,6 @@ static int dce_ipc_init(void)
157 159
158 printf(" >> dce_ipc_init\n"); 160 printf(" >> dce_ipc_init\n");
159 161
160 pthread_mutex_lock(&mutex);
161
162 count++; 162 count++;
163 /* Check if already Initialized */ 163 /* Check if already Initialized */
164 _ASSERT(count == 1, DCE_EOK); 164 _ASSERT(count == 1, DCE_EOK);
@@ -173,7 +173,6 @@ static int dce_ipc_init(void)
173 printf("open(/dev/" DCE_DEVICE_NAME ") -> 0x%x\n", (int)MmRpcHandle); 173 printf("open(/dev/" DCE_DEVICE_NAME ") -> 0x%x\n", (int)MmRpcHandle);
174 174
175EXIT: 175EXIT:
176 pthread_mutex_unlock(&mutex);
177 return (eError); 176 return (eError);
178} 177}
179 178
@@ -183,8 +182,6 @@ EXIT:
183 */ 182 */
184static void dce_ipc_deinit() 183static void dce_ipc_deinit()
185{ 184{
186 pthread_mutex_lock(&mutex);
187
188 count--; 185 count--;
189 if( count > 0 ) { 186 if( count > 0 ) {
190 goto EXIT; 187 goto EXIT;
@@ -196,7 +193,6 @@ static void dce_ipc_deinit()
196 MmRpcHandle = NULL; 193 MmRpcHandle = NULL;
197 194
198EXIT: 195EXIT:
199 pthread_mutex_unlock(&mutex);
200 return; 196 return;
201} 197}
202 198
@@ -219,6 +215,12 @@ Engine_Handle Engine_open(String name, Engine_Attrs *attrs, Engine_Error *ec)
219 215
220 _ASSERT(name != '\0', DCE_EINVALID_INPUT); 216 _ASSERT(name != '\0', DCE_EINVALID_INPUT);
221 217
218 if( dce_semaphore == NULL ) {
219 _ASSERT((dce_semaphore = sem_open("/dce_semaphore", O_CREAT, S_IRWXU | S_IRWXO | S_IRWXG, 1)) != SEM_FAILED, DCE_ESEMAPHORE_FAIL);
220 }
221 /* Lock dce_ipc_init() and Engine_open() IPU call to prevent hang*/
222 _ASSERT(sem_wait(dce_semaphore) == DCE_EOK, DCE_ESEMAPHORE_FAIL);
223
222 /* Initialize IPC. In case of Error Deinitialize them */ 224 /* Initialize IPC. In case of Error Deinitialize them */
223 _ASSERT_AND_EXECUTE(dce_ipc_init() == DCE_EOK, DCE_EIPC_CREATE_FAIL, dce_ipc_deinit()); 225 _ASSERT_AND_EXECUTE(dce_ipc_init() == DCE_EOK, DCE_EIPC_CREATE_FAIL, dce_ipc_deinit());
224 226
@@ -252,7 +254,11 @@ Engine_Handle Engine_open(String name, Engine_Attrs *attrs, Engine_Error *ec)
252 if( ec ) { 254 if( ec ) {
253 *ec = engine_open_msg->error_code; 255 *ec = engine_open_msg->error_code;
254 } 256 }
257
255EXIT: 258EXIT:
259 /* Unlock dce_ipc_init() and Engine_open() IPU call */
260 _ASSERT(sem_post(dce_semaphore) == DCE_EOK, DCE_ESEMAPHORE_FAIL);
261
256 memplugin_free(engine_open_msg, TILER_1D_BUFFER); 262 memplugin_free(engine_open_msg, TILER_1D_BUFFER);
257 if( engine_attrs ) { 263 if( engine_attrs ) {
258 memplugin_free(engine_attrs, TILER_1D_BUFFER); 264 memplugin_free(engine_attrs, TILER_1D_BUFFER);
@@ -273,6 +279,9 @@ Void Engine_close(Engine_Handle engine)
273 279
274 _ASSERT(engine != NULL, DCE_EINVALID_INPUT); 280 _ASSERT(engine != NULL, DCE_EINVALID_INPUT);
275 281
282 /* Lock dce_ipc_deinit() and Engine_close() IPU call */
283 _ASSERT(sem_wait(dce_semaphore) == DCE_EOK, DCE_ESEMAPHORE_FAIL);
284
276 /* Marshall function arguments into the send buffer */ 285 /* Marshall function arguments into the send buffer */
277 Fill_MmRpc_fxnCtx(&fxnCtx, DCE_RPC_ENGINE_CLOSE, 1, 0, NULL); 286 Fill_MmRpc_fxnCtx(&fxnCtx, DCE_RPC_ENGINE_CLOSE, 1, 0, NULL);
278 Fill_MmRpc_fxnCtx_Scalar_Params(fxnCtx.params, sizeof(Engine_Handle), (int32_t)engine); 287 Fill_MmRpc_fxnCtx_Scalar_Params(fxnCtx.params, sizeof(Engine_Handle), (int32_t)engine);
@@ -284,6 +293,11 @@ Void Engine_close(Engine_Handle engine)
284 293
285EXIT: 294EXIT:
286 dce_ipc_deinit(); 295 dce_ipc_deinit();
296
297 /* Unlock dce_ipc_deinit() and Engine_close() IPU call */
298 _ASSERT(sem_post(dce_semaphore) == DCE_EOK, DCE_ESEMAPHORE_FAIL);
299 sem_close(dce_semaphore);
300
287 return; 301 return;
288} 302}
289 303