John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame^] | 1 | // |
| 2 | // Copyright (c) 2012 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 | #include "compiler/InitializeParseContext.h" |
| 8 | |
| 9 | #include "compiler/osinclude.h" |
| 10 | |
| 11 | OS_TLSIndex GlobalParseContextIndex = OS_INVALID_TLS_INDEX; |
| 12 | |
| 13 | bool InitializeParseContextIndex() |
| 14 | { |
| 15 | if (GlobalParseContextIndex != OS_INVALID_TLS_INDEX) { |
| 16 | assert(0 && "InitializeParseContextIndex(): Parse Context already initalized"); |
| 17 | return false; |
| 18 | } |
| 19 | |
| 20 | // |
| 21 | // Allocate a TLS index. |
| 22 | // |
| 23 | GlobalParseContextIndex = OS_AllocTLSIndex(); |
| 24 | |
| 25 | if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) { |
| 26 | assert(0 && "InitializeParseContextIndex(): Parse Context already initalized"); |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | bool FreeParseContextIndex() |
| 34 | { |
| 35 | OS_TLSIndex tlsiIndex = GlobalParseContextIndex; |
| 36 | |
| 37 | if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) { |
| 38 | assert(0 && "FreeParseContextIndex(): Parse Context index not initalized"); |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | GlobalParseContextIndex = OS_INVALID_TLS_INDEX; |
| 43 | |
| 44 | return OS_FreeTLSIndex(tlsiIndex); |
| 45 | } |
| 46 | |
| 47 | bool InitializeGlobalParseContext() |
| 48 | { |
| 49 | if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) { |
| 50 | assert(0 && "InitializeGlobalParseContext(): Parse Context index not initalized"); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex)); |
| 55 | if (lpParseContext != 0) { |
| 56 | assert(0 && "InitializeParseContextIndex(): Parse Context already initalized"); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | TThreadParseContext *lpThreadData = new TThreadParseContext(); |
| 61 | if (lpThreadData == 0) { |
| 62 | assert(0 && "InitializeGlobalParseContext(): Unable to create thread parse context"); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | lpThreadData->lpGlobalParseContext = 0; |
| 67 | OS_SetTLSValue(GlobalParseContextIndex, lpThreadData); |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool FreeParseContext() |
| 73 | { |
| 74 | if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) { |
| 75 | assert(0 && "FreeParseContext(): Parse Context index not initalized"); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex)); |
| 80 | if (lpParseContext) |
| 81 | delete lpParseContext; |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | TParseContextPointer& GetGlobalParseContext() |
| 87 | { |
| 88 | // |
| 89 | // Minimal error checking for speed |
| 90 | // |
| 91 | |
| 92 | TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex)); |
| 93 | |
| 94 | return lpParseContext->lpGlobalParseContext; |
| 95 | } |
| 96 | |