1/*-------------------------------------------------------------------------
2 * dEQP glslang integration
3 * ------------------------
4 *
5 * Copyright 2015 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 glslang OS interface.
22 *//*--------------------------------------------------------------------*/
23
24#include "osinclude.h"
25
26#include "deThread.h"
27#include "deThreadLocal.h"
28#include "deMutex.h"
29
30namespace glslang
31{
32
33DE_STATIC_ASSERT(sizeof(deThreadLocal)	== sizeof(OS_TLSIndex));
34DE_STATIC_ASSERT(sizeof(deThread)		== sizeof(void*));
35
36// Thread-local
37
38OS_TLSIndex OS_AllocTLSIndex (void)
39{
40	return (OS_TLSIndex)deThreadLocal_create();
41}
42
43bool OS_SetTLSValue (OS_TLSIndex nIndex, void* lpvValue)
44{
45	deThreadLocal_set((deThreadLocal)nIndex, lpvValue);
46	return true;
47}
48
49bool OS_FreeTLSIndex (OS_TLSIndex nIndex)
50{
51	deThreadLocal_destroy((deThreadLocal)nIndex);
52	return true;
53}
54
55void* OS_GetTLSValue (OS_TLSIndex nIndex)
56{
57	return deThreadLocal_get((deThreadLocal)nIndex);
58}
59
60// Global lock
61
62static deMutex s_globalLock = 0;
63
64void InitGlobalLock (void)
65{
66	DE_ASSERT(s_globalLock == 0);
67	s_globalLock = deMutex_create(DE_NULL);
68}
69
70void GetGlobalLock (void)
71{
72	deMutex_lock(s_globalLock);
73}
74
75void ReleaseGlobalLock (void)
76{
77	deMutex_unlock(s_globalLock);
78}
79
80// Threading
81
82DE_STATIC_ASSERT(sizeof(void*) >= sizeof(deThread));
83
84static void EnterGenericThread (void* entry)
85{
86	((TThreadEntrypoint)entry)(DE_NULL);
87}
88
89void* OS_CreateThread (TThreadEntrypoint entry)
90{
91	return (void*)(deUintptr)deThread_create(EnterGenericThread, (void*)entry, DE_NULL);
92}
93
94void OS_WaitForAllThreads (void* threads, int numThreads)
95{
96	for (int ndx = 0; ndx < numThreads; ndx++)
97	{
98		const deThread thread = (deThread)(deUintptr)((void**)threads)[ndx];
99		deThread_join(thread);
100		deThread_destroy(thread);
101	}
102}
103
104void OS_Sleep (int milliseconds)
105{
106	deSleep(milliseconds);
107}
108
109void OS_DumpMemoryCounters (void)
110{
111	// Not used
112}
113
114} // glslang
115