-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.cpp
414 lines (328 loc) · 12.3 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include <iostream>
#include <thread>
#include <mutex>
#include <shared_mutex>
#include <iterator>
#include <iomanip>
#include <array>
#include <random>
#include <nes/pipe.hpp>
#include <nes/shared_library.hpp>
#include <nes/process.hpp>
#include <nes/shared_memory.hpp>
#include <nes/named_mutex.hpp>
#include <nes/semaphore.hpp>
#include <nes/named_semaphore.hpp>
#include <nes/hash.hpp>
#include <nes/thread_pool.hpp>
/*
#if defined(NES_WIN32_SHARED_LIBRARY)
#define NES_EXAMPLE_EXPORT __declspec(dllexport)
#elif defined(NES_POSIX_SHARED_LIBRARY) && defined(__GNUC__)
#define NES_EXAMPLE_EXPORT __attribute__((visibility("default")))
#else
#define NES_EXAMPLE_EXPORT //The example may not work properly in this case.
#endif
#if defined(NES_WIN32_PROCESS)
constexpr const char* other_path{"not_enough_standards_other.exe"};
#elif defined(NES_POSIX_PROCESS)
constexpr const char* other_path{"not_enough_standards_other"};
#endif
extern "C" NES_EXAMPLE_EXPORT void foo(int i);
extern "C" NES_EXAMPLE_EXPORT void foo(int i)
{
std::cout << "Hello " << i << "!" << std::endl;
}
static void shared_library_example()
{
nes::shared_library lib{nes::load_current};
auto foo_func = lib.load<void(int)>("foo");
if(foo_func)
foo_func(42);
}
enum class data_type : std::uint32_t
{
uint32 = 1,
float64,
string
};
static void a_thread(nes::basic_pipe_istream<char>& is) noexcept
{
while(is)
{
data_type type{};
is.read(reinterpret_cast<char*>(&type), sizeof(data_type));
if(type == data_type::uint32)
{
std::uint32_t value{};
is.read(reinterpret_cast<char*>(&value), sizeof(std::uint32_t));
std::cout << "Received an unsigned integer: " << value << std::endl;
}
else if(type == data_type::float64)
{
double value{};
is.read(reinterpret_cast<char*>(&value), sizeof(double));
std::cout << "Received a double: " << value << std::endl;
}
else if(type == data_type::string)
{
std::uint64_t size{};
is.read(reinterpret_cast<char*>(&size), sizeof(std::uint64_t));
std::string str{};
str.resize(size);
is.read(reinterpret_cast<char*>(std::data(str)), static_cast<std::streamsize>(size));
std::cout << "Received a string: " << str << std::endl;
}
}
}
static void pipe_example()
{
auto&&[is, os] = nes::make_anonymous_pipe();
std::thread thread{a_thread, std::ref(is)};
for(std::uint32_t i{1}; i < 20; ++i)
{
if((i % 3) == 0)
{
const data_type type{data_type::uint32};
os.write(reinterpret_cast<const char*>(&type), sizeof(data_type));
os.write(reinterpret_cast<const char*>(&i), sizeof(std::uint32_t));
}
else if((i % 3) == 1)
{
const data_type type{data_type::float64};
os.write(reinterpret_cast<const char*>(&type), sizeof(data_type));
const double value{1.0 / i};
os.write(reinterpret_cast<const char*>(&value), sizeof(double));
}
else if((i % 3) == 2)
{
const data_type type{data_type::string};
os.write(reinterpret_cast<const char*>(&type), sizeof(data_type));
const std::string str{"Hello " + std::to_string(i) + "!"};
const std::uint64_t size{std::size(str)};
os.write(reinterpret_cast<const char*>(&size), sizeof(std::uint64_t));
os.write(std::data(str), static_cast<std::streamsize>(size));
}
}
os.close();
if(thread.joinable())
thread.join();
}
static void another_thread(const std::array<std::uint64_t, 8>& data, nes::semaphore& semaphore)
{
const auto tp1{std::chrono::high_resolution_clock::now()};
for(std::size_t i{}; i < 8; ++i)
{
semaphore.acquire();
const auto elapsed_time{std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - tp1)};
std::cout << "Value " << i << " ready after " << elapsed_time.count() << "ms: " << data[i] << std::endl;
}
}
static void semaphore_example()
{
std::array<std::uint64_t, 8> data{0, 1};
nes::semaphore semaphore{2};
std::thread thread{another_thread, std::cref(data), std::ref(semaphore)};
for(std::size_t i{2}; i < 8; ++i)
{
std::this_thread::sleep_for(std::chrono::milliseconds{250});
data[i] = i * i;
semaphore.release();
}
if(thread.joinable())
thread.join();
}
static void named_pipe_example()
{
nes::process other{other_path, std::vector<std::string>{"named pipe example"}, nes::process_options::grab_stdout};
nes::pipe_ostream os{"nes_example_pipe"};
if(!os)
throw std::runtime_error{"Failed to open pipe."};
for(std::uint32_t i{1}; i < 20; ++i)
{
if((i % 3) == 0)
{
const data_type type{data_type::uint32};
os.write(reinterpret_cast<const char*>(&type), sizeof(data_type));
os.write(reinterpret_cast<const char*>(&i), sizeof(std::uint32_t));
}
else if((i % 3) == 1)
{
const data_type type{data_type::float64};
os.write(reinterpret_cast<const char*>(&type), sizeof(data_type));
const double value{1.0 / i};
os.write(reinterpret_cast<const char*>(&value), sizeof(double));
}
else if((i % 3) == 2)
{
const data_type type{data_type::string};
os.write(reinterpret_cast<const char*>(&type), sizeof(data_type));
const std::string str{"Hello " + std::to_string(i) + "!"};
const std::uint64_t size{std::size(str)};
os.write(reinterpret_cast<const char*>(&size), sizeof(std::uint64_t));
os.write(std::data(str), static_cast<std::streamsize>(size));
}
}
os.close();
std::cout << other.stdout_stream().rdbuf() << std::endl;
if(other.joinable())
other.join();
std::cout << "Other process ended with code: " << other.return_code() << std::endl;
}
static void process_example()
{
std::cout << "Current process has id " << nes::this_process::get_id() << " and its current directory is \"" << nes::this_process::working_directory() << "\"" << std::endl;
nes::process other{other_path, {"Hey!", "\\\"12\"\"\\\\\\", "\\42\\", "It's \"me\"!"}, nes::process_options::grab_stdout};
std::cout << other.stdout_stream().rdbuf() << std::endl;
if(other.joinable())
other.join();
std::cout << "Other process ended with code: " << other.return_code() << std::endl;
}
static void process_kill_example()
{
nes::process other{other_path, std::vector<std::string>{"process kill example"}, nes::process_options::grab_stdout};
std::this_thread::sleep_for(std::chrono::seconds{3});
other.kill();
std::cout << other.stdout_stream().rdbuf() << std::endl;
std::cout << "Shut up." << std::endl;
std::cout << "Other process ended with code: " << other.return_code() << std::endl;
}
static void shared_memory_example()
{
nes::shared_memory memory{"nes_example_shared_memory", sizeof(std::uint64_t)};
auto value{memory.map<std::uint64_t>(0)};
*value = 42;
nes::process other{other_path, std::vector<std::string>{"shared memory example"}, nes::process_options::grab_stdout};
std::cout << other.stdout_stream().rdbuf() << std::endl;
if(other.joinable())
other.join();
std::cout << "Other process ended with code: " << other.return_code() << std::endl;
std::cout << "The value in shared memory is: " << *value << std::endl;
}
static void named_mutex_example()
{
nes::named_mutex mutex{"nes_example_named_mutex"};
std::unique_lock lock{mutex};
nes::process other{other_path, std::vector<std::string>{"named mutex example"}, nes::process_options::grab_stdout};
std::this_thread::sleep_for(std::chrono::milliseconds{500});
lock.unlock();
std::cout << other.stdout_stream().rdbuf() << std::endl;
if(other.joinable())
other.join();
}
static void timed_named_mutex_example()
{
nes::timed_named_mutex mutex{"nes_example_timed_named_mutex"};
std::unique_lock lock{mutex};
nes::process other{other_path, std::vector<std::string>{"timed named mutex example"}, nes::process_options::grab_stdout};
std::this_thread::sleep_for(std::chrono::milliseconds{1000});
lock.unlock();
std::cout << other.stdout_stream().rdbuf() << std::endl;
if(other.joinable())
other.join();
}
static void named_semaphore_example()
{
nes::named_semaphore semaphore{"nes_example_named_semaphore"};
nes::process other{other_path, std::vector<std::string>{"named semaphore example"}, nes::process_options::grab_stdout};
for(std::size_t i{}; i < 8; ++i)
{
std::this_thread::sleep_for(std::chrono::milliseconds{100});
semaphore.release();
}
std::cout << other.stdout_stream().rdbuf() << std::endl;
if(other.joinable())
other.join();
}
static void hash_example()
{
nes::hash<std::variant<std::string_view, double>> hash{};
std::cout << nes::from_hash_value<std::uint64_t>(hash("Hello world!")) << std::endl;
}
using hrc = std::chrono::high_resolution_clock;
*/
static void thread_pool_example()
{
static constexpr std::size_t buffer_size{32};
//Some buffers
std::array<std::uint32_t, buffer_size> input{};
std::array<std::uint32_t, buffer_size> temp{};
std::array<std::uint32_t, buffer_size> output{};
const auto print_buffers = [&input, &temp, &output]()
{
const auto print_buffer = [](const std::array<std::uint32_t, buffer_size>& buffer)
{
for(auto value : buffer)
{
std::cout << value << ",";
}
};
std::cout << "input: ";
print_buffer(input);
std::cout << "\ntemp: ";
print_buffer(temp);
std::cout << "\noutput: ";
print_buffer(output);
std::cout << std::endl;
};
//Fill the buffer with random values
std::mt19937 rng{std::random_device{}()};
std::uniform_int_distribution<std::uint32_t> dist{1, 9};
for(auto& input_value : input)
{
input_value = dist(rng);
}
//The task builder
nes::task_builder builder{};
builder.dispatch(buffer_size, 1, 1, [&input, &temp](std::uint32_t x, std::uint32_t y [[maybe_unused]], std::uint32_t z [[maybe_unused]])
{
temp[x] = input[x] * 2u;
});
nes::task_checkpoint checkpoint{builder.checkpoint()};
nes::task_fence fence{builder.fence()};
builder.dispatch(buffer_size, 1, 1, [&input, &temp, &output](std::uint32_t x, std::uint32_t y [[maybe_unused]], std::uint32_t z [[maybe_unused]])
{
for(auto value : temp)
{
output[x] += (value + input[x]);
}
});
//Create a thread pool to run our task list.
nes::thread_pool thread_pool{};
std::cout << "Initial state:" << std::endl;
print_buffers();
std::cout << "Launching first the work..." << std::endl;
std::future<nes::task_list> future{thread_pool.push(builder.build())};
std::cout << "Work started..." << std::endl;
checkpoint.wait();
std::cout << "First dispatch done:" << std::endl;
print_buffers();
std::cout << "Launching second dispatch..." << std::endl;
fence.signal();
std::cout << "Second dispatch started..." << std::endl;
future.wait();
std::cout << "Second dispatch done:" << std::endl;
print_buffers();
}
int main()
{
try
{/*
shared_library_example();
pipe_example();
semaphore_example();
process_example();
process_kill_example();
named_pipe_example();
shared_memory_example();
named_mutex_example();
timed_named_mutex_example();
named_semaphore_example();
hash_example();*/
thread_pool_example();
}
catch(const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}