daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 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 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 7 | #include "compiler/InitializeDll.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 8 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 9 | #include "compiler/InitializeGlobals.h" |
| 10 | #include "compiler/InitializeParseContext.h" |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame^] | 11 | #include "compiler/osinclude.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 12 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 13 | OS_TLSIndex ThreadInitializeIndex = OS_INVALID_TLS_INDEX; |
| 14 | |
| 15 | bool InitProcess() |
| 16 | { |
| 17 | if (ThreadInitializeIndex != OS_INVALID_TLS_INDEX) { |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame^] | 18 | // |
| 19 | // Function is re-entrant. |
| 20 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 21 | return true; |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame^] | 22 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 23 | |
| 24 | ThreadInitializeIndex = OS_AllocTLSIndex(); |
| 25 | |
| 26 | if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) { |
| 27 | assert(0 && "InitProcess(): Failed to allocate TLS area for init flag"); |
| 28 | return false; |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame^] | 29 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | if (!InitializePoolIndex()) { |
| 33 | assert(0 && "InitProcess(): Failed to initalize global pool"); |
| 34 | return false; |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame^] | 35 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 36 | |
| 37 | if (!InitializeParseContextIndex()) { |
| 38 | assert(0 && "InitProcess(): Failed to initalize parse context"); |
| 39 | return false; |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame^] | 40 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 41 | |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame^] | 42 | return InitThread(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | bool DetachProcess() |
| 46 | { |
| 47 | bool success = true; |
| 48 | |
| 49 | if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) |
| 50 | return true; |
| 51 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 52 | success = DetachThread(); |
| 53 | |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame^] | 54 | if (!FreeParseContextIndex()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 55 | success = false; |
| 56 | |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame^] | 57 | FreePoolIndex(); |
| 58 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 59 | OS_FreeTLSIndex(ThreadInitializeIndex); |
| 60 | ThreadInitializeIndex = OS_INVALID_TLS_INDEX; |
| 61 | |
| 62 | return success; |
| 63 | } |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame^] | 64 | |
| 65 | bool InitThread() |
| 66 | { |
| 67 | // |
| 68 | // This function is re-entrant |
| 69 | // |
| 70 | if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) { |
| 71 | assert(0 && "InitThread(): Process hasn't been initalised."); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | if (OS_GetTLSValue(ThreadInitializeIndex) != 0) |
| 76 | return true; |
| 77 | |
| 78 | InitializeGlobalPools(); |
| 79 | |
| 80 | if (!InitializeGlobalParseContext()) |
| 81 | return false; |
| 82 | |
| 83 | if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)1)) { |
| 84 | assert(0 && "InitThread(): Unable to set init flag."); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | bool DetachThread() |
| 92 | { |
| 93 | bool success = true; |
| 94 | |
| 95 | if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) |
| 96 | return true; |
| 97 | |
| 98 | // |
| 99 | // Function is re-entrant and this thread may not have been initalised. |
| 100 | // |
| 101 | if (OS_GetTLSValue(ThreadInitializeIndex) != 0) { |
| 102 | if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)0)) { |
| 103 | assert(0 && "DetachThread(): Unable to clear init flag."); |
| 104 | success = false; |
| 105 | } |
| 106 | |
| 107 | if (!FreeParseContext()) |
| 108 | success = false; |
| 109 | |
| 110 | FreeGlobalPools(); |
| 111 | } |
| 112 | |
| 113 | return success; |
| 114 | } |
| 115 | |