Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(610)

Side by Side Diff: base/i18n/rtl.cc

Issue 2877983004: [TSAN] Fix TSAN error in i18n RTL. (Closed)
Patch Set: fix typo in type name Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/i18n/rtl.h" 5 #include "base/i18n/rtl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
11 11
12 #include "base/atomicops.h"
12 #include "base/command_line.h" 13 #include "base/command_line.h"
13 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
14 #include "base/i18n/base_i18n_switches.h" 15 #include "base/i18n/base_i18n_switches.h"
15 #include "base/logging.h" 16 #include "base/logging.h"
16 #include "base/macros.h" 17 #include "base/macros.h"
17 #include "base/strings/string_split.h" 18 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
19 #include "base/strings/sys_string_conversions.h" 20 #include "base/strings/sys_string_conversions.h"
20 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
21 #include "build/build_config.h" 22 #include "build/build_config.h"
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 107
107 return base::i18n::UNKNOWN_DIRECTION; 108 return base::i18n::UNKNOWN_DIRECTION;
108 } 109 }
109 110
110 } // namespace 111 } // namespace
111 112
112 namespace base { 113 namespace base {
113 namespace i18n { 114 namespace i18n {
114 115
115 // Represents the locale-specific ICU text direction. 116 // Represents the locale-specific ICU text direction.
116 static TextDirection g_icu_text_direction = UNKNOWN_DIRECTION; 117 static subtle::Atomic32 g_icu_text_direction =
118 static_cast<subtle::Atomic32>(UNKNOWN_DIRECTION);
117 119
118 // Convert the ICU default locale to a string. 120 // Convert the ICU default locale to a string.
119 std::string GetConfiguredLocale() { 121 std::string GetConfiguredLocale() {
120 return GetLocaleString(icu::Locale::getDefault()); 122 return GetLocaleString(icu::Locale::getDefault());
121 } 123 }
122 124
123 // Convert the ICU canonicalized locale to a string. 125 // Convert the ICU canonicalized locale to a string.
124 std::string GetCanonicalLocale(const std::string& locale) { 126 std::string GetCanonicalLocale(const std::string& locale) {
125 return GetLocaleString(icu::Locale::createCanonical(locale.c_str())); 127 return GetLocaleString(icu::Locale::createCanonical(locale.c_str()));
126 } 128 }
(...skipping 28 matching lines...) Expand all
155 157
156 void SetICUDefaultLocale(const std::string& locale_string) { 158 void SetICUDefaultLocale(const std::string& locale_string) {
157 icu::Locale locale(ICULocaleName(locale_string).c_str()); 159 icu::Locale locale(ICULocaleName(locale_string).c_str());
158 UErrorCode error_code = U_ZERO_ERROR; 160 UErrorCode error_code = U_ZERO_ERROR;
159 icu::Locale::setDefault(locale, error_code); 161 icu::Locale::setDefault(locale, error_code);
160 // This return value is actually bogus because Locale object is 162 // This return value is actually bogus because Locale object is
161 // an ID and setDefault seems to always succeed (regardless of the 163 // an ID and setDefault seems to always succeed (regardless of the
162 // presence of actual locale data). However, 164 // presence of actual locale data). However,
163 // it does not hurt to have it as a sanity check. 165 // it does not hurt to have it as a sanity check.
164 DCHECK(U_SUCCESS(error_code)); 166 DCHECK(U_SUCCESS(error_code));
165 g_icu_text_direction = UNKNOWN_DIRECTION; 167 subtle::Release_Store(
168 &g_icu_text_direction,
169 static_cast<subtle::Atomic32>(
170 GetTextDirectionForLocaleInStartUp(locale.getName())));
166 } 171 }
167 172
168 bool IsRTL() { 173 bool IsRTL() {
169 return ICUIsRTL(); 174 return ICUIsRTL();
170 } 175 }
171 176
172 bool ICUIsRTL() { 177 bool ICUIsRTL() {
173 if (g_icu_text_direction == UNKNOWN_DIRECTION) { 178 // Note: There is still a race if this is executed between the
174 const icu::Locale& locale = icu::Locale::getDefault(); 179 // icu::Locale::setDefault and the g_icu_text_direction store
175 g_icu_text_direction = GetTextDirectionForLocaleInStartUp(locale.getName()); 180 // that happens in SetICUDefaultLocale.
176 } 181 return static_cast<TextDirection>(
177 return g_icu_text_direction == RIGHT_TO_LEFT; 182 subtle::Acquire_Load(&g_icu_text_direction)) == RIGHT_TO_LEFT;
178 } 183 }
179 184
180 TextDirection GetTextDirectionForLocaleInStartUp(const char* locale_name) { 185 TextDirection GetTextDirectionForLocaleInStartUp(const char* locale_name) {
181 // Check for direction forcing. 186 // Check for direction forcing.
182 TextDirection forced_direction = GetForcedTextDirection(); 187 TextDirection forced_direction = GetForcedTextDirection();
183 if (forced_direction != UNKNOWN_DIRECTION) 188 if (forced_direction != UNKNOWN_DIRECTION)
184 return forced_direction; 189 return forced_direction;
185 190
186 // This list needs to be updated in alphabetical order if we add more RTL 191 // This list needs to be updated in alphabetical order if we add more RTL
187 // locales. 192 // locales.
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 begin == kRightToLeftOverride) 461 begin == kRightToLeftOverride)
457 ++begin_index; 462 ++begin_index;
458 size_t end_index = text.length() - 1; 463 size_t end_index = text.length() - 1;
459 if (text[end_index] == kPopDirectionalFormatting) 464 if (text[end_index] == kPopDirectionalFormatting)
460 --end_index; 465 --end_index;
461 return text.substr(begin_index, end_index - begin_index + 1); 466 return text.substr(begin_index, end_index - begin_index + 1);
462 } 467 }
463 468
464 } // namespace i18n 469 } // namespace i18n
465 } // namespace base 470 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698