ANNA Suite  2020b
Multipurpose development suite for Telco applications
ThreadData.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_core_mt_ThreadData_hpp
10 #define anna_core_mt_ThreadData_hpp
11 
12 #ifdef _MT
13 #include <map>
14 #include <anna/core/mt/NRMutex.hpp>
15 #include <anna/core/functions.hpp>
17 #endif
18 
19 namespace anna {
20 
27 template <typename T> class ThreadData {
28 public:
33  ThreadData(const char* name) : a_name(name) {;}
34 
40  T& get() {
41 #ifndef _MT
42  return a_data;
43 #else
44  Guard guard(a_mutex, "anna::ThreadData");
45  const int index = (int) functions::getCurrentThread();
46  data_iterator ii = a_datas.find(index);
47  T* result = NULL;
48 
49  if(ii == a_datas.end()) {
50  result = new T;
51  a_datas.insert(value_type(index, result));
52  } else
53  result = ii->second;
54 
55  LOGDEBUG(
56  std::string msg("anna::ThreadData { Name: ");
57  msg += a_name;
58  msg += " | ";
60  msg += " }";
62  );
63  return *result;
64 #endif
65  }
66 
67 private:
68 #ifndef _MT
69  T a_data;
70  const char* a_name;
71 #else
72  typedef typename std::map <int, T*> container;
73  typedef typename container::iterator data_iterator;
74  typedef typename container::value_type value_type;
75  NRMutex a_mutex;
76  container a_datas;
77  const char* a_name;
78 #endif
79 };
80 }
81 
82 #endif
83 
#define anna_ptrnumber_cast(pointer)
Definition: defines.hpp:118
static void debug(const std::string &text, const char *fromFile, const int fromLine)
Definition: Logger.hpp:305
Definition: NRMutex.hpp:39
ThreadData(const char *name)
Definition: ThreadData.hpp:33
Definition: ThreadData.hpp:27
#define LOGDEBUG(a)
Definition: defines.hpp:64
static std::string asHexString(const int number)
Definition: app.hpp:12
Definition: Guard.hpp:35
static pthread_t getCurrentThread()
#define ANNA_FILE_LOCATION
Definition: defines.hpp:23