1 // 2 // Copyright (c) 2014 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 7 // tls.cpp: Simple cross-platform interface for thread local storage. 8 9 #include "common/tls.h" 10 11 #include <assert.h> 12 CreateTLSIndex()13TLSIndex CreateTLSIndex() 14 { 15 TLSIndex index; 16 17 #ifdef ANGLE_PLATFORM_WINDOWS 18 index = TlsAlloc(); 19 #elif defined(ANGLE_PLATFORM_POSIX) 20 // Create global pool key 21 if ((pthread_key_create(&index, NULL)) != 0) 22 { 23 index = TLS_INVALID_INDEX; 24 } 25 #endif 26 27 assert(index != TLS_INVALID_INDEX && "CreateTLSIndex(): Unable to allocate Thread Local Storage"); 28 return index; 29 } 30 DestroyTLSIndex(TLSIndex index)31bool DestroyTLSIndex(TLSIndex index) 32 { 33 assert(index != TLS_INVALID_INDEX && "DestroyTLSIndex(): Invalid TLS Index"); 34 if (index == TLS_INVALID_INDEX) 35 { 36 return false; 37 } 38 39 #ifdef ANGLE_PLATFORM_WINDOWS 40 return (TlsFree(index) == TRUE); 41 #elif defined(ANGLE_PLATFORM_POSIX) 42 return (pthread_key_delete(index) == 0); 43 #endif 44 } 45 SetTLSValue(TLSIndex index,void * value)46bool SetTLSValue(TLSIndex index, void *value) 47 { 48 assert(index != TLS_INVALID_INDEX && "SetTLSValue(): Invalid TLS Index"); 49 if (index == TLS_INVALID_INDEX) 50 { 51 return false; 52 } 53 54 #ifdef ANGLE_PLATFORM_WINDOWS 55 return (TlsSetValue(index, value) == TRUE); 56 #elif defined(ANGLE_PLATFORM_POSIX) 57 return (pthread_setspecific(index, value) == 0); 58 #endif 59 } 60 GetTLSValue(TLSIndex index)61void *GetTLSValue(TLSIndex index) 62 { 63 assert(index != TLS_INVALID_INDEX && "GetTLSValue(): Invalid TLS Index"); 64 if (index == TLS_INVALID_INDEX) 65 { 66 return NULL; 67 } 68 69 #ifdef ANGLE_PLATFORM_WINDOWS 70 return TlsGetValue(index); 71 #elif defined(ANGLE_PLATFORM_POSIX) 72 return pthread_getspecific(index); 73 #endif 74 } 75