michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/thread_local_storage.h" michael@0: michael@0: #include "base/logging.h" michael@0: michael@0: ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor) michael@0: : initialized_(false) { michael@0: Initialize(destructor); michael@0: } michael@0: michael@0: bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) { michael@0: DCHECK(!initialized_); michael@0: int error = pthread_key_create(&key_, destructor); michael@0: if (error) { michael@0: NOTREACHED(); michael@0: return false; michael@0: } michael@0: michael@0: initialized_ = true; michael@0: return true; michael@0: } michael@0: michael@0: void ThreadLocalStorage::Slot::Free() { michael@0: DCHECK(initialized_); michael@0: int error = pthread_key_delete(key_); michael@0: if (error) michael@0: NOTREACHED(); michael@0: initialized_ = false; michael@0: } michael@0: michael@0: void* ThreadLocalStorage::Slot::Get() const { michael@0: DCHECK(initialized_); michael@0: return pthread_getspecific(key_); michael@0: } michael@0: michael@0: void ThreadLocalStorage::Slot::Set(void* value) { michael@0: DCHECK(initialized_); michael@0: int error = pthread_setspecific(key_, value); michael@0: if (error) michael@0: NOTREACHED(); michael@0: }