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.
This commit is contained in:
Martin Storsjö 2014-03-08 12:58:26 +02:00
parent f293d26a62
commit b35c21201b
2 changed files with 17 additions and 0 deletions

View File

@ -2,6 +2,7 @@ include build/platform-msvc-common.mk
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

View File

@ -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 WelsEventClose (WELS_EVENT* event, const char* event_n
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;