blob: f40627d9536925e38650f116063acf0437fe7b00 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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.com4be2f9b2008-08-08 05:12:28 +09004
5#include "base/thread_local_storage.h"
6
7#include "base/logging.h"
8
evanm@google.comf26fd3a2008-08-21 07:54:52 +09009ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor)
10 : initialized_(false) {
11 Initialize(destructor);
12}
13
14bool 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
26void ThreadLocalStorage::Slot::Free() {
27 DCHECK(initialized_);
28 int error = pthread_key_delete(key_);
evanm@google.comc6857672008-08-21 07:11:47 +090029 if (error)
evanm@google.com78257d42008-08-21 06:47:40 +090030 NOTREACHED();
evanm@google.comf26fd3a2008-08-21 07:54:52 +090031 initialized_ = false;
evanm@google.com78257d42008-08-21 06:47:40 +090032}
33
evanm@google.comf26fd3a2008-08-21 07:54:52 +090034void* ThreadLocalStorage::Slot::Get() const {
35 DCHECK(initialized_);
36 return pthread_getspecific(key_);
avi@google.com4be2f9b2008-08-08 05:12:28 +090037}
38
evanm@google.comf26fd3a2008-08-21 07:54:52 +090039void ThreadLocalStorage::Slot::Set(void* value) {
40 DCHECK(initialized_);
41 int error = pthread_setspecific(key_, value);
42 if (error)
43 NOTREACHED();
avi@google.com4be2f9b2008-08-08 05:12:28 +090044}
license.botf003cfe2008-08-24 09:55:55 +090045