Main Page · Modules · All Classes · Class Hierarchy
MCThreadLocalData.hpp
1 /*
2  * This file is part of the AiBO+ project
3  *
4  * Copyright (C) 2005-2016 Csaba Kertész (csaba.kertesz@gmail.com)
5  *
6  * AiBO+ is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * AiBO+ is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
19  *
20  */
21 
22 #pragma once
23 
24 #include <cstddef>
25 
26 #if defined(__MINGW32__) || defined(AP_COVERITY) || (!defined(__AIBO_BUILD__) && defined(__unix__) && !defined(__LP64__))
27 #include <boost/thread/tss.hpp>
28 #elif !defined(__AIBO_BUILD__) && !defined(__ANDROID__) && !defined(__APPLE__)
29 
30 // This hack is needed for testing
31 #if defined(protected)
32 #undef protected
33 #undef private
34 #include <folly/ThreadLocal.h> // krazy:exclude=includes
35 #define protected public
36 #define private public
37 #else
38 #include <folly/ThreadLocal.h>
39 #endif
40 
41 #endif
42 
51 #if defined(__MINGW32__) || defined(AP_COVERITY) || (!defined(__AIBO_BUILD__) && defined(__unix__) && !defined(__LP64__))
52 template <typename T>
53 class MCThreadLocalData : public boost::thread_specific_ptr<T>
54 {
55 public:
56  explicit MCThreadLocalData(bool cleanup = false) : boost::thread_specific_ptr<T>(), Cleanup(cleanup)
57  { }
58 
60  {
61  // The objects are only destroyed if the thread local storage is released finally
62  if (!Cleanup)
63  boost::thread_specific_ptr<T>::release();
64  }
65 
66 private:
68  bool Cleanup;
69 };
70 #elif !defined(__AIBO_BUILD__) && !defined(__ANDROID__) && !defined(__APPLE__)
71 template <typename T>
72 class MCThreadLocalData : public folly::ThreadLocalPtr<T>
73 {
74 public:
75  explicit MCThreadLocalData(bool cleanup = false) : folly::ThreadLocalPtr<T>(), Cleanup(cleanup)
76  { }
77 
79  {
80  // The objects are only destroyed if the thread local storage is released finally
81  if (!Cleanup)
82  folly::ThreadLocalPtr<T>::release();
83  }
84 
85 private:
87  bool Cleanup;
88 };
89 #else
90 template <typename T>
92 {
93 public:
94  explicit MCThreadLocalData(bool) : DataPtr(nullptr)
95  {
96  }
97 
99  {
100  delete DataPtr;
101  DataPtr = nullptr;
102  }
103 
104  T* get() const
105  {
106  return DataPtr;
107  }
108 
109  T* operator->() const
110  {
111  return DataPtr;
112  }
113 
114  T& operator*() const
115  {
116  return *DataPtr;
117  }
118 
119  void reset(T* new_data_ptr)
120  {
121  delete DataPtr;
122  DataPtr = new_data_ptr;
123  }
124 
125  T* release()
126  {
127  T* TempPtr = DataPtr;
128 
129  DataPtr = nullptr;
130  return TempPtr;
131  }
132 
133  T* DataPtr;
134 };
135 #endif
136 
bool Cleanup
Whenever the thread local data is registered for cleanup.
A wrapper class to cover boost::thread_specific_ptr/folly::ThreadLocal API on certain targets...