185 lines
5.9 KiB
Plaintext
185 lines
5.9 KiB
Plaintext
#! Contains `workers`: the threads `par_each` calls functions on, started as
|
|
#! it's first called and kept, waiting, until the world closes.
|
|
|
|
import system;
|
|
import std::thread;
|
|
module malunal::ecs;
|
|
|
|
# A run of a table's rows a thread calls a function for.
|
|
struct piece {
|
|
var which: u64; # The table, among those its query matched.
|
|
var begin: u64;
|
|
var end: u64;
|
|
}
|
|
|
|
# What the threads are given to do: a function for each piece.
|
|
struct job {
|
|
var run: def(*mut u8, &piece): void;
|
|
var context: *mut u8;
|
|
var pieces: *piece;
|
|
var count: u64;
|
|
}
|
|
|
|
# What the threads share, on the heap, so it stays where it is.
|
|
struct crew {
|
|
var job: *job;
|
|
var next: atomic<u64>; # The next piece to take.
|
|
var busy: atomic<u32>; # The workers yet to finish the job.
|
|
var round: atomic<u32>; # Counts jobs, and the workers wait on it.
|
|
var done: atomic<u32>; # 1 once the workers finished the job, which the world waits on.
|
|
var stopping: atomic<u32>;
|
|
}
|
|
|
|
# The threads a world calls functions on, besides its own.
|
|
struct workers : owning {
|
|
var shared: *mut crew;
|
|
var threads: vector<thread::handle<void>>;
|
|
var wanted: u32; # How many threads in all, the world's own among them, or 0 for one for each processor.
|
|
}
|
|
|
|
def workers::new(): workers {
|
|
return workers{ threads = vector::<thread::handle<void>>::new() };
|
|
}
|
|
|
|
# Starts the threads, where they aren't.
|
|
def workers::start(&mut this): void {
|
|
return if this.shared != null;
|
|
this.shared = raw_allocate_zeroed(size_of::<crew>()) as *mut crew;
|
|
val all := this.wanted == 0 ? thread::cpu_count() : this.wanted;
|
|
for _ in 1..all {
|
|
var started := thread::spawn_with(work, this.shared) else (_) { break; };
|
|
this.threads.append(move(started));
|
|
}
|
|
}
|
|
|
|
# Calls a job's function for each of its pieces, on every thread, this one
|
|
# among them, and returns once they're done.
|
|
def workers::run(&mut this, given: &job): void {
|
|
this.start();
|
|
val s := &mut through(this.shared);
|
|
s.job = address(given);
|
|
s.next.store(0, order::relaxed);
|
|
if this.threads.length > 0 {
|
|
s.done.store(0, order::relaxed);
|
|
s.busy.store(this.threads.length as u32, order::relaxed);
|
|
_ = s.round.fetch_add(1, order::release);
|
|
_ = system::futex_wake(word_of(&mut s.round), 0x7FFFFFFF);
|
|
}
|
|
drain(s);
|
|
if this.threads.length > 0 {
|
|
while s.done.load(order::acquire) == 0 {
|
|
_ = system::futex_wait(word_of(&mut s.done), 0, -1);
|
|
}
|
|
}
|
|
s.job = null;
|
|
}
|
|
|
|
pub def workers::close(&mut this): void {
|
|
if this.shared != null {
|
|
val s := &mut through(this.shared);
|
|
s.stopping.store(1, order::release);
|
|
_ = s.round.fetch_add(1, order::release);
|
|
_ = system::futex_wake(word_of(&mut s.round), 0x7FFFFFFF);
|
|
this.threads.close();
|
|
raw_release(this.shared as *mut u8);
|
|
this.shared = null;
|
|
}
|
|
this.threads.close();
|
|
}
|
|
|
|
# What each worker does: waits for a job, takes its pieces until none are
|
|
# left, and says it's done.
|
|
def work(shared: *mut crew): void {
|
|
val s := &mut through(shared);
|
|
var seen: u32 = 0;
|
|
while true {
|
|
var round := s.round.load(order::acquire);
|
|
while round == seen {
|
|
_ = system::futex_wait(word_of(&mut s.round), seen, -1);
|
|
round = s.round.load(order::acquire);
|
|
}
|
|
seen = round;
|
|
return if s.stopping.load(order::acquire) != 0;
|
|
drain(s);
|
|
if s.busy.fetch_sub(1, order::acq_rel) == 1 {
|
|
s.done.store(1, order::release);
|
|
_ = system::futex_wake(word_of(&mut s.done), 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
# Takes pieces of the job, calling its function for each, until none are left.
|
|
def drain(s: &mut crew): void {
|
|
val given := &through(s.job);
|
|
while true {
|
|
val i := s.next.fetch_add(1, order::relaxed);
|
|
return if i >= given.count;
|
|
given.run(given.context, &given.pieces[i]);
|
|
}
|
|
}
|
|
|
|
# The word of an atomic, which the system's futexes wait on.
|
|
def word_of(of: &mut atomic<u32>): *mut u32 {
|
|
return (address(of) as *mut atomic<u32>) as *mut u32;
|
|
}
|
|
|
|
#! Says how many threads `par_each` calls functions on, the world's own among
|
|
#! them: by default, one for each processor. Those it started already end.
|
|
pub def world::threads(&mut this, count: u32): void {
|
|
this.crew.close();
|
|
this.crew.wanted = count == 0 ? 1 : count;
|
|
}
|
|
|
|
# Fewer rows than this a pass calls its function for on its own thread, as
|
|
# waking the workers would cost more than they'd save.
|
|
const alone: u64 = 4096;
|
|
|
|
# Cuts the rows of the tables a pass matched into pieces, and has the workers
|
|
# call a function for each. Where threads share the world, this one calls it
|
|
# for each alone, as those threads are the workers.
|
|
def world::run_pieces(&mut this, q: *mut query_state, p: pass, context: *mut u8, run: def(*mut u8, &piece): void): void {
|
|
var pieces := vector::<piece>::new();
|
|
var total := 0u;
|
|
for m in 0..q[0].matched.length {
|
|
val rows := this.rows_of(q, m, p.since, p.now);
|
|
if rows > 0 {
|
|
pieces.append(piece{ which = m, begin = 0, end = rows });
|
|
total += rows;
|
|
}
|
|
}
|
|
if this.sharing {
|
|
for i in 0..pieces.length {
|
|
run(context, &pieces[i]);
|
|
}
|
|
return;
|
|
}
|
|
# What the function may do is the same however many threads it's called on.
|
|
this.parallel = true;
|
|
val all := this.crew.wanted == 0 ? thread::cpu_count() as u64 : this.crew.wanted as u64;
|
|
if total < alone || all <= 1 {
|
|
for i in 0..pieces.length {
|
|
run(context, &pieces[i]);
|
|
}
|
|
this.parallel = false;
|
|
return;
|
|
}
|
|
|
|
# Pieces small enough that every thread gets several, so those finishing
|
|
# first take more.
|
|
var grain := total / (all * 8);
|
|
grain = grain < 1024 ? 1024u : grain;
|
|
var cut := vector::<piece>::new();
|
|
for i in 0..pieces.length {
|
|
val whole: piece = pieces[i];
|
|
var at := whole.begin;
|
|
while at < whole.end {
|
|
val end := at + grain < whole.end ? at + grain : whole.end;
|
|
cut.append(piece{ which = whole.which, begin = at, end = end });
|
|
at = end;
|
|
}
|
|
}
|
|
val given := job{ run = run, context = context, pieces = cut.span.data, count = cut.length };
|
|
this.crew.run(&given);
|
|
this.parallel = false;
|
|
}
|