shithub: openh264

Download patch

ref: 8beb3c8c09db358068d8f9d081c9a3a274b6e768
parent: 61dae45fad6594954a9244b804d9a28c9ec76575
parent: d4bdef29167134919d80690213b30bbd5692b2f6
author: volvet <qizh@cisco.com>
date: Thu Mar 6 04:13:13 EST 2014

Merge pull request #417 from mstorsjo/unify-event-init

Unify the interface for creating/deleting event objects

--- a/codec/common/WelsThreadLib.cpp
+++ b/codec/common/WelsThreadLib.cpp
@@ -58,6 +58,7 @@
 
 #include "WelsThreadLib.h"
 #include <stdio.h>
+#include <stdlib.h>
 
 #ifdef MT_ENABLED
 
@@ -91,7 +92,7 @@
   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 @@
   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 @@
 
 // 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 @@
   } 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) {
--- a/codec/common/WelsThreadLib.h
+++ b/codec/common/WelsThreadLib.h
@@ -105,12 +105,8 @@
 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);
--- a/codec/encoder/core/inc/mt_defs.h
+++ b/codec/encoder/core/inc/mt_defs.h
@@ -91,6 +91,7 @@
 
 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]
--- a/codec/encoder/core/src/slice_multi_threading.cpp
+++ b/codec/encoder/core/src/slice_multi_threading.cpp
@@ -43,11 +43,12 @@
 #include <assert.h>
 #if !defined(_WIN32)
 #include <semaphore.h>
+#include <unistd.h>
+#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 @@
                           "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,10 +360,8 @@
 
   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;
@@ -362,36 +368,39 @@
     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 @@
     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));
+    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%p", iIdx, (void*) (*ppCtx));
+    WelsSnprintf (ename, SEM_NAME_MAX, "rc%d%s", iIdx, pSmt->eventNamespace);
     WelsEventClose (&pSmt->pReadySliceCodingEvent[iIdx], ename);
-    WelsSnprintf (ename, SEM_NAME_MAX, "ud%d%p", iIdx, (void*) (*ppCtx));
+    WelsSnprintf (ename, SEM_NAME_MAX, "ud%d%s", iIdx, pSmt->eventNamespace);
     WelsEventClose (&pSmt->pUpdateMbListEvent[iIdx], ename);
-    WelsSnprintf (ename, SEM_NAME_MAX, "fu%d%p", iIdx, (void*) (*ppCtx));
+    WelsSnprintf (ename, SEM_NAME_MAX, "fu%d%s", iIdx, pSmt->eventNamespace);
     WelsEventClose (&pSmt->pFinUpdateMbListEvent[iIdx], ename);
-#endif//_WIN32
 
     ++ iIdx;
   }