Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2008 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2009 Jian Li <jianli@chromium.org> | 3 * Copyright (C) 2009 Jian Li <jianli@chromium.org> |
| 4 * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com> | 4 * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com> |
| 5 * | 5 * |
| 6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
| 8 * are met: | 8 * are met: |
| 9 * | 9 * |
| 10 * 1. Redistributions of source code must retain the above copyright | 10 * 1. Redistributions of source code must retain the above copyright |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 261 inline bool ThreadSpecific<T>::isSet() { | 261 inline bool ThreadSpecific<T>::isSet() { |
| 262 return !!get(); | 262 return !!get(); |
| 263 } | 263 } |
| 264 | 264 |
| 265 template <typename T> | 265 template <typename T> |
| 266 inline ThreadSpecific<T>::operator T*() { | 266 inline ThreadSpecific<T>::operator T*() { |
| 267 T* offThreadPtr; | 267 T* offThreadPtr; |
| 268 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) | 268 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) |
| 269 // TLS is fast on these platforms. | 269 // TLS is fast on these platforms. |
| 270 // TODO(csharrison): Qualify this statement for Android. | 270 // TODO(csharrison): Qualify this statement for Android. |
| 271 const bool mainThreadAlwaysChecksTLS = true; | |
| 271 T** ptr = &offThreadPtr; | 272 T** ptr = &offThreadPtr; |
| 272 offThreadPtr = static_cast<T*>(get()); | 273 offThreadPtr = static_cast<T*>(get()); |
| 273 #else | 274 #else |
| 275 const bool mainThreadAlwaysChecksTLS = false; | |
| 274 T** ptr = &m_mainThreadStorage; | 276 T** ptr = &m_mainThreadStorage; |
| 275 if (UNLIKELY(internal::mayNotBeMainThread())) { | 277 if (UNLIKELY(mayNotBeMainThread())) { |
| 276 offThreadPtr = static_cast<T*>(get()); | 278 offThreadPtr = static_cast<T*>(get()); |
| 277 ptr = &offThreadPtr; | 279 ptr = &offThreadPtr; |
| 278 } | 280 } |
| 279 #endif | 281 #endif |
| 280 // Set up thread-specific value's memory pointer before invoking constructor, | 282 // Set up thread-specific value's memory pointer before invoking constructor, |
| 281 // in case any function it calls needs to access the value, to avoid | 283 // in case any function it calls needs to access the value, to avoid |
| 282 // recursion. | 284 // recursion. |
| 283 if (UNLIKELY(!*ptr)) { | 285 if (UNLIKELY(!*ptr)) { |
| 284 *ptr = static_cast<T*>(Partitions::fastZeroedMalloc( | 286 *ptr = static_cast<T*>(Partitions::fastZeroedMalloc( |
| 285 sizeof(T), WTF_HEAP_PROFILER_TYPE_NAME(T))); | 287 sizeof(T), WTF_HEAP_PROFILER_TYPE_NAME(T))); |
| 288 | |
| 289 // Even if we didn't realize we're on the main thread, we might still be. | |
| 290 // We need to double-check so that |m_mainThreadStorage| is populated. | |
| 291 if (!mainThreadAlwaysChecksTLS && UNLIKELY(ptr != &m_mainThreadStorage) && | |
| 292 isMainThread()) { | |
| 293 m_mainThreadStorage = *ptr; | |
|
haraken
2017/03/06 19:21:36
Or is there any option to populate m_mainThreadSto
jbroman
2017/03/06 20:28:09
We have no registry of all ThreadSpecific objects,
haraken
2017/03/06 20:46:42
Ah, sorry, you're totally right. Given that Thread
| |
| 294 } | |
| 295 | |
| 286 set(*ptr); | 296 set(*ptr); |
| 287 new (NotNull, *ptr) T; | 297 new (NotNull, *ptr) T; |
| 288 } | 298 } |
| 289 return *ptr; | 299 return *ptr; |
| 290 } | 300 } |
| 291 | 301 |
| 292 template <typename T> | 302 template <typename T> |
| 293 inline T* ThreadSpecific<T>::operator->() { | 303 inline T* ThreadSpecific<T>::operator->() { |
| 294 return operator T*(); | 304 return operator T*(); |
| 295 } | 305 } |
| 296 | 306 |
| 297 template <typename T> | 307 template <typename T> |
| 298 inline T& ThreadSpecific<T>::operator*() { | 308 inline T& ThreadSpecific<T>::operator*() { |
| 299 return *operator T*(); | 309 return *operator T*(); |
| 300 } | 310 } |
| 301 | 311 |
| 302 } // namespace WTF | 312 } // namespace WTF |
| 303 | 313 |
| 304 using WTF::ThreadSpecific; | 314 using WTF::ThreadSpecific; |
| 305 | 315 |
| 306 #endif // WTF_ThreadSpecific_h | 316 #endif // WTF_ThreadSpecific_h |
| OLD | NEW |