On Sat, Aug 24, 2013 at 3:26 PM, bearophile
<bearophileHUGS@lycos.com> wrote:
David Nadlinger:
It's a cute idea, but not really practical, I'm afraid – Goroutines are
managed by a scheduler in the Go runtime library, whereas D threads
directly map to OS threads.
Can't Rory McGuire add a scheduler to his code? How much code does it take?
Bye,
bearophile
Here is a basic co-operative scheduler based version of go!(F) that uses a channel to keep things simple, its co-op so Fiber.yield has to be called at some point. If the channel detects its in a Fiber it calls yield if there is nothing available in it:
void go(alias F)() {
scheduler._ = new Fiber(F);
}
shared chan!Fiber scheduler; // channel contains Fibers waiting for their time slice
static this () {
if (scheduler is null) {
scheduler = makeChan!Fiber(100);
// create the workers
auto goprocs = environment.get("GOPROCS");
int num_threads = 1;
if (goprocs != null) {
num_threads = to!int(goprocs);
}
foreach (i; 0..num_threads) {
// create threads that process the live fibers
auto t = new Thread(() {
for (;;) {
auto fiber = scheduler._;
fiber.call();
if (fiber.state != Fiber.State.TERM) {
scheduler._ (fiber);
}
}
});
t.start();
}
}
}
Just need to figure out a way to detect when main() has exited.