Home | History | Annotate | Download | only in win32
      1 /*-------------------------------------------------------------------------
      2  * drawElements Thread Library
      3  * ---------------------------
      4  *
      5  * Copyright 2014 The Android Open Source Project
      6  *
      7  * Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  *      http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  *
     19  *//*!
     20  * \file
     21  * \brief Win32 implementation of thread management.
     22  *//*--------------------------------------------------------------------*/
     23 
     24 #include "deThread.h"
     25 
     26 #if (DE_OS == DE_OS_WIN32 || DE_OS == DE_OS_WINCE)
     27 
     28 #include "deMemory.h"
     29 
     30 #define VC_EXTRALEAN
     31 #define WIN32_LEAN_AND_MEAN
     32 #include <windows.h>
     33 
     34 /* Thread handle equals deThread in this implementation. */
     35 DE_STATIC_ASSERT(sizeof(deThread) >= sizeof(HANDLE));
     36 
     37 typedef struct ThreadEntry_s
     38 {
     39 	deThreadFunc	func;
     40 	void*			arg;
     41 } ThreadEntry;
     42 
     43 static int mapPriority (deThreadPriority priority)
     44 {
     45 	switch (priority)
     46 	{
     47 		case DE_THREADPRIORITY_LOWEST:	return THREAD_PRIORITY_IDLE;
     48 		case DE_THREADPRIORITY_LOW:		return THREAD_PRIORITY_LOWEST;
     49 		case DE_THREADPRIORITY_NORMAL:	return THREAD_PRIORITY_NORMAL;
     50 		case DE_THREADPRIORITY_HIGH:	return THREAD_PRIORITY_ABOVE_NORMAL;
     51 		case DE_THREADPRIORITY_HIGHEST:	return THREAD_PRIORITY_HIGHEST;
     52 		default:	DE_ASSERT(DE_FALSE);
     53 	}
     54 	return 0;
     55 }
     56 
     57 static DWORD __stdcall startThread (LPVOID entryPtr)
     58 {
     59 	ThreadEntry*	entry	= (ThreadEntry*)entryPtr;
     60 	deThreadFunc	func	= entry->func;
     61 	void*			arg		= entry->arg;
     62 
     63 	deFree(entry);
     64 
     65 	func(arg);
     66 
     67 	return 0;
     68 }
     69 
     70 deThread deThread_create (deThreadFunc func, void* arg, const deThreadAttributes* attributes)
     71 {
     72 	ThreadEntry*	entry	= (ThreadEntry*)deMalloc(sizeof(ThreadEntry));
     73 	HANDLE			thread	= 0;
     74 
     75 	if (!entry)
     76 		return 0;
     77 
     78 	entry->func	= func;
     79 	entry->arg	= arg;
     80 
     81 	thread = CreateThread(DE_NULL, 0, startThread, entry, 0, DE_NULL);
     82 	if (!thread)
     83 	{
     84 		deFree(entry);
     85 		return 0;
     86 	}
     87 
     88 	if (attributes)
     89 		SetThreadPriority(thread, mapPriority(attributes->priority));
     90 
     91 	return (deThread)thread;
     92 }
     93 
     94 deBool deThread_join (deThread thread)
     95 {
     96 	HANDLE	handle		= (HANDLE)thread;
     97 	WaitForSingleObject(handle, INFINITE);
     98 
     99 	return DE_TRUE;
    100 }
    101 
    102 void deThread_destroy (deThread thread)
    103 {
    104 	HANDLE	handle		= (HANDLE)thread;
    105 	CloseHandle(handle);
    106 }
    107 
    108 void deSleep (deUint32 milliseconds)
    109 {
    110 	Sleep((DWORD)milliseconds);
    111 }
    112 
    113 void deYield (void)
    114 {
    115 	SwitchToThread();
    116 }
    117 
    118 #endif /* DE_OS */
    119