shithub: openh264

Download patch

ref: b35c21201ba006ddae5f10e103cc8cc03861fac7
parent: f293d26a6230524e63bead2a0014437edd74ae8c
author: Martin Storsjö <[email protected]>
date: Sat Mar 8 07:58:26 EST 2014

Use the Windows Runtime ThreadPool API for creating threads on Windows Phone

Windows Phone lacks the old CreateThread/beginthreadex APIs for
creating threads. (Technically, the functions still do exist,
but they aren't officially supported and aren't visible in the
headers when targeting Windows Phone.)

Building code that uses the Windows Runtime language extensions
requires building with the -ZW option.

--- a/build/platform-msvc-arm.mk
+++ b/build/platform-msvc-arm.mk
@@ -2,6 +2,7 @@
 ARCH=arm
 include build/platform-arch.mk
 CFLAGS += -DWINAPI_FAMILY=WINAPI_FAMILY_PHONE_APP -MD -DWIN32
+CXXFLAGS += -ZW
 LDFLAGS +=
 CCAS = gas-preprocessor.pl -as-type armasm -force-thumb -- armasm
 
--- a/codec/common/src/WelsThreadLib.cpp
+++ b/codec/common/src/WelsThreadLib.cpp
@@ -68,6 +68,11 @@
 
 #ifdef WINAPI_FAMILY
 #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
+using namespace Platform;
+using namespace Windows::Foundation;
+using namespace Windows::System::Threading;
+#define USE_THREADPOOL
+
 #define InitializeCriticalSection(x) InitializeCriticalSectionEx(x, 0, 0)
 #define GetSystemInfo(x) GetNativeSystemInfo(x)
 #define CreateEvent(attr, reset, init, name) CreateEventEx(attr, name, ((reset) ? CREATE_EVENT_MANUAL_RESET : 0) | ((init) ? CREATE_EVENT_INITIAL_SET : 0), EVENT_ALL_ACCESS)
@@ -169,7 +174,18 @@
 
 WELS_THREAD_ERROR_CODE    WelsThreadCreate (WELS_THREAD_HANDLE* thread,  LPWELS_THREAD_ROUTINE  routine,
     void* arg, WELS_THREAD_ATTR attr) {
+#ifdef USE_THREADPOOL
+  HANDLE h = CreateEvent(NULL, FALSE, FALSE, NULL);
+  HANDLE h2;
+  DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), &h2, 0, FALSE, DUPLICATE_SAME_ACCESS);
+  ThreadPool::RunAsync(ref new WorkItemHandler([=](IAsyncAction^) {
+    routine(arg);
+    SetEvent(h2);
+    CloseHandle(h2);
+  }, CallbackContext::Any), WorkItemPriority::Normal, WorkItemOptions::TimeSliced);
+#else
   WELS_THREAD_HANDLE   h = CreateThread (NULL, 0, routine, arg, 0, NULL);
+#endif
 
   if (h == NULL) {
     return WELS_THREAD_ERROR_GENERAL;