license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
avi@google.com | 4be2f9b | 2008-08-08 05:12:28 +0900 | [diff] [blame] | 4 | |
| 5 | #include "base/thread_local_storage.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 9 | ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor) |
| 10 | : initialized_(false) { |
| 11 | Initialize(destructor); |
| 12 | } |
| 13 | |
| 14 | bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) { |
| 15 | DCHECK(!initialized_); |
| 16 | int error = pthread_key_create(&key_, destructor); |
| 17 | if (error) { |
| 18 | NOTREACHED(); |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | initialized_ = true; |
| 23 | return true; |
| 24 | } |
| 25 | |
| 26 | void ThreadLocalStorage::Slot::Free() { |
| 27 | DCHECK(initialized_); |
| 28 | int error = pthread_key_delete(key_); |
evanm@google.com | c685767 | 2008-08-21 07:11:47 +0900 | [diff] [blame] | 29 | if (error) |
evanm@google.com | 78257d4 | 2008-08-21 06:47:40 +0900 | [diff] [blame] | 30 | NOTREACHED(); |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 31 | initialized_ = false; |
evanm@google.com | 78257d4 | 2008-08-21 06:47:40 +0900 | [diff] [blame] | 32 | } |
| 33 | |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 34 | void* ThreadLocalStorage::Slot::Get() const { |
| 35 | DCHECK(initialized_); |
| 36 | return pthread_getspecific(key_); |
avi@google.com | 4be2f9b | 2008-08-08 05:12:28 +0900 | [diff] [blame] | 37 | } |
| 38 | |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 39 | void ThreadLocalStorage::Slot::Set(void* value) { |
| 40 | DCHECK(initialized_); |
| 41 | int error = pthread_setspecific(key_, value); |
| 42 | if (error) |
| 43 | NOTREACHED(); |
avi@google.com | 4be2f9b | 2008-08-08 05:12:28 +0900 | [diff] [blame] | 44 | } |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 45 | |