shithub: openh264

Download patch

ref: ffde7b2e099e550c790b4291fab0b1c0f45dc68c
parent: a127690884bd382f57b02fcfc1316479b8b08e64
parent: 5b8ee37162625cd2260a28a0cd52607cca297217
author: volvet <[email protected]>
date: Fri Mar 7 13:14:03 EST 2014

Merge pull request #442 from mstorsjo/unify-thread-cleanup

Unify the thread cleanup code, enable threading on android

--- a/build/platform-android.mk
+++ b/build/platform-android.mk
@@ -30,7 +30,7 @@
 CXX = $(TOOLCHAINPREFIX)g++
 CC = $(TOOLCHAINPREFIX)gcc
 AR = $(TOOLCHAINPREFIX)ar
-CFLAGS += -DLINUX -DANDROID_NDK -fpic --sysroot=$(SYSROOT)
+CFLAGS += -DLINUX -DANDROID_NDK -DMT_ENABLED -fpic --sysroot=$(SYSROOT)
 CXXFLAGS += -fno-rtti -fno-exceptions
 LDFLAGS += --sysroot=$(SYSROOT)
 SHLDFLAGS = -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,-soname,libwels.so
--- a/codec/common/WelsThreadLib.cpp
+++ b/codec/common/WelsThreadLib.cpp
@@ -152,30 +152,14 @@
   return WELS_THREAD_ERROR_OK;
 }
 
-WELS_THREAD_ERROR_CODE	  WelsSetThreadCancelable() {
-  // nil implementation for WIN32
-  return WELS_THREAD_ERROR_OK;
-}
-
 WELS_THREAD_ERROR_CODE    WelsThreadJoin (WELS_THREAD_HANDLE  thread) {
   WaitForSingleObject (thread, INFINITE);
+  CloseHandle (thread);
 
   return WELS_THREAD_ERROR_OK;
 }
 
-WELS_THREAD_ERROR_CODE    WelsThreadCancel (WELS_THREAD_HANDLE  thread) {
-  return WELS_THREAD_ERROR_OK;
-}
 
-
-WELS_THREAD_ERROR_CODE    WelsThreadDestroy (WELS_THREAD_HANDLE* thread) {
-  if (thread != NULL) {
-    CloseHandle (*thread);
-    *thread = NULL;
-  }
-  return WELS_THREAD_ERROR_OK;
-}
-
 WELS_THREAD_HANDLE        WelsThreadSelf() {
   return GetCurrentThread();
 }
@@ -217,23 +201,8 @@
   return err;
 }
 
-WELS_THREAD_ERROR_CODE	  WelsSetThreadCancelable() {
-  WELS_THREAD_ERROR_CODE err = pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
-  if (0 == err)
-    err = pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL);
-  return err;
-}
-
 WELS_THREAD_ERROR_CODE    WelsThreadJoin (WELS_THREAD_HANDLE  thread) {
   return pthread_join (thread, NULL);
-}
-
-WELS_THREAD_ERROR_CODE    WelsThreadCancel (WELS_THREAD_HANDLE  thread) {
-  return pthread_cancel (thread);
-}
-
-WELS_THREAD_ERROR_CODE    WelsThreadDestroy (WELS_THREAD_HANDLE* thread) {
-  return WELS_THREAD_ERROR_OK;
 }
 
 WELS_THREAD_HANDLE        WelsThreadSelf() {
--- a/codec/common/WelsThreadLib.h
+++ b/codec/common/WelsThreadLib.h
@@ -118,13 +118,7 @@
 WELS_THREAD_ERROR_CODE    WelsThreadCreate (WELS_THREAD_HANDLE* thread,  LPWELS_THREAD_ROUTINE  routine,
     void* arg, WELS_THREAD_ATTR attr);
 
-WELS_THREAD_ERROR_CODE	  WelsSetThreadCancelable();
-
 WELS_THREAD_ERROR_CODE    WelsThreadJoin (WELS_THREAD_HANDLE  thread);
-
-WELS_THREAD_ERROR_CODE    WelsThreadCancel (WELS_THREAD_HANDLE  thread);
-
-WELS_THREAD_ERROR_CODE    WelsThreadDestroy (WELS_THREAD_HANDLE* thread);
 
 WELS_THREAD_HANDLE        WelsThreadSelf();
 
--- a/codec/encoder/core/inc/mt_defs.h
+++ b/codec/encoder/core/inc/mt_defs.h
@@ -100,9 +100,6 @@
 WELS_EVENT					pFinUpdateMbListEvent[MAX_THREADS_NUM];	// signal to indicate finish updating mb list
 WELS_EVENT					pExitEncodeEvent[MAX_THREADS_NUM];			// event for exit encoding event
 WELS_EVENT					pThreadMasterEvent[MAX_THREADS_NUM];	// event for indicating that some event has been signalled to the thread
-#ifdef _WIN32
-WELS_EVENT					pFinSliceCodingEvent[MAX_THREADS_NUM];	// notify slice coding thread is done
-#endif//_WIN32
 
 WELS_MUTEX					mutexSliceNumUpdate;	// for dynamic slicing mode MT
 
--- a/codec/encoder/core/src/encoder_ext.cpp
+++ b/codec/encoder/core/src/encoder_ext.cpp
@@ -2152,33 +2152,20 @@
     const int32_t iThreadCount = (*ppCtx)->pSvcParam->iCountThreadsNum;
     int32_t iThreadIdx = 0;
 
-#if defined(_WIN32)
     if ((*ppCtx)->pSliceThreading->pExitEncodeEvent != NULL) {
-      do {
-        if ((*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx] != NULL)	// iThreadIdx is already created successfully
+      while (iThreadIdx < iThreadCount) {
+        int res = 0;
+        if ((*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx]) {
           WelsEventSignal (& (*ppCtx)->pSliceThreading->pExitEncodeEvent[iThreadIdx]);
           WelsEventSignal (& (*ppCtx)->pSliceThreading->pThreadMasterEvent[iThreadIdx]);
+          res = WelsThreadJoin ((*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx]);	// waiting thread exit
+          WelsLog (*ppCtx, WELS_LOG_INFO, "WelsUninitEncoderExt(), pthread_join(pThreadHandles%d) return %d..\n", iThreadIdx,
+                   res);
+          (*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx] = 0;
+        }
         ++ iThreadIdx;
-      } while (iThreadIdx < iThreadCount);
-
-      WelsMultipleEventsWaitAllBlocking (iThreadCount, & (*ppCtx)->pSliceThreading->pFinSliceCodingEvent[0]);
-
-    }
-#else
-    while (iThreadIdx < iThreadCount) {
-      int res = 0;
-      if ((*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx]) {
-        res = WelsThreadCancel ((*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx]);
-        WelsLog (*ppCtx, WELS_LOG_INFO, "WelsUninitEncoderExt(), WelsThreadCancel(pThreadHandles%d) return %d..\n", iThreadIdx,
-                 res);
-        res = WelsThreadJoin ((*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx]);	// waiting thread exit
-        WelsLog (*ppCtx, WELS_LOG_INFO, "WelsUninitEncoderExt(), pthread_join(pThreadHandles%d) return %d..\n", iThreadIdx,
-                 res);
-        (*ppCtx)->pSliceThreading->pThreadHandles[iThreadIdx] = 0;
       }
-      ++ iThreadIdx;
     }
-#endif//WIN32
   }
 #endif//MT_ENABLED
 
--- a/codec/encoder/core/src/slice_multi_threading.cpp
+++ b/codec/encoder/core/src/slice_multi_threading.cpp
@@ -362,11 +362,6 @@
     pSmt->pThreadPEncCtx[iIdx].iThreadIndex	= iIdx;
     pSmt->pThreadHandles[iIdx]				= 0;
 
-#ifdef _WIN32
-    WelsSnprintf (name, SEM_NAME_MAX, "fs%d%s", iIdx, pSmt->eventNamespace);
-    err = WelsEventOpen (&pSmt->pFinSliceCodingEvent[iIdx], name);
-    MT_TRACE_LOG ((*ppCtx), WELS_LOG_INFO, "[MT] Open pFinSliceCodingEvent%d named(%s) ret%d err%d\n", iIdx, name, err, errno);
-#endif//_WIN32
     WelsSnprintf (name, SEM_NAME_MAX, "ee%d%s", iIdx, pSmt->eventNamespace);
     err = WelsEventOpen (&pSmt->pExitEncodeEvent[iIdx], name);
     MT_TRACE_LOG ((*ppCtx), WELS_LOG_INFO, "[MT] Open pExitEncodeEvent%d named(%s) ret%d err%d\n", iIdx, name, err, errno);
@@ -456,13 +451,6 @@
   char ename[SEM_NAME_MAX] = {0};
   while (iIdx < iThreadNum) {
     // 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]);
-
-    WelsSnprintf (ename, SEM_NAME_MAX, "fs%d%s", iIdx, pSmt->eventNamespace);
-    WelsEventClose (&pSmt->pFinSliceCodingEvent[iIdx], ename);
-#endif//_WIN32
     WelsSnprintf (ename, SEM_NAME_MAX, "ee%d%s", iIdx, pSmt->eventNamespace);
     WelsEventClose (&pSmt->pExitEncodeEvent[iIdx], ename);
     WelsSnprintf (ename, SEM_NAME_MAX, "tm%d%s", iIdx, pSmt->eventNamespace);
@@ -705,8 +693,6 @@
   if (NULL == pPrivateData)
     WELS_THREAD_ROUTINE_RETURN (1);
 
-  WelsSetThreadCancelable();
-
   pEncPEncCtx	= (sWelsEncCtx*)pPrivateData->pWelsPEncCtx;
 
   iThreadIdx		= pPrivateData->iThreadIndex;
@@ -958,10 +944,6 @@
       break;
     }
   } while (1);
-
-#ifdef _WIN32
-  WelsEventSignal (&pEncPEncCtx->pSliceThreading->pFinSliceCodingEvent[iEventIdx]);	// notify to mother encoding threading
-#endif//WIN32
 
   //sync multi-threading error
   WelsMutexLock (&pEncPEncCtx->mutexEncoderError);