-
Notifications
You must be signed in to change notification settings - Fork 0
/
HCPPGlobalObject.cpp
149 lines (130 loc) · 3.12 KB
/
HCPPGlobalObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
*为使C++全局变量按照一定顺序构造及析构,将有关联的全局变量放入一个文件中.
*一定要将被依赖的组件的全局变量放在依赖其的组件的全局变量的前面。
*/
//HCPPObjectPool
#include "HCPPObjectPool.h"
#include "map"
#include <mutex>
namespace HCPPObjectPoolGlobal
{
std::mutex HCPPObjectPoolLock;
std::map<std::string,HCPPObject *> HCPPObjectPool;
std::map<void *,std::string> HCPPObjectPoolDeleteHelper;
}
//HCPPObject
#include "HCPPObject.h"
#include <map>
#include <mutex>
#include <algorithm>
namespace HCPPObjectGlobal
{
class HCPPObjectInfo
{
std::thread::id _tid;
public:
HCPPObjectInfo();
HCPPObjectInfo(const HCPPObjectInfo&& other);
HCPPObjectInfo& operator =(const HCPPObjectInfo&& other);
HCPPObjectInfo(const HCPPObjectInfo& other);
HCPPObjectInfo& operator =(const HCPPObjectInfo& other);
std::thread::id& GetThreadId();
bool SetThreadId(std::thread::id _id, bool force_update = false);
};
HCPPObjectInfo::HCPPObjectInfo()
{
_tid = std::this_thread::get_id();
}
HCPPObjectInfo::HCPPObjectInfo(const HCPPObjectInfo&& other) :_tid(other._tid)
{
}
HCPPObjectInfo& HCPPObjectInfo::operator =(const HCPPObjectInfo&& other)
{
if (this == &other)
{
return *this;
}
_tid = other._tid;
return *this;
}
HCPPObjectInfo::HCPPObjectInfo(const HCPPObjectInfo& other) :_tid(other._tid)
{
}
HCPPObjectInfo& HCPPObjectInfo::operator =(const HCPPObjectInfo& other)
{
if (this == &other)
{
return *this;
}
_tid = other._tid;
return *this;
}
std::thread::id& HCPPObjectInfo::GetThreadId()
{
return _tid;
}
bool HCPPObjectInfo::SetThreadId(std::thread::id _id, bool force_update)
{
if (force_update)
{
_tid = _id;
return true;
}
if (_id == std::this_thread::get_id())
{
_tid = _id;
return true;
}
return false;
}
std::map<void *,HCPPObjectInfo> CPPHeapObjPool;
std::recursive_mutex CPPHeapObjPoolLock;
}
//HCPPThread
//HCPPTimer
#include "HCPPTimer.h"
#include <mutex>
namespace HCPPTimerGlobal
{
class TimerThread
{
HCPPThread *m_thread;
std::recursive_mutex m_lock;
public:
TimerThread();
~TimerThread();
HCPPThread* GetThread();
} g_timerthread;
TimerThread::TimerThread() :m_thread(NULL)
{
}
TimerThread::~TimerThread()
{
std::lock_guard<std::recursive_mutex> lock(m_lock);
if (m_thread != NULL)
{
m_thread->DeleteThread();
m_thread = NULL;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
HCPPThread* TimerThread::GetThread()
{
std::lock_guard<std::recursive_mutex> lock(m_lock);
if (m_thread == NULL)
{
m_thread = HCPPThread::New();
}
return m_thread;
}
}
//HCPPInit,为保证可靠性,此组件需要放在最后
#include <mutex>
#include <string>
#include <queue>
#include <functional>
namespace HCPPInitGlobal
{
std::recursive_mutex m_lock;
std::map<std::string,std::queue<std::function<void(void)>>> *m_map=NULL;
}