1// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//
16// This file contains the posix specific functions
17//
18#include "osinclude.h"
19
20#if !defined(ANGLE_OS_POSIX)
21#error Trying to build a posix specific file in a non-posix build.
22#endif
23
24//
25// Thread Local Storage Operations
26//
27OS_TLSIndex OS_AllocTLSIndex()
28{
29 pthread_key_t pPoolIndex;
30
31 //
32 // Create global pool key.
33 //
34 if ((pthread_key_create(&pPoolIndex, NULL)) != 0) {
35 assert(0 && "OS_AllocTLSIndex(): Unable to allocate Thread Local Storage");
36 return false;
37 }
38 else {
39 return pPoolIndex;
40 }
41}
42
43
44bool OS_SetTLSValue(OS_TLSIndex nIndex, void *lpvValue)
45{
46 if (nIndex == OS_INVALID_TLS_INDEX) {
47 assert(0 && "OS_SetTLSValue(): Invalid TLS Index");
48 return false;
49 }
50
51 if (pthread_setspecific(nIndex, lpvValue) == 0)
52 return true;
53 else
54 return false;
55}
56
57
58bool OS_FreeTLSIndex(OS_TLSIndex nIndex)
59{
60 if (nIndex == OS_INVALID_TLS_INDEX) {
61 assert(0 && "OS_SetTLSValue(): Invalid TLS Index");
62 return false;
63 }
64
65 //
66 // Delete the global pool key.
67 //
68 if (pthread_key_delete(nIndex) == 0)
69 return true;
70 else
71 return false;
72}
73