141 lines
5.0 KiB
Plaintext
141 lines
5.0 KiB
Plaintext
#! Contains `crew`: the threads a space solves a step's colors on, besides
|
|
#! its caller's, started as a step first asks for more than one and kept until
|
|
#! the space closes. Each job is a number of things, taken a block at a time by
|
|
#! whichever thread is free, the caller among them, which returns once all are
|
|
#! done. Between jobs, as a step's come one after another, the threads spin a
|
|
#! little before sleeping, so a job begins in a microsecond rather than the
|
|
#! tens a sleeping thread takes to wake, as Box2D's workers spin. They spin by
|
|
#! reading what they wait on: `system::pause` is the system's wait for a signal,
|
|
#! not the processor's hint that a thread spins.
|
|
|
|
import system;
|
|
import std::containers;
|
|
import std::thread;
|
|
module malunal::physics2d;
|
|
|
|
# What the threads are given to do: a function for each range of things.
|
|
struct crew_job {
|
|
var run: def(*mut u8, u64, u64): void;
|
|
var context: *mut u8;
|
|
var count: u64;
|
|
var block: u64;
|
|
}
|
|
|
|
# What the threads share, on the heap, so it stays where it is.
|
|
struct crew_state {
|
|
var job: crew_job;
|
|
var next: atomic<u64>; # The job's round, high, and the next thing to take, low.
|
|
var done: atomic<u64>; # How many things are done.
|
|
var round: atomic<u32>; # Counts jobs, and the threads wait on it.
|
|
var sleeping: atomic<u32>; # How many threads sleep rather than spin.
|
|
var stopping: atomic<u32>;
|
|
}
|
|
|
|
# How many times a thread spins waiting for a job before it sleeps.
|
|
const spins: u32 = 200000;
|
|
|
|
# The threads a space solves on, besides its caller's.
|
|
struct crew : owning {
|
|
var shared: *mut crew_state;
|
|
var threads: vector<thread::handle<void>>;
|
|
}
|
|
|
|
# Starts threads, so there are as many as asked for in all, the caller's among
|
|
# them, where there aren't.
|
|
def crew::start(&mut this, count: u32): void {
|
|
if this.shared != null && this.threads.length + 1 != count as u64 {
|
|
this.close();
|
|
}
|
|
return if this.shared != null || count <= 1;
|
|
this.shared = raw_allocate_zeroed(size_of::<crew_state>()) as *mut crew_state;
|
|
for _ in 1..count {
|
|
var started := thread::spawn_with(work, this.shared) else (_) { break; };
|
|
this.threads.append(move(started));
|
|
}
|
|
}
|
|
|
|
# Runs a function over ranges of a job's things, a block at a time, on this
|
|
# thread and the crew's, and returns once all are done.
|
|
def crew::run(&mut this, run: def(*mut u8, u64, u64): void, context: *mut u8, count: u64, block: u64): void {
|
|
return if count == 0;
|
|
if this.shared == null || count <= block {
|
|
run(context, 0, count);
|
|
return;
|
|
}
|
|
val s := &mut through(this.shared);
|
|
# The next round's things first, so a thread late from the last takes none
|
|
# of this job's by the last's function.
|
|
val round := s.round.load(order::relaxed) + 1;
|
|
s.next.store((round as u64) << 32, order::release);
|
|
s.job = crew_job{ run = run, context = context, count = count, block = block };
|
|
s.done.store(0, order::relaxed);
|
|
s.round.store(round, order::release);
|
|
if s.sleeping.load(order::acquire) > 0 {
|
|
_ = system::futex_wake(word_of(&mut s.round), 0x7FFFFFFF);
|
|
}
|
|
drain(s, round);
|
|
while s.done.load(order::acquire) < count {
|
|
}
|
|
}
|
|
|
|
pub def crew::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 thread does: waits for a job, spinning then sleeping, and takes
|
|
# its things until none are left.
|
|
def work(shared: *mut crew_state): void {
|
|
val s := &mut through(shared);
|
|
var seen: u32 = 0;
|
|
while true {
|
|
var round := s.round.load(order::acquire);
|
|
var spun: u32 = 0;
|
|
while round == seen {
|
|
if spun < spins {
|
|
spun++;
|
|
} else {
|
|
_ = s.sleeping.fetch_add(1, order::acq_rel);
|
|
if s.round.load(order::acquire) == seen {
|
|
_ = system::futex_wait(word_of(&mut s.round), seen, -1);
|
|
}
|
|
_ = s.sleeping.fetch_sub(1, order::acq_rel);
|
|
}
|
|
round = s.round.load(order::acquire);
|
|
}
|
|
seen = round;
|
|
return if s.stopping.load(order::acquire) != 0;
|
|
drain(s, round);
|
|
}
|
|
}
|
|
|
|
# Takes blocks of a round's job, running its function over each, until none
|
|
# are left, or the round has passed.
|
|
def drain(s: &mut crew_state, round: u32): void {
|
|
val job := s.job;
|
|
while true {
|
|
val held := s.next.load(order::acquire);
|
|
return if (held >> 32) as u32 != round;
|
|
val at := held & 0xFFFFFFFF;
|
|
return if at >= job.count;
|
|
val claimed := s.next.compare_exchange_weak(held, held + job.block, order::acq_rel, order::acquire);
|
|
continue if claimed is error(_);
|
|
val end := at + job.block < job.count ? at + job.block : job.count;
|
|
job.run(job.context, at, end);
|
|
_ = s.done.fetch_add(end - at, order::acq_rel);
|
|
}
|
|
}
|
|
|
|
# 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;
|
|
}
|