forked from UBC-Thunderbots/Software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coroutine_test_examples.cpp
601 lines (468 loc) · 15.5 KB
/
coroutine_test_examples.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#include <boost/bind.hpp>
#include <boost/coroutine2/all.hpp>
#include <functional>
#include <iostream>
using VoidCoroutine = boost::coroutines2::coroutine<void>;
using TestFunction = std::function<void(std::shared_ptr<int>, VoidCoroutine::push_type&)>;
class FunctionMemberObject
{
public:
explicit FunctionMemberObject(TestFunction func, std::shared_ptr<int> data)
: func(func),
coroutine_sequence(
boost::bind(&FunctionMemberObject::executeWrapper, this, _1, data))
{
}
bool execute()
{
if (coroutine_sequence)
{
coroutine_sequence();
}
return !static_cast<bool>(coroutine_sequence);
}
private:
void executeWrapper(VoidCoroutine::push_type& yield, std::shared_ptr<int> data)
{
yield();
func(data, yield);
}
// The TestFunction is stored in the same memory (stack or heap) as the allocated
// instance of this class. The coroutine accesses this member variable directly
// from "inside" the coroutine. On the other hand, the shared_ptr to the data
// is passed into the coroutine right at the start.
VoidCoroutine::pull_type coroutine_sequence;
TestFunction func;
};
class DataMemberObject
{
public:
explicit DataMemberObject(TestFunction func, std::shared_ptr<int> data)
: data(data),
coroutine_sequence(
boost::bind(&DataMemberObject::executeWrapper, this, _1, func))
{
}
bool execute()
{
if (coroutine_sequence)
{
coroutine_sequence();
}
return !static_cast<bool>(coroutine_sequence);
}
private:
void executeWrapper(VoidCoroutine::push_type& yield, TestFunction func)
{
yield();
func(data, yield);
}
// The shared_ptr to the data is stored in the same memory (stack or heap) as the
// allocated instance of this class. The coroutine accesses this member variable
// directly from "inside" the coroutine. On the other hand, the TestFunction is passed
// into the coroutine right at the start.
VoidCoroutine::pull_type coroutine_sequence;
std::shared_ptr<int> data;
};
class NoMemberObject
{
public:
explicit NoMemberObject(TestFunction validation_function, std::shared_ptr<int> data)
: coroutine_sequence(boost::bind(&NoMemberObject::executeWrapper, this, _1, data,
validation_function))
{
}
bool execute()
{
if (coroutine_sequence)
{
coroutine_sequence();
}
return !static_cast<bool>(coroutine_sequence);
}
private:
void executeWrapper(VoidCoroutine::push_type& yield, std::shared_ptr<int> data,
TestFunction func)
{
yield();
func(data, yield);
}
// This class passes the TestFunction and shared_ptr to the data into the
// coroutine right at the start, so they are not stored in the same memory
// as the allocated object.
VoidCoroutine::pull_type coroutine_sequence;
};
auto print_data_func = [](std::shared_ptr<int> data, VoidCoroutine::push_type& yield) {
while (true)
{
if (data)
{
std::cout << *data << std::endl;
}
else
{
std::cout << "null" << std::endl;
}
yield();
}
};
void variablesOnStackNoMove()
{
std::cout << std::endl << std::endl;
std::cout << "Test on the stack with no move" << std::endl;
auto data = std::make_shared<int>(99);
FunctionMemberObject fmo(print_data_func, data);
DataMemberObject dmo(print_data_func, data);
NoMemberObject nmo(print_data_func, data);
fmo.execute();
dmo.execute();
nmo.execute();
*data = 5;
fmo.execute();
dmo.execute();
nmo.execute();
// Output
// 99
// 99
// 99
// 5
// 5
// 5
}
void variablesOnHeapNoMove()
{
std::cout << std::endl << std::endl;
std::cout << "Test on the heap with no move" << std::endl;
auto data = std::make_shared<int>(99);
auto fmo_ptr = std::make_unique<FunctionMemberObject>(print_data_func, data);
auto dmo_ptr = std::make_unique<DataMemberObject>(print_data_func, data);
auto nmo_ptr = std::make_unique<NoMemberObject>(print_data_func, data);
fmo_ptr->execute();
dmo_ptr->execute();
nmo_ptr->execute();
*data = 8;
fmo_ptr->execute();
dmo_ptr->execute();
nmo_ptr->execute();
// Output
// 99
// 99
// 99
// 8
// 8
// 8
}
void moveFromStackToStack()
{
std::cout << std::endl << std::endl;
std::cout << "Test on the stack with moving variables" << std::endl;
auto data = std::make_shared<int>(99);
FunctionMemberObject fmo_test(print_data_func, data);
DataMemberObject dmo_test(print_data_func, data);
NoMemberObject nmo_test(print_data_func, data);
fmo_test.execute();
dmo_test.execute();
nmo_test.execute();
*data = -14;
FunctionMemberObject fmo_test_move = std::move(fmo_test);
DataMemberObject dmo_test_move = std::move(dmo_test);
NoMemberObject nmo_test_move = std::move(nmo_test);
fmo_test_move.execute();
dmo_test_move.execute();
nmo_test_move.execute();
*data = -15;
fmo_test_move.execute();
dmo_test_move.execute();
nmo_test_move.execute();
// Output
// 99
// 99
// 99
// -14
// -14
// -14
// -15
// -15
// -15
}
void moveFromHeapToHeap()
{
std::cout << std::endl << std::endl;
std::cout << "Test on the heap with moving variables" << std::endl;
auto data = std::make_shared<int>(99);
auto fmo_ptr = std::make_unique<FunctionMemberObject>(print_data_func, data);
auto dmo_ptr = std::make_unique<DataMemberObject>(print_data_func, data);
auto nmo_ptr = std::make_unique<NoMemberObject>(print_data_func, data);
fmo_ptr->execute();
dmo_ptr->execute();
nmo_ptr->execute();
*data = 3;
auto fmo_ptr_move = std::move(fmo_ptr);
auto dmo_ptr_move = std::move(dmo_ptr);
auto nmo_ptr_move = std::move(nmo_ptr);
fmo_ptr_move->execute();
dmo_ptr_move->execute();
nmo_ptr_move->execute();
*data = 4;
fmo_ptr_move->execute();
dmo_ptr_move->execute();
nmo_ptr_move->execute();
// Output
// 99
// 99
// 99
// 3
// 3
// 3
// 4
// 4
// 4
}
void moveFromStackToHeapFMO()
{
std::cout << std::endl << std::endl;
std::cout << "Test moving from stack to heap - FunctionMemberObject" << std::endl;
auto data = std::make_shared<int>(99);
FunctionMemberObject fmo(print_data_func, data);
fmo.execute();
*data = 45;
auto fmo_heap = std::unique_ptr<FunctionMemberObject>();
*fmo_heap = std::move(fmo);
fmo_heap->execute();
*data = 83;
fmo_heap->execute();
// Output
// 99
// (printouts stop working after this point)
}
void moveFromStackToHeapDMO()
{
std::cout << std::endl << std::endl;
std::cout << "Test moving from stack to heap - DataMemberObject" << std::endl;
auto data = std::make_shared<int>(99);
DataMemberObject dmo(print_data_func, data);
dmo.execute();
*data = 45;
auto dmo_heap = std::unique_ptr<DataMemberObject>();
*dmo_heap = std::move(dmo);
dmo_heap->execute();
*data = 83;
dmo_heap->execute();
// Output
// 99
// (printouts stop working after this point)
}
void moveFromStackToHeapNMO()
{
std::cout << std::endl << std::endl;
std::cout << "Test moving from stack to heap - NoMemberObject" << std::endl;
auto data = std::make_shared<int>(99);
NoMemberObject nmo(print_data_func, data);
nmo.execute();
*data = 45;
auto nmo_heap = std::unique_ptr<NoMemberObject>();
*nmo_heap = std::move(nmo);
nmo_heap->execute();
*data = 83;
nmo_heap->execute();
// Output
// 99
// (printouts stop working after this point)
}
void moveFromHeapToStack()
{
std::cout << std::endl << std::endl;
std::cout << "Test moving from heap to stack" << std::endl;
auto data = std::make_shared<int>(99);
auto fmo_heap = std::make_unique<FunctionMemberObject>(print_data_func, data);
auto dmo_heap = std::make_unique<DataMemberObject>(print_data_func, data);
auto nmo_heap = std::make_unique<NoMemberObject>(print_data_func, data);
std::optional<FunctionMemberObject> fmo;
std::optional<DataMemberObject> dmo;
std::optional<NoMemberObject> nmo;
fmo_heap->execute();
dmo_heap->execute();
nmo_heap->execute();
*data = 45;
*fmo = std::move(*fmo_heap);
*dmo = std::move(*dmo_heap);
*nmo = std::move(*nmo_heap);
fmo->execute();
dmo->execute();
nmo->execute();
*data = 83;
fmo->execute();
dmo->execute();
nmo->execute();
// Output'
// 99
// 99
// 99
// 45
// 45
// 45
// 83
// 83
// 83
}
void moveStackToVectorFMO()
{
std::cout << std::endl << std::endl;
std::cout << "Test move stack variables into vector - FunctionMemberObject"
<< std::endl;
auto data = std::make_shared<int>(99);
std::vector<FunctionMemberObject> fmo_vector;
fmo_vector.push_back(std::move(FunctionMemberObject(print_data_func, data)));
fmo_vector.at(0).execute();
*data = -3;
fmo_vector.at(0).execute();
// Output
// std::bad_function_call / stops all printouts
// std::bad_function_call / stops all printouts
}
void moveStackToVectorDMO()
{
std::cout << std::endl << std::endl;
std::cout << "Test move stack variables into vector - DataMemberObject" << std::endl;
auto data = std::make_shared<int>(99);
std::vector<DataMemberObject> dmo_vector;
dmo_vector.push_back(std::move(DataMemberObject(print_data_func, data)));
dmo_vector.at(0).execute();
*data = -3;
dmo_vector.at(0).execute();
// Output
// null
// null
}
void moveStackToVectorNMO()
{
std::cout << std::endl << std::endl;
std::cout << "Test move stack variables into vector - NoMemberObject" << std::endl;
auto data = std::make_shared<int>(99);
std::vector<NoMemberObject> nmo_vector;
nmo_vector.push_back(std::move(NoMemberObject(print_data_func, data)));
nmo_vector.at(0).execute();
*data = -3;
nmo_vector.at(0).execute();
// Output
// 99
// -3
}
void moveHeapToVector()
{
std::cout << std::endl << std::endl;
std::cout << "Test move heap variables into vector" << std::endl;
auto data = std::make_shared<int>(99);
std::vector<std::unique_ptr<FunctionMemberObject>> fmo_vector;
fmo_vector.push_back(std::make_unique<FunctionMemberObject>(print_data_func, data));
std::vector<std::unique_ptr<DataMemberObject>> dmo_vector;
dmo_vector.push_back(std::make_unique<DataMemberObject>(print_data_func, data));
std::vector<std::unique_ptr<NoMemberObject>> nmo_vector;
nmo_vector.push_back(std::make_unique<NoMemberObject>(print_data_func, data));
fmo_vector.at(0)->execute();
dmo_vector.at(0)->execute();
nmo_vector.at(0)->execute();
*data = 1;
fmo_vector.at(0)->execute();
dmo_vector.at(0)->execute();
nmo_vector.at(0)->execute();
// Output
// 99
// 99
// 99
// 1
// 1
// 1
}
int main(int argc, char** argv)
{
// ~~~~~~~~~~ Copy and Move semantics ~~~~~~~~~~
// The copy-constructor is deleted for VoidCoroutine::pull_type, which
// implicitly deletes it for all classes that use it. The copy constructors
// being deleted makes sense since copying the coroutine would mean copying
// the "coroutine stack", and that would get messy if the same stack existed
// in more than one place.
//
// The move constructor / assgnment is allowed.
//
// We can verify this with the code below, or by lookinAg at pull_coroutine.hpp
// https://www.boost.org/doc/libs/1_74_0/libs/coroutine2/doc/html/coroutine2/coroutine/asymmetric/pull_coro.html
auto data = std::make_shared<int>(99);
FunctionMemberObject fmo(print_data_func, data);
DataMemberObject dmo(print_data_func, data);
NoMemberObject nmo(print_data_func, data);
// Won't compile
// FunctionMemberObject fmo_copy = fmo;
// DataMemberObject dmo_copy = dmo;
// NoMemberObject nmo_copy = nmo;
// OK
FunctionMemberObject fmo_move = std::move(fmo);
DataMemberObject dmo_move = std::move(dmo);
NoMemberObject nmo_move = std::move(nmo);
// ~~~~~~~~~~ Experiments and Observations ~~~~~~~~~~ //
// Note: Run each of these functions independently
// since failures from one can affect the behaviour
// of later functions
/* There are no issues if no data is moved. This makes sense
* since this is just a basic usage of coroutines, and is a
* case we really expect to work.
*/
// variablesOnStackNoMove(); // OK
// variablesOnHeapNoMove(); // OK
/* Moving coroutines works fine as long as the memory
* is moved from the stack to the stack, of from the
* heap to the heap
*/
// moveFromStackToStack(); // OK
// moveFromHeapToHeap(); // OK
/* For some reason, moving coroutines from the stack to the heap
* does not work in both directions. We don't have any ideas
* why moving from the stack to the heap causes issues, while
* moving from the heap to the stack does not.
*/
// moveFromStackToHeapFMO(); // BAD
// moveFromStackToHeapDMO(); // BAD
// moveFromStackToHeapNMO(); // BAD
// moveFromHeapToStack(); // OK
/* Because vectors (and other resizable containers) actually store
* data on the heap, moving coroutines into vectors only works
* in certain cases.
*
* As seen above, moving data from the heap to the heap works fine,
* and is also the case here when the data starts on the heap.
*
* We are not sure why moving data from the stack to the vector heap
* does not work here, when it seems to work fine in the above test
* without vectors. Specifically, it fails with the FunctionMemberObject
* and DataMemberObject, but works fine with NoMemberObject.
*/
// moveStackToVectorFMO(); // BAD
// moveStackToVectorDMO(); // BAD
// moveStackToVectorNMO(); // OK
// moveHeapToVector(); // OK
// ~~~~~~~~~~ Conclusions ~~~~~~~~~~ //
/* - Whether a resizable container is used or not, moving coroutines
* between the stack and the heap results in inconsistent behaviour
* (it works in some cases but not others, without any obviously
* consistent rules). Therefore, coroutines should never be
* moved between the stack and heap in any way.
*
* - In some "bad" cases, such as moveStackToVector(), the NoMemberObject
* actually works fine. This seems to imply that coroutines behave better,
* especially when being moved, when any data it's accessing is
* passed into the coroutine when it's created so it exists on
* the "coroutine stack". Therefore, prefer passing as much data
* to the coroutine as possible on creation and avoid using member
* variables when possible.
*
* COROUTINES BEST PRACTICES
* - Avoid moving coroutines. If the absolutely must be moved,
* make sure they are not moved between the stack and heap.
* - Avoid using coroutines with resizable containers. If they
* must be used, make sure that the coroutines are allocated
* on the heap.
* - Pass data to the coroutine on creation as much as possible,
* avoid using member variables.
*/
return 0;
}