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