Skip to content

Commit aaf5636

Browse files
committed
MiLi coroutines!
1 parent d2cb635 commit aaf5636

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+6517
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@
2727
*.exe
2828
*.out
2929
*.app
30+
31+
*.suo
32+
*.vcxproj.user
33+
Debug
34+
Release
35+
*.db
36+
*.opendb
37+
/packages

CoroutineTest/CoroutineTest.sln

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25123.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CoroutineTest", "CoroutineTest\CoroutineTest.vcxproj", "{9DCF92A7-2429-4200-BCCE-42020729E25B}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{9DCF92A7-2429-4200-BCCE-42020729E25B}.Debug|x64.ActiveCfg = Debug|x64
17+
{9DCF92A7-2429-4200-BCCE-42020729E25B}.Debug|x64.Build.0 = Debug|x64
18+
{9DCF92A7-2429-4200-BCCE-42020729E25B}.Debug|x86.ActiveCfg = Debug|Win32
19+
{9DCF92A7-2429-4200-BCCE-42020729E25B}.Debug|x86.Build.0 = Debug|Win32
20+
{9DCF92A7-2429-4200-BCCE-42020729E25B}.Release|x64.ActiveCfg = Release|x64
21+
{9DCF92A7-2429-4200-BCCE-42020729E25B}.Release|x64.Build.0 = Release|x64
22+
{9DCF92A7-2429-4200-BCCE-42020729E25B}.Release|x86.ActiveCfg = Release|Win32
23+
{9DCF92A7-2429-4200-BCCE-42020729E25B}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "test_scenario.h"
2+
#include "MiLi\mili.h"
3+
#include <iostream>
4+
5+
using namespace mili;
6+
using namespace std;
7+
8+
9+
struct MiliTask : Task, mili::Coroutine {
10+
11+
int run() {
12+
BEGIN_COROUTINE
13+
while (true) {
14+
while (!worker.atMine()) {
15+
worker.moveMine();
16+
mili_yield(0);
17+
}
18+
do {
19+
worker.gather();
20+
mili_yield(0);
21+
} while (worker.isMining());
22+
while (!worker.atHome()) {
23+
worker.moveHome();
24+
mili_yield(0);
25+
}
26+
worker.dropoff();
27+
mili_yield(0);
28+
}
29+
END_COROUTINE(0);
30+
}
31+
32+
void print(ostream& stream) const {
33+
stream << "Mili Coroutine1: " << worker.total << endl;
34+
}
35+
};
36+
37+
38+
39+
40+
inline int runMili() {
41+
cout << "Raw MiLi coroutines" << endl;
42+
auto *task = new MiliTask();
43+
World world(task);
44+
return world.run();
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#pragma once
2+
#include "test_scenario.h"
3+
#include "MiLi\mili.h"
4+
#include <iostream>
5+
#include <queue>
6+
7+
using namespace mili;
8+
using namespace std;
9+
10+
struct MiliTask2;
11+
12+
static MiliTask2* next_frame2;
13+
static MiliTask2* end_coro2;
14+
15+
struct MiliTask2 : mili::Coroutine {
16+
MiliTask2* caller = nullptr;
17+
virtual MiliTask2* run() {
18+
return end_coro2;
19+
}
20+
};
21+
22+
struct MiliTask2_Worker : MiliTask2 {
23+
Worker& worker;
24+
MiliTask2_Worker(Worker& p_worker) :worker(p_worker) {}
25+
};
26+
27+
28+
struct MiliTask2Main : MiliTask2_Worker {
29+
virtual MiliTask2* run() {
30+
BEGIN_COROUTINE
31+
while (true) {
32+
while (!worker.atMine()) {
33+
worker.moveMine();
34+
mili_yield(next_frame2);
35+
}
36+
do {
37+
worker.gather();
38+
mili_yield(next_frame2);
39+
} while (worker.isMining());
40+
while (!worker.atHome()) {
41+
worker.moveHome();
42+
mili_yield(next_frame2);
43+
}
44+
worker.dropoff();
45+
mili_yield(next_frame2);
46+
}
47+
END_COROUTINE(end_coro2);
48+
}
49+
MiliTask2Main(Worker& p_worker) :MiliTask2_Worker(p_worker) {}
50+
};
51+
52+
struct MiliTask2Mgr : Task {
53+
queue<MiliTask2*> q;
54+
int run() {
55+
do {
56+
auto curr = q.front();
57+
q.pop();
58+
auto awt = curr->run();
59+
if (awt == next_frame2) {
60+
q.push(curr);
61+
break;
62+
}
63+
} while (q.size() > 0);
64+
return 0;
65+
}
66+
MiliTask2Mgr()
67+
: Task()
68+
{
69+
auto t = new MiliTask2Main(worker);
70+
q.push(t);
71+
}
72+
73+
void print(ostream& stream) const {
74+
stream << "Mili Coroutine2: " << worker.total << endl;
75+
}
76+
};
77+
78+
79+
inline int runMili2() {
80+
cout << "MiLi coroutines with managed queue" << endl;
81+
next_frame2 = new MiliTask2();
82+
end_coro2 = new MiliTask2();
83+
auto *task = new MiliTask2Mgr();
84+
World world(task);
85+
return world.run();
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#pragma once
2+
#include "test_scenario.h"
3+
#include "MiLi\mili.h"
4+
#include <iostream>
5+
#include <queue>
6+
7+
using namespace mili;
8+
using namespace std;
9+
10+
struct MiliTask3;
11+
12+
static MiliTask3* next_frame3;
13+
static MiliTask3* end_coro3;
14+
15+
struct MiliTask3 : mili::Coroutine {
16+
MiliTask3* caller = nullptr;
17+
virtual MiliTask3* run() {
18+
return end_coro3;
19+
}
20+
};
21+
22+
struct MiliTask3_Worker : MiliTask3 {
23+
Worker& worker;
24+
MiliTask3_Worker(Worker& p_worker) :worker(p_worker) {}
25+
};
26+
27+
struct MiliTask3_GotoMine : MiliTask3_Worker {
28+
virtual MiliTask3* run() {
29+
BEGIN_COROUTINE
30+
while (!worker.atMine()) {
31+
worker.moveMine();
32+
mili_yield(next_frame3);
33+
}
34+
END_COROUTINE(end_coro3);
35+
}
36+
MiliTask3_GotoMine(Worker& p_worker, MiliTask3* p_caller)
37+
:MiliTask3_Worker(p_worker)
38+
{
39+
caller = p_caller;
40+
}
41+
};
42+
43+
struct MiliTask3_Gather : MiliTask3_Worker {
44+
virtual MiliTask3* run() {
45+
BEGIN_COROUTINE
46+
do {
47+
worker.gather();
48+
mili_yield(next_frame3);
49+
} while (worker.isMining());
50+
END_COROUTINE(end_coro3);
51+
}
52+
MiliTask3_Gather(Worker& p_worker, MiliTask3* p_caller)
53+
:MiliTask3_Worker(p_worker)
54+
{
55+
caller = p_caller;
56+
}
57+
};
58+
59+
struct MiliTask3_Dropoff : MiliTask3_Worker {
60+
virtual MiliTask3* run() {
61+
BEGIN_COROUTINE
62+
while (!worker.atHome()) {
63+
worker.moveHome();
64+
mili_yield(next_frame3);
65+
}
66+
worker.dropoff();
67+
END_COROUTINE(end_coro3);
68+
}
69+
MiliTask3_Dropoff(Worker& p_worker, MiliTask3* p_caller)
70+
:MiliTask3_Worker(p_worker)
71+
{
72+
caller = p_caller;
73+
}
74+
};
75+
76+
struct MiliTask3Main : MiliTask3_Worker {
77+
virtual MiliTask3* run() {
78+
BEGIN_COROUTINE
79+
while (true) {
80+
mili_yield(new MiliTask3_GotoMine(worker, this));
81+
mili_yield(new MiliTask3_Gather(worker, this));
82+
mili_yield(new MiliTask3_Dropoff(worker, this));
83+
}
84+
END_COROUTINE(end_coro3);
85+
}
86+
MiliTask3Main(Worker& p_worker) :MiliTask3_Worker(p_worker) {}
87+
};
88+
89+
struct MiliTask3Mgr : Task {
90+
queue<MiliTask3*> q;
91+
int run() {
92+
do {
93+
auto curr = q.front();
94+
q.pop();
95+
auto awt = curr->run();
96+
if (awt == next_frame3) {
97+
q.push(curr);
98+
break;
99+
}
100+
if (awt == end_coro3 && curr->caller != nullptr) {
101+
q.push(curr->caller);
102+
continue;
103+
}
104+
if (awt->caller != nullptr) {
105+
q.push(awt);
106+
continue;
107+
}
108+
} while (q.size() > 0);
109+
return 0;
110+
}
111+
MiliTask3Mgr()
112+
: Task()
113+
{
114+
auto t = new MiliTask3Main(worker);
115+
q.push(t);
116+
}
117+
118+
void print(ostream& stream) const {
119+
stream << "Mili Coroutine3: " << worker.total << endl;
120+
}
121+
};
122+
123+
124+
inline int runMili3() {
125+
cout << "MiLi coroutines with await" << endl;
126+
next_frame3 = new MiliTask3();
127+
end_coro3 = new MiliTask3();
128+
auto *task = new MiliTask3Mgr();
129+
World world(task);
130+
return world.run();
131+
}

0 commit comments

Comments
 (0)