-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
276 lines (217 loc) · 7.42 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
#include <mutex>
#include <vector>
#include <unordered_map>
/* --------------------------------
MenuResourceBase
---------------------------------- */
class MenuResourceBase {
public:
MenuResourceBase() {};
virtual void load( std::string filename ) = 0;
virtual bool is_loaded() = 0;
virtual void update() = 0;
virtual void draw() = 0;
virtual void cleanup() = 0;
// Note: non-virtual destructor is OK here
~MenuResourceBase() {};
};
/* --------------------------------
Aliases
---------------------------------- */
typedef std::unordered_map< std::string,std::shared_ptr<MenuResourceBase> > resource_umap;
/* --------------------------------
ScreenWriter
---------------------------------- */
class ScreenWriter: public MenuResourceBase {
bool loaded_state;
public:
ScreenWriter() : loaded_state(false) {
std::cout << " ScreenWriter::ScreenWriter()\n";
}
~ScreenWriter() {
//cleanup();
std::cout << " ScreenWriter::~ScreenWriter()\n";
}
/* Virtual Defines */
void load( std::string filename ) {
if (!loaded_state) {
//std::cout << " ScreenWriter Loading Stuff!: " << filename << std::endl;
float sum(0.0f);
for (unsigned i = 0; i < 2000000000; ++i) {
sum +=1.0f;
}
//std::cout << " ScreenWriter Finished" << std::endl;
loaded_state=true; // Change to loaded state
}
};
/* Checks if it is loaded */
bool is_loaded() {
return loaded_state;
};
/* Update the class */
void update() {
if (loaded_state) {
}
};
/* Draw things */
void draw() {
if (loaded_state) {
}
};
/* Cleanup the class */
void cleanup() {
if (loaded_state) {
}
};
};
/* --------------------------------
ScreenWriter
---------------------------------- */
class ImageLoader: public MenuResourceBase {
bool loaded_state;
public:
ImageLoader() : loaded_state(false) {
std::cout << " ImageLoader::ImageLoader()\n";
}
~ImageLoader() {
cleanup();
std::cout << " ImageLoader::~ImageLoader()\n";
}
/* Virtual Defines */
void load( std::string filename ) {
if (!loaded_state) {
//std::cout << " ImageLoader Loading Stuff!: " << filename << std::endl;
//std::this_thread::sleep_for(std::chrono::seconds(5));
float sum(0.0f);
for (unsigned i = 0; i < 500000000; ++i) {
sum +=1.0f;
}
//std::cout << " ImageLoader Finished" << std::endl;
loaded_state=true; // Change to loaded state
}
};
/* Checks if it is loaded */
bool is_loaded() {
return loaded_state;
};
/* Update the class */
void update() {};
/* Draw things */
void draw() {};
/* Cleanup the class */
void cleanup() {};
};
/* --------------------------------
thr() Function
---------------------------------- */
void thr_loader(std::shared_ptr<MenuResourceBase> p,std::string filename) {
// shared use_count is incremented
{
//static std::mutex load_mutex;
//std::lock_guard<std::mutex> lk(load_mutex);
p.get()->load(filename);
}
}
/* --------------------------------
Menu Resource Manager
---------------------------------- */
class MenuResourceManager {
resource_umap umap;
std::vector<std::string> add_resourcestack;
std::vector<std::string> rmv_resourcestack;
public:
/* Constructor */
MenuResourceManager () {};
/* Destructor */
~MenuResourceManager () {umap.clear();};
/*------------------------------------
Function for Requesting Resources
-------------------------------------*/
std::shared_ptr<MenuResourceBase> requestResource( std::string ident ) {
unsigned n( ident.find_first_of(":") );
std::string type = ident.substr(0,n);
resource_umap::iterator it = umap.find(ident);
if ( it == umap.end() ) {
if ( type == std::string("screenwriter") ) {
umap.emplace(ident,std::make_shared<ScreenWriter>());
} else if ( type == std::string("imageloader") ) {
umap.emplace(ident,std::make_shared<ImageLoader >());
}
add_resourcestack.push_back(ident);
it = umap.find(ident);
return (*it).second;
}
return (*it).second;
};
/* ----------------------------------------------
This function loads all resources in the stack
------------------------------------------------*/
void manageResources () {
resource_umap::iterator it;
while (!add_resourcestack.empty()) {
it = umap.find(add_resourcestack.back());
add_resourcestack.pop_back();
std::thread (thr_loader,(*it).second,(*it).first.substr((*it).first.find_first_of(":")+1)).detach();
}
for (auto i(umap.begin());i != umap.end();++i) {
//std::cout << i->first << ":" << i->second.use_count() << std::endl;
if ( i->second.use_count() == 1 ) {
i->second.reset();
rmv_resourcestack.push_back(i->first);
}
}
while (!rmv_resourcestack.empty()) {
it = umap.find(rmv_resourcestack.back());
rmv_resourcestack.pop_back();
umap.erase(it);
}
};
/* Print loaded or not */
void printLoad() {
for (auto&& i : umap) {
if (i.second.get()->is_loaded()) {
std::cout << i.first << " LOADED!" << std::endl;
} else {
std::cout << i.first << " WAITING!" << std::endl;
};
}
std::cout << "nresources: " << umap.size() << std::endl;
};
};
/* --------------------------------
Main
---------------------------------- */
int main() {
std::string fname1("screenwriter:fontone.dat");
std::string fname2("screenwriter:fonttwo.dat");
std::string fname3("imageloader:imageone.dat");
std::string fname4("imageloader:imagetwo.dat");
std::string fname5("imageloader:imagethree.dat");
MenuResourceManager mrm;
std::shared_ptr<MenuResourceBase> p1 = mrm.requestResource(fname1);
std::shared_ptr<MenuResourceBase> p2 = mrm.requestResource(fname1);
std::shared_ptr<MenuResourceBase> p3 = mrm.requestResource(fname2);
std::shared_ptr<MenuResourceBase> p4 = mrm.requestResource(fname3);
std::shared_ptr<MenuResourceBase> p5 = mrm.requestResource(fname4);
std::shared_ptr<MenuResourceBase> p6 = mrm.requestResource(fname5);
unsigned int n( std::thread::hardware_concurrency() );
std::cout << n << " concurrent threads are supported.\n";
unsigned N(0);
while (true) { // Simulate a graphix engine loop
mrm.manageResources();
std::cout << "Hi, I'm the main thread, waiting for my children threads to finish!\n";
std::cout << "COUNT: " << N << "\n";
std::this_thread::sleep_for( std::chrono::milliseconds(100) );
mrm.printLoad();
if ( p1 && p1.get()->is_loaded() )
p1.reset();
if ( p2 && p2.get()->is_loaded() )
p2.reset();
++N;
}
std::cout << "Hi, I'm Main after the threads finished loading!\n";
}