OLD | NEW |
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/threading/thread_local_storage.h" | 5 #include "base/threading/thread_local_storage.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 | 10 |
11 ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor) | 11 ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor) { |
12 : initialized_(false), | 12 initialized_ = false; |
13 key_(0) { | 13 key_ = 0; |
14 Initialize(destructor); | 14 Initialize(destructor); |
15 } | 15 } |
16 | 16 |
17 bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) { | 17 bool ThreadLocalStorage::StaticSlot::Initialize(TLSDestructorFunc destructor) { |
18 DCHECK(!initialized_); | 18 DCHECK(!initialized_); |
19 int error = pthread_key_create(&key_, destructor); | 19 int error = pthread_key_create(&key_, destructor); |
20 if (error) { | 20 if (error) { |
21 NOTREACHED(); | 21 NOTREACHED(); |
22 return false; | 22 return false; |
23 } | 23 } |
24 | 24 |
25 initialized_ = true; | 25 initialized_ = true; |
26 return true; | 26 return true; |
27 } | 27 } |
28 | 28 |
29 void ThreadLocalStorage::Slot::Free() { | 29 void ThreadLocalStorage::StaticSlot::Free() { |
30 DCHECK(initialized_); | 30 DCHECK(initialized_); |
31 int error = pthread_key_delete(key_); | 31 int error = pthread_key_delete(key_); |
32 if (error) | 32 if (error) |
33 NOTREACHED(); | 33 NOTREACHED(); |
34 initialized_ = false; | 34 initialized_ = false; |
35 } | 35 } |
36 | 36 |
37 void* ThreadLocalStorage::Slot::Get() const { | 37 void* ThreadLocalStorage::StaticSlot::Get() const { |
38 DCHECK(initialized_); | 38 DCHECK(initialized_); |
39 return pthread_getspecific(key_); | 39 return pthread_getspecific(key_); |
40 } | 40 } |
41 | 41 |
42 void ThreadLocalStorage::Slot::Set(void* value) { | 42 void ThreadLocalStorage::StaticSlot::Set(void* value) { |
43 DCHECK(initialized_); | 43 DCHECK(initialized_); |
44 int error = pthread_setspecific(key_, value); | 44 int error = pthread_setspecific(key_, value); |
45 if (error) | 45 if (error) |
46 NOTREACHED(); | 46 NOTREACHED(); |
47 } | 47 } |
48 | 48 |
49 } // namespace base | 49 } // namespace base |
OLD | NEW |