diff --git a/codec/common/WelsThreadLib.cpp b/codec/common/WelsThreadLib.cpp index 2d906d3a..f3c5e2f9 100644 --- a/codec/common/WelsThreadLib.cpp +++ b/codec/common/WelsThreadLib.cpp @@ -58,6 +58,7 @@ #include "WelsThreadLib.h" #include +#include #ifdef MT_ENABLED @@ -91,7 +92,7 @@ WELS_THREAD_ERROR_CODE WelsMutexDestroy (WELS_MUTEX* mutex) { return WELS_THREAD_ERROR_OK; } -WELS_THREAD_ERROR_CODE WelsEventInit (WELS_EVENT* event) { +WELS_THREAD_ERROR_CODE WelsEventOpen (WELS_EVENT* event, const char* event_name) { WELS_EVENT h = CreateEvent (NULL, FALSE, FALSE, NULL); if (h == NULL) { @@ -126,7 +127,7 @@ WELS_THREAD_ERROR_CODE WelsMultipleEventsWaitAllBlocking (uint32_t nCount, WE return WaitForMultipleObjects (nCount, event_list, TRUE, INFINITE); } -WELS_THREAD_ERROR_CODE WelsEventDestroy (WELS_EVENT* event) { +WELS_THREAD_ERROR_CODE WelsEventClose (WELS_EVENT* event, const char* event_name) { CloseHandle (*event); *event = NULL; @@ -252,15 +253,8 @@ WELS_THREAD_ERROR_CODE WelsMutexDestroy (WELS_MUTEX* mutex) { // unnamed semaphores aren't supported on OS X -WELS_THREAD_ERROR_CODE WelsEventInit (WELS_EVENT* event) { - return sem_init (*event, 0, 0); -} - -WELS_THREAD_ERROR_CODE WelsEventDestroy (WELS_EVENT* event) { - return sem_destroy (*event); // match with sem_init -} - WELS_THREAD_ERROR_CODE WelsEventOpen (WELS_EVENT* p_event, const char* event_name) { +#ifdef __APPLE__ if (p_event == NULL || event_name == NULL) return WELS_THREAD_ERROR_GENERAL; *p_event = sem_open (event_name, O_CREAT, (S_IRUSR | S_IWUSR)/*0600*/, 0); @@ -271,12 +265,30 @@ WELS_THREAD_ERROR_CODE WelsEventOpen (WELS_EVENT* p_event, const char* event_ } else { return WELS_THREAD_ERROR_OK; } +#else + WELS_EVENT event = (WELS_EVENT) malloc(sizeof(*event)); + if (event == NULL) + return WELS_THREAD_ERROR_GENERAL; + WELS_THREAD_ERROR_CODE err = sem_init(event, 0, 0); + if (!err) { + *p_event = event; + return err; + } + free(event); + return err; +#endif } WELS_THREAD_ERROR_CODE WelsEventClose (WELS_EVENT* event, const char* event_name) { +#ifdef __APPLE__ WELS_THREAD_ERROR_CODE err = sem_close (*event); // match with sem_open if (event_name) sem_unlink (event_name); return err; +#else + WELS_THREAD_ERROR_CODE err = sem_destroy (*event); // match with sem_init + free(*event); + return err; +#endif } WELS_THREAD_ERROR_CODE WelsEventSignal (WELS_EVENT* event) { diff --git a/codec/common/WelsThreadLib.h b/codec/common/WelsThreadLib.h index 1301a7f7..d88043d9 100644 --- a/codec/common/WelsThreadLib.h +++ b/codec/common/WelsThreadLib.h @@ -105,12 +105,8 @@ WELS_THREAD_ERROR_CODE WelsMutexLock (WELS_MUTEX* mutex); WELS_THREAD_ERROR_CODE WelsMutexUnlock (WELS_MUTEX* mutex); WELS_THREAD_ERROR_CODE WelsMutexDestroy (WELS_MUTEX* mutex); -#ifndef _WIN32 WELS_THREAD_ERROR_CODE WelsEventOpen (WELS_EVENT* p_event, const char* event_name); WELS_THREAD_ERROR_CODE WelsEventClose (WELS_EVENT* event, const char* event_name); -#endif//!_WIN32 -WELS_THREAD_ERROR_CODE WelsEventInit (WELS_EVENT* event); -WELS_THREAD_ERROR_CODE WelsEventDestroy (WELS_EVENT* event); WELS_THREAD_ERROR_CODE WelsEventSignal (WELS_EVENT* event); WELS_THREAD_ERROR_CODE WelsEventWait (WELS_EVENT* event); WELS_THREAD_ERROR_CODE WelsEventWaitWithTimeOut (WELS_EVENT* event, uint32_t dwMilliseconds); diff --git a/codec/encoder/core/inc/mt_defs.h b/codec/encoder/core/inc/mt_defs.h index f46eda63..7fdaf0ed 100644 --- a/codec/encoder/core/inc/mt_defs.h +++ b/codec/encoder/core/inc/mt_defs.h @@ -91,6 +91,7 @@ int32_t iEndMbIndex; // exclusive typedef struct TagSliceThreading { SSliceThreadPrivateData* pThreadPEncCtx;// thread context, [iThreadIdx] +char eventNamespace[100]; WELS_THREAD_HANDLE pThreadHandles[MAX_THREADS_NUM];// thread handles, [iThreadIdx] WELS_EVENT pSliceCodedEvent[MAX_THREADS_NUM];// events for slice coded state, [iThreadIdx] WELS_EVENT pReadySliceCodingEvent[MAX_THREADS_NUM]; // events for slice coding ready, [iThreadIdx] diff --git a/codec/encoder/core/src/slice_multi_threading.cpp b/codec/encoder/core/src/slice_multi_threading.cpp index cde622c7..fce8acfe 100644 --- a/codec/encoder/core/src/slice_multi_threading.cpp +++ b/codec/encoder/core/src/slice_multi_threading.cpp @@ -43,11 +43,12 @@ #include #if !defined(_WIN32) #include +#include +#endif//!_WIN32 #ifndef SEM_NAME_MAX // length of semaphore name should be system constrained at least on mac 10.7 #define SEM_NAME_MAX 32 #endif//SEM_NAME_MAX -#endif//!_WIN32 #include "slice_multi_threading.h" #include "mt_defs.h" #include "nal_encap.h" @@ -317,6 +318,13 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara "pThreadPEncCtx"); WELS_VERIFY_RETURN_PROC_IF (1, (NULL == pSmt->pThreadPEncCtx), FreeMemorySvc (ppCtx)) +#ifdef _WIN32 + // Dummy event namespace, the windows events don't actually use this + WelsSnprintf (pSmt->eventNamespace, sizeof(pSmt->eventNamespace), "%p", (void*) (*ppCtx)); +#else + WelsSnprintf (pSmt->eventNamespace, sizeof(pSmt->eventNamespace), "%p%x", (void*) (*ppCtx), getpid()); +#endif//!_WIN32 + iIdx = 0; while (iIdx < iNumSpatialLayers) { SSliceConfig* pMso = &pPara->sDependencyLayers[iIdx].sSliceCfg; @@ -352,46 +360,47 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara iIdx = 0; while (iIdx < iThreadNum) { -#if !defined(_WIN32) // for posix threading char name[SEM_NAME_MAX] = {0}; WELS_THREAD_ERROR_CODE err = 0; -#endif//!_WIN32 pSmt->pThreadPEncCtx[iIdx].pWelsPEncCtx = (void*) (*ppCtx); pSmt->pThreadPEncCtx[iIdx].iSliceIndex = iIdx; pSmt->pThreadPEncCtx[iIdx].iThreadIndex = iIdx; pSmt->pThreadHandles[iIdx] = 0; #ifdef _WIN32 - WelsEventInit (&pSmt->pUpdateMbListEvent[iIdx]); - WelsEventInit (&pSmt->pFinUpdateMbListEvent[iIdx]); - WelsEventInit (&pSmt->pSliceCodedEvent[iIdx]); - WelsEventInit (&pSmt->pReadySliceCodingEvent[iIdx]); - WelsEventInit (&pSmt->pFinSliceCodingEvent[iIdx]); - WelsEventInit (&pSmt->pExitEncodeEvent[iIdx]); -#else + WelsSnprintf (name, SEM_NAME_MAX, "fs%d%s", iIdx, pSmt->eventNamespace); + err = WelsEventOpen (&pSmt->pFinSliceCodingEvent[iIdx], name); +#if defined(ENABLE_TRACE_MT) + WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pFinSliceCodingEvent%d named(%s) ret%d err%d\n", iIdx, name, err, errno); +#endif + WelsSnprintf (name, SEM_NAME_MAX, "ee%d%s", iIdx, pSmt->eventNamespace); + err = WelsEventOpen (&pSmt->pExitEncodeEvent[iIdx], name); +#if defined(ENABLE_TRACE_MT) + WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pExitEncodeEvent%d named(%s) ret%d err%d\n", iIdx, name, err, errno); +#endif +#endif//_WIN32 // length of semaphore name should be system constrained at least on mac 10.7 - WelsSnprintf (name, SEM_NAME_MAX, "ud%d%p", iIdx, (void*) (*ppCtx)); + WelsSnprintf (name, SEM_NAME_MAX, "ud%d%s", iIdx, pSmt->eventNamespace); err = WelsEventOpen (&pSmt->pUpdateMbListEvent[iIdx], name); #if defined(ENABLE_TRACE_MT) WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pUpdateMbListEvent%d named(%s) ret%d err%d\n", iIdx, name, err, errno); #endif - WelsSnprintf (name, SEM_NAME_MAX, "fu%d%p", iIdx, (void*) (*ppCtx)); + WelsSnprintf (name, SEM_NAME_MAX, "fu%d%s", iIdx, pSmt->eventNamespace); err = WelsEventOpen (&pSmt->pFinUpdateMbListEvent[iIdx], name); #if defined(ENABLE_TRACE_MT) WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pFinUpdateMbListEvent%d named(%s) ret%d err%d\n", iIdx, name, err, errno); #endif - WelsSnprintf (name, SEM_NAME_MAX, "sc%d%p", iIdx, (void*) (*ppCtx)); + WelsSnprintf (name, SEM_NAME_MAX, "sc%d%s", iIdx, pSmt->eventNamespace); err = WelsEventOpen (&pSmt->pSliceCodedEvent[iIdx], name); #if defined(ENABLE_TRACE_MT) WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pSliceCodedEvent%d named(%s) ret%d err%d\n", iIdx, name, err, errno); #endif - WelsSnprintf (name, SEM_NAME_MAX, "rc%d%p", iIdx, (void*) (*ppCtx)); + WelsSnprintf (name, SEM_NAME_MAX, "rc%d%s", iIdx, pSmt->eventNamespace); err = WelsEventOpen (&pSmt->pReadySliceCodingEvent[iIdx], name); #if defined(ENABLE_TRACE_MT) WelsLog ((*ppCtx), WELS_LOG_INFO, "[MT] Open pReadySliceCodingEvent%d = 0x%p named(%s) ret%d err%d\n", iIdx, (void*)pSmt->pReadySliceCodingEvent[iIdx], name, err, errno); #endif -#endif//_WIN32 ++ iIdx; } @@ -457,34 +466,25 @@ void ReleaseMtResource (sWelsEncCtx** ppCtx) { return; while (iIdx < iThreadNum) { + char ename[SEM_NAME_MAX] = {0}; + // length of semaphore name should be system constrained at least on mac 10.7 #ifdef _WIN32 if (pSmt->pThreadHandles != NULL && pSmt->pThreadHandles[iIdx] != NULL) WelsThreadDestroy (&pSmt->pThreadHandles[iIdx]); - if (pSmt->pSliceCodedEvent != NULL) - WelsEventDestroy (&pSmt->pSliceCodedEvent[iIdx]); - if (pSmt->pReadySliceCodingEvent != NULL) - WelsEventDestroy (&pSmt->pReadySliceCodingEvent[iIdx]); - if (pSmt->pFinSliceCodingEvent != NULL) - WelsEventDestroy (&pSmt->pFinSliceCodingEvent[iIdx]); - if (pSmt->pExitEncodeEvent != NULL) - WelsEventDestroy (&pSmt->pExitEncodeEvent[iIdx]); - if (pSmt->pUpdateMbListEvent != NULL) - WelsEventDestroy (&pSmt->pUpdateMbListEvent[iIdx]); - if (pSmt->pFinUpdateMbListEvent != NULL) - WelsEventDestroy (&pSmt->pFinUpdateMbListEvent[iIdx]); -#else - char ename[SEM_NAME_MAX] = {0}; - // length of semaphore name should be system constrained at least on mac 10.7 - WelsSnprintf (ename, SEM_NAME_MAX, "sc%d%p", iIdx, (void*) (*ppCtx)); - WelsEventClose (&pSmt->pSliceCodedEvent[iIdx], ename); - WelsSnprintf (ename, SEM_NAME_MAX, "rc%d%p", iIdx, (void*) (*ppCtx)); - WelsEventClose (&pSmt->pReadySliceCodingEvent[iIdx], ename); - WelsSnprintf (ename, SEM_NAME_MAX, "ud%d%p", iIdx, (void*) (*ppCtx)); - WelsEventClose (&pSmt->pUpdateMbListEvent[iIdx], ename); - WelsSnprintf (ename, SEM_NAME_MAX, "fu%d%p", iIdx, (void*) (*ppCtx)); - WelsEventClose (&pSmt->pFinUpdateMbListEvent[iIdx], ename); + WelsSnprintf (ename, SEM_NAME_MAX, "fs%d%s", iIdx, pSmt->eventNamespace); + WelsEventClose (&pSmt->pFinSliceCodingEvent[iIdx], ename); + WelsSnprintf (ename, SEM_NAME_MAX, "ee%d%s", iIdx, pSmt->eventNamespace); + WelsEventClose (&pSmt->pExitEncodeEvent[iIdx], ename); #endif//_WIN32 + WelsSnprintf (ename, SEM_NAME_MAX, "sc%d%s", iIdx, pSmt->eventNamespace); + WelsEventClose (&pSmt->pSliceCodedEvent[iIdx], ename); + WelsSnprintf (ename, SEM_NAME_MAX, "rc%d%s", iIdx, pSmt->eventNamespace); + WelsEventClose (&pSmt->pReadySliceCodingEvent[iIdx], ename); + WelsSnprintf (ename, SEM_NAME_MAX, "ud%d%s", iIdx, pSmt->eventNamespace); + WelsEventClose (&pSmt->pUpdateMbListEvent[iIdx], ename); + WelsSnprintf (ename, SEM_NAME_MAX, "fu%d%s", iIdx, pSmt->eventNamespace); + WelsEventClose (&pSmt->pFinUpdateMbListEvent[iIdx], ename); ++ iIdx; }