Skip to content

Commit 66a36cf

Browse files
committed
smp: Restructure task arrays
1 parent 1577c90 commit 66a36cf

4 files changed

Lines changed: 54 additions & 64 deletions

File tree

api/smp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ public:
114114
// execute @func on another CPU core
115115
// call @done back on main CPU when task returns
116116
// use signal() to broadcast work should begin
117-
static void add_task(task_func func, done_func done, int cpu = -1);
118-
static void add_task(task_func func, int cpu = -1);
117+
static void add_task(task_func func, done_func done, int cpu = 0);
118+
static void add_task(task_func func, int cpu = 0);
119119
// execute a function on the main cpu
120120
static void add_bsp_task(done_func func);
121121

src/platform/x86_pc/apic_revenant.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ extern "C" void lapic_exception_handler();
1212
#define INFO(FROM, TEXT, ...) printf("%13s ] " TEXT "\n", "[ " FROM, ##__VA_ARGS__)
1313

1414
using namespace x86;
15-
smp_stuff smp;
16-
SMP_ARRAY<smp_stuff> smp_local;
15+
smp_stuff smp_main;
16+
SMP_ARRAY<smp_system_stuff> smp_system;
1717

18-
static bool revenant_task_doer(smp_stuff& system)
18+
static bool revenant_task_doer(smp_system_stuff& system)
1919
{
2020
// grab hold on task list
2121
lock(system.tlock);
@@ -39,26 +39,29 @@ static bool revenant_task_doer(smp_stuff& system)
3939
if (task.done)
4040
{
4141
// NOTE: specifically pushing to 'smp' here, and not 'system'
42-
lock(smp.flock);
43-
smp.completed.push_back(std::move(task.done));
44-
unlock(smp.flock);
42+
lock(smp_main.flock);
43+
smp_main.completed.push_back(std::move(task.done));
44+
unlock(smp_main.flock);
4545
return true;
4646
}
4747
// we did work, but we aren't going to signal back
4848
return false;
4949
}
5050
static void revenant_task_handler()
5151
{
52-
bool work_done = false;
53-
while (true) {
54-
// global tasks
55-
bool did_something = revenant_task_doer(smp);
56-
work_done = work_done || did_something;
52+
bool work_done = false;
53+
bool did_something = false;
54+
do {
5755
// cpu-specific tasks
58-
did_something = revenant_task_doer(PER_CPU(smp_local));
56+
did_something = revenant_task_doer(PER_CPU(smp_system));
5957
work_done = work_done || did_something;
60-
if (did_something == false) break;
61-
}
58+
// global tasks (by taking from index 0)
59+
did_something = revenant_task_doer(smp_system[0]);
60+
work_done = work_done || did_something;
61+
// continue as long as something was done
62+
// because there could be more
63+
} while (did_something);
64+
// if we did any work with done functions, signal back
6265
if (work_done) {
6366
x86::APIC::get().send_bsp_intr();
6467
}
@@ -91,7 +94,7 @@ void revenant_main(int cpu)
9194
::SMP::init_task();
9295

9396
// signal that the revenant has started
94-
smp.boot_barrier.inc();
97+
smp_main.boot_barrier.inc();
9598

9699
while (true)
97100
{

src/platform/x86_pc/apic_revenant.hpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,29 @@
2525

2626
extern "C" void revenant_main(int);
2727

28-
struct smp_stuff
29-
{
30-
struct task {
31-
task(SMP::task_func a,
32-
SMP::done_func b)
33-
: func(a), done(b) {}
28+
struct smp_task {
29+
smp_task(SMP::task_func a,
30+
SMP::done_func b)
31+
: func(a), done(b) {}
3432

35-
SMP::task_func func;
36-
SMP::done_func done;
37-
};
33+
SMP::task_func func;
34+
SMP::done_func done;
35+
};
3836

37+
struct smp_stuff
38+
{
3939
minimal_barrier_t boot_barrier;
4040

41-
spinlock_t tlock;
42-
std::deque<task> tasks;
43-
4441
spinlock_t flock;
4542
std::deque<SMP::done_func> completed;
4643
};
47-
extern smp_stuff smp;
48-
extern SMP_ARRAY<smp_stuff> smp_local;
44+
extern smp_stuff smp_main;
45+
46+
struct smp_system_stuff
47+
{
48+
spinlock_t tlock;
49+
std::deque<smp_task> tasks;
50+
};
51+
extern SMP_ARRAY<smp_system_stuff> smp_system;
4952

5053
#endif

src/platform/x86_pc/smp.cpp

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void SMP::init()
7575
boot->stack_base, boot->stack_size, sizeof(boot->worker_addr));
7676

7777
// reset barrier
78-
smp.boot_barrier.reset(1);
78+
smp_main.boot_barrier.reset(1);
7979

8080
auto& apic = x86::APIC::get();
8181
// turn on CPUs
@@ -98,7 +98,7 @@ void SMP::init()
9898
}
9999

100100
// wait for all APs to start
101-
smp.boot_barrier.spin_wait(CPUcount);
101+
smp_main.boot_barrier.spin_wait(CPUcount);
102102
INFO("SMP", "All %u APs are online now\n", CPUcount);
103103

104104
// subscribe to IPIs
@@ -116,10 +116,10 @@ void SMP::init()
116116
std::vector<smp_done_func> SMP::get_completed()
117117
{
118118
std::vector<smp_done_func> done;
119-
lock(smp.flock);
120-
for (auto& func : smp.completed) done.push_back(func);
121-
smp.completed.clear(); // MUI IMPORTANTE
122-
unlock(smp.flock);
119+
lock(smp_main.flock);
120+
for (auto& func : smp_main.completed) done.push_back(func);
121+
smp_main.completed.clear(); // MUI IMPORTANTE
122+
unlock(smp_main.flock);
123123
return done;
124124
}
125125

@@ -154,49 +154,33 @@ void ::SMP::init_task()
154154
void ::SMP::add_task(smp_task_func task, smp_done_func done, int cpu)
155155
{
156156
#ifdef INCLUDEOS_SINGLE_THREADED
157-
assert(cpu == -1 || cpu == 0);
157+
assert(cpu == 0);
158158
task(); done();
159159
#else
160-
if (cpu == -1)
161-
{
162-
lock(smp.tlock);
163-
smp.tasks.emplace_back(std::move(task), std::move(done));
164-
unlock(smp.tlock);
165-
}
166-
else {
167-
lock(smp_local[cpu].tlock);
168-
smp_local[cpu].tasks.emplace_back(std::move(task), std::move(done));
169-
unlock(smp_local[cpu].tlock);
170-
}
160+
lock(smp_system[cpu].tlock);
161+
smp_system[cpu].tasks.emplace_back(std::move(task), std::move(done));
162+
unlock(smp_system[cpu].tlock);
171163
#endif
172164
}
173165
void ::SMP::add_task(smp_task_func task, int cpu)
174166
{
175167
#ifdef INCLUDEOS_SINGLE_THREADED
176-
assert(cpu == -1 || cpu == 0);
168+
assert(cpu == 0);
177169
task();
178170
#else
179-
if (cpu == -1)
180-
{
181-
lock(smp.tlock);
182-
smp.tasks.emplace_back(std::move(task), nullptr);
183-
unlock(smp.tlock);
184-
}
185-
else {
186-
lock(smp_local[cpu].tlock);
187-
smp_local[cpu].tasks.emplace_back(std::move(task), nullptr);
188-
unlock(smp_local[cpu].tlock);
189-
}
171+
lock(smp_system[cpu].tlock);
172+
smp_system[cpu].tasks.emplace_back(std::move(task), nullptr);
173+
unlock(smp_system[cpu].tlock);
190174
#endif
191175
}
192176
void ::SMP::add_bsp_task(smp_done_func task)
193177
{
194178
#ifdef INCLUDEOS_SINGLE_THREADED
195179
task();
196180
#else
197-
lock(smp.flock);
198-
smp.completed.push_back(std::move(task));
199-
unlock(smp.flock);
181+
lock(smp_main.flock);
182+
smp_main.completed.push_back(std::move(task));
183+
unlock(smp_main.flock);
200184
x86::APIC::get().send_bsp_intr();
201185
#endif
202186
}

0 commit comments

Comments
 (0)