photon
Photon is a lightweight transparent fiber scheduler. It's inspired by Golang's green thread model and
the spawn function is called go doing the same job that Golang's keyword does.
The framework API surface is kept to a minimum, many programs can be written using only
three primitives: initPhoton to initialize Photon, runScheduler to start fiber scheduler and
go to create tasks, including the initial tasks.
Discussion
Example, showcasing channels and std.range interop:
module examples.channels;
import std.algorithm, std.datetime, std.range, std.stdio;
import photon;
void first(shared Channel!string work, shared Channel!int completion) {
delay(2.msecs);
work.put("first #1");
delay(2.msecs);
work.put("first #2");
delay(2.msecs);
work.put("first #3");
completion.put(1);
}
void second(shared Channel!string work, shared Channel!int completion) {
delay(3.msecs);
work.put("second #1");
delay(3.msecs);
work.put("second #2");
completion.put(2);
}
void main() {
initPhoton();
auto jobQueue = channel!string(2);
auto finishQueue = channel!int(1);
go({
first(jobQueue, finishQueue);
});
go({ // producer # 2
second(jobQueue, finishQueue);
});
go({ // consumer
foreach (item; jobQueue) {
delay(1.seconds);
writeln(item);
}
});
go({ // closer
auto completions = finishQueue.take(2).array;
assert(completions.length == 2);
jobQueue.close(); // all producers are done
});
runScheduler();
}
-
Declaration
structTask;Taskresult allows one fiber to wait on the other by joining the execution. -
Declaration
nothrow @trusted TaskinitPhoton();Initialize event loop and internal data structures for Photon scheduler.
-
Declaration
@trusted Taskgo(void delegate()func);
@safe Taskgo(void function()func);Setup a fiber task to run on the Photon scheduler.
-
Declaration
@trusted TaskgoOnSameThread(void delegate()func);
@safe TaskgoOnSameThread(void function()func);Same as go but make sure the fiber is scheduled on the same thread of the threadpool. Could be useful if there is a need to propagate TLS variable.
-
Declaration
@trusted Toffload(T)(T delegate()work);Run
workon a dedicated thread pool and pass the result back to the calling fiber or thread. This avoids blocking event loop on computationally intensive tasks. -
Declaration
voiddelay(Durationreq);Suspend the current fiber or thread for
reqamount of time. Note the resolution of wait is in milliseconds, adelayof zero will still yield the execution. -
Declaration
voidyield();Yields the execution of current fiber or thread
-
Declaration
nothrow @safe size_tschedulerThreads();Number of threads running the scheduler loop
-
Declaration
@trusted voidrunScheduler();Start sheduler and run fibers until all are terminated.
-
Declaration
@trusted voidrunPhoton(void delegate()main);Initialize and run fibers with the given
main -
Declaration
nothrow @trusted automutex();Create non-recursive
mutexExamples
initPhoton(); auto mtx = mutex(); int counter = 0; go({ foreach (_; 0..100) { mtx.lock(); int c = counter; delay(1.msecs); counter = c + 1; mtx.unlock(); } }); go({ foreach (_; 0..100) { mtx.lock(); int c = counter; delay(1.msecs); counter = c + 1; mtx.unlock(); } }); runScheduler(); mtx.dispose(); assert(counter == 200);
-
Declaration
structRecursiveMutex; -
Declaration
nothrow @trusted autorecursiveMutex();Create recursive mutex
-
Declaration
nothrow @trusted autocondition();Create a conditional variable
-
Declaration
structChannel(T);A ref-counted channel that is safe to share between multiple fibers. In essence it's a multiple producer single consumer queue, that implements
OutputRangeandInputRangeconcepts.-
Declaration
voidput(Tvalue);OutputRange contract - puts a new item into the channel.
-
Declaration
boolempty();Part of InputRange contract - checks if there is an item in the queue. Returns
trueif channel is closed and its buffer is exhausted. -
Declaration
ref Tfront();Part of InputRange contract - returns an item available in the channel.
-
Declaration
voidpopFront();Part of InputRange contract - advances range forward.
-
-
Declaration
@safe autochannel(T)(size_tcapacity= 1);Create a new shared
Channelwith givencapacity. -
Declaration
@trusted voidselect(Args...)(auto ref Argsargs) if (allSatisfy!(isChannel, Even!Args) && allSatisfy!(isHandler, Odd!Args));Multiplex between multiple channels, executes a lambda attached to the first channel that becomes ready to read.
-
Declaration
enum autoisChannel(T);Trait for testing if a type is Channel
-
Declaration
classPool(T); -
Declaration
@trusted autopool(T)(size_tsize, DurationmaxIdle, T delegate()open, void delegate(ref T)close);Create generic
poolfor resources,opencreates new resource,closereleases the resource.