From 3fbf857a07b630c7881bacdb4ec2b853bbbbaf37 Mon Sep 17 00:00:00 2001 From: Zhiliang Wang Date: Wed, 5 Jan 2022 11:14:29 +0800 Subject: [PATCH] AMP: event: add mutex for rwlock to avoid it dead lock currently there is bug in pthread lib, dead lock will easily occur when multiple threads call pthread_rwlock apis, add mutext as a workaround to avoid it Change-Id: Ie0ecf17cb27d00c5c9119ab9fdd52cc3319a8543 --- amp/src/libs/server/amp_event_service.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/amp/src/libs/server/amp_event_service.c b/amp/src/libs/server/amp_event_service.c index 623866b..30644dd 100755 --- a/amp/src/libs/server/amp_event_service.c +++ b/amp/src/libs/server/amp_event_service.c @@ -97,6 +97,7 @@ typedef struct { #ifdef AMP_LATENCY_EVENT_SRV_PROF AMP_TOP stEventCBStats; #endif + MV_OSAL_HANDLE_MUTEX_t hMutex; } EVENT_LISTEN_SERVICE; static EVENT_LISTEN_SERVICE *m_event_service; @@ -292,10 +293,12 @@ HRESULT ISL_Listener_Create(UINT uiSrvIdN, UINT uiSrvIdC, HANDLE *phListener) EVENT_LISTEN_SERVICE *pEventService = m_event_service; EVENT_LISTEN_CLIENT *pClientListener = NULL; + MV_OSAL_Mutex_Lock(pEventService->hMutex); MV_OSAL_RWLock_WRLock(pEventService->hEventLock); pClientListener = alloc_event_listener(pEventService); if (!pClientListener) { MV_OSAL_RWLock_Unlock(pEventService->hEventLock); + MV_OSAL_Mutex_Unlock(pEventService->hMutex); AMPLOGE("Allocate listen fail!(%x %x)", uiSrvIdN, uiSrvIdC); return HRESULT_GEN(EVENTLI, ERR_NOSWRSC); } @@ -305,11 +308,13 @@ HRESULT ISL_Listener_Create(UINT uiSrvIdN, UINT uiSrvIdC, HANDLE *phListener) if (rc != SUCCESS) { pClientListener->bAllocated = 0; MV_OSAL_RWLock_Unlock(pEventService->hEventLock); + MV_OSAL_Mutex_Unlock(pEventService->hMutex); AMPLOGE("%p new eventbuf (%x %x) rc:%x", pClientListener, uiSrvIdN, uiSrvIdC, rc); return rc; } MV_OSAL_RWLock_Unlock(pEventService->hEventLock); + MV_OSAL_Mutex_Unlock(pEventService->hMutex); AMPLOGD("%p new eventsrv (%x %x) rc:%x", pClientListener, uiSrvIdN, uiSrvIdC, rc); *phListener = (HANDLE) pClientListener; @@ -333,6 +338,7 @@ HRESULT ISL_Listener_Destory(HANDLE hListener) AMP_RemoteEventCallbackPro((UINT32)pClientListener, &event); AMPLOGD("NOTIFY listener:%x, we are done", pClientListener); + MV_OSAL_Mutex_Lock(pEventService->hMutex); MV_OSAL_RWLock_WRLock(pEventService->hEventLock); rc = AMP_DeinitEventBuffer(&pClientListener->hEventBuffer); if (rc != S_OK) { @@ -341,6 +347,7 @@ HRESULT ISL_Listener_Destory(HANDLE hListener) pClientListener->bAllocated = 0; pEventService->uiAllocatedClients--; MV_OSAL_RWLock_Unlock(pEventService->hEventLock); + MV_OSAL_Mutex_Unlock(pEventService->hMutex); return SUCCESS; } @@ -496,13 +503,20 @@ HRESULT AMP_InitializeRemoteEventService() } AmpMemSet(pEventService, 0, sizeof(EVENT_LISTEN_SERVICE)); - rc = MV_OSAL_RWLock_Create(&pEventService->hEventLock); + rc = MV_OSAL_Mutex_Create(&pEventService->hMutex); if (rc != S_OK) { AMPLOGE("Create mutex fail:%x\n", rc); AmpFree(pEventService); return rc; } + rc = MV_OSAL_RWLock_Create(&pEventService->hEventLock); + if (rc != S_OK) { + AMPLOGE("Create RWLock fail:%x\n", rc); + AmpFree(pEventService); + return rc; + } + evl_cache_add_event(&pEventService->stEventCache, AMP_EVENT_API_DISP_CALLBACK_VSYNC); m_event_service = pEventService; @@ -519,6 +533,10 @@ HRESULT AMP_DeinitializeRemoteEventService() if (rc != S_OK) { return rc; } + rc = MV_OSAL_Mutex_Destroy(&pEventService->hMutex); + if (rc != S_OK) { + return rc; + } if (pEventService) { AmpFree(pEventService); m_event_service = NULL; @@ -555,11 +573,14 @@ HRESULT AMP_RemoteEventCallbackPro(UINT32 ServiceID, AMP_EVENT *pEvent) return SUCCESS; } + MV_OSAL_Mutex_Lock(pEventService->hMutex); + ll = send = 0; if (ServiceID == REMOTE_EVENT_SERVERID_DEFAULT) { rc = MV_OSAL_RWLock_TryWRLock(pEventService->hEventLock, 10); if (rc != S_OK) { AMPLOGE("trylock eventlock rc:%x", rc); + MV_OSAL_Mutex_Unlock(pEventService->hMutex); return rc; } @@ -605,6 +626,7 @@ HRESULT AMP_RemoteEventCallbackPro(UINT32 ServiceID, AMP_EVENT *pEvent) } MV_OSAL_RWLock_Unlock(pEventService->hEventLock); + MV_OSAL_Mutex_Unlock(pEventService->hMutex); return rc; } -- 2.7.4