13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Thread Library
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ---------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Win32 implementation of thread management.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deThread.h"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if (DE_OS == DE_OS_WIN32 || DE_OS == DE_OS_WINCE)
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deMemory.h"
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define VC_EXTRALEAN
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define WIN32_LEAN_AND_MEAN
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <windows.h>
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/* Thread handle equals deThread in this implementation. */
353c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDE_STATIC_ASSERT(sizeof(deThread) >= sizeof(HANDLE));
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef struct ThreadEntry_s
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deThreadFunc	func;
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void*			arg;
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} ThreadEntry;
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic int mapPriority (deThreadPriority priority)
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	switch (priority)
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case DE_THREADPRIORITY_LOWEST:	return THREAD_PRIORITY_IDLE;
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case DE_THREADPRIORITY_LOW:		return THREAD_PRIORITY_LOWEST;
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case DE_THREADPRIORITY_NORMAL:	return THREAD_PRIORITY_NORMAL;
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case DE_THREADPRIORITY_HIGH:	return THREAD_PRIORITY_ABOVE_NORMAL;
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case DE_THREADPRIORITY_HIGHEST:	return THREAD_PRIORITY_HIGHEST;
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		default:	DE_ASSERT(DE_FALSE);
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return 0;
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic DWORD __stdcall startThread (LPVOID entryPtr)
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ThreadEntry*	entry	= (ThreadEntry*)entryPtr;
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deThreadFunc	func	= entry->func;
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void*			arg		= entry->arg;
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deFree(entry);
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	func(arg);
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return 0;
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
703c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeThread deThread_create (deThreadFunc func, void* arg, const deThreadAttributes* attributes)
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ThreadEntry*	entry	= (ThreadEntry*)deMalloc(sizeof(ThreadEntry));
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	HANDLE			thread	= 0;
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!entry)
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return 0;
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	entry->func	= func;
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	entry->arg	= arg;
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	thread = CreateThread(DE_NULL, 0, startThread, entry, 0, DE_NULL);
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!thread)
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		deFree(entry);
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return 0;
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (attributes)
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		SetThreadPriority(thread, mapPriority(attributes->priority));
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return (deThread)thread;
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
943c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeBool deThread_join (deThread thread)
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	HANDLE	handle		= (HANDLE)thread;
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	WaitForSingleObject(handle, INFINITE);
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return DE_TRUE;
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid deThread_destroy (deThread thread)
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	HANDLE	handle		= (HANDLE)thread;
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	CloseHandle(handle);
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid deSleep (deUint32 milliseconds)
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Sleep((DWORD)milliseconds);
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid deYield (void)
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	SwitchToThread();
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif /* DE_OS */
119