#!/usr/bin/env dub
/+ dub.sdl:
name "vibe_signal"
dependency "vibe-core" version="2.14.0"
buildType "checked" {
buildOptions "optimize" "inline" "debugInfo"
}
platforms "linux"
+/
import (package) corecore.(package) core.syssys.(package) core.sys.posixposix.(module) core.sys.posix.signalD header file for POSIX.
Source
core/sys/posix/signal.d
signal;
import (package) corecore.(package) core.syssys.(package) core.sys.posixposix.(module) core.sys.posix.pthreadD header file for POSIX.
pthread : (alias) vibe_signal.pthread_self = ulong core.sys.posix.pthread.pthread_self() nothrow @nogcpthread_self;
import (package) stdstd.(module) std.stdioCategory Symbols File handles _popen File isFileHandle openNetwork stderr stdin stdout Reading chunks lines readf readfln readln Writing toFile write writef writefln writeln Misc KeepTerminator LockType StdioException
Standard I/O functions that extend core.stdc.stdio. core.stdc.stdio
is publically imported when importing std.stdio.
There are three layers of I/O:
The lowest layer is the operating system layer. The two main schemes are Windows and Posix.
C's stdio.h which unifies the two operating system schemes.
std.stdio, this module, unifies the various stdio.h implementations into
a high level package for D programs.
Source
std/stdio.d
stdio : (alias template) vibe_signal.writeln = std.stdio.writeln(T...)(T args)Equivalent to write(args, '\n'). Calling writeln without
arguments is valid and just prints a newline to the standard
output.
Example
Reads stdin and writes it to stdout with an argument
counter.
import std.stdio;
void main()
{
string line;
for (size_t count = 0; (line = readln) !is null; count++)
{
writeln("Input ", count, ": ", line);
}
}
writeln;
void void D main()main()
{
int (local variable) int receivedreceived;
// Application POSIX adapter. sigwait runs in ordinary thread context, so
// no event, allocation or mutex operation is called from a signal handler.
void vibe_signal.runWorker(void delegate() work)runWorker({
(struct) core.sys.posix.signal.sigset_tsigset_t (local variable) core.sys.posix.signal.sigset_t signalssignals, (local variable) core.sys.posix.signal.sigset_t previousprevious;
assert(int core.sys.posix.signal.sigemptyset(core.sys.posix.signal.sigset_t*) nothrow @nogcsigemptyset(&(local variable) core.sys.posix.signal.sigset_t signalssignals) == 0);
assert(int core.sys.posix.signal.sigaddset(core.sys.posix.signal.sigset_t*, int) nothrow @nogcsigaddset(&(local variable) core.sys.posix.signal.sigset_t signalssignals, (constant) int core.sys.posix.signal.SIGUSR1 = 10SIGUSR1) == 0);
assert(int core.sys.posix.signal.pthread_sigmask(int, scope const(core.sys.posix.signal.sigset_t*), core.sys.posix.signal.sigset_t*) nothrow @nogcpthread_sigmask((constant) int core.sys.posix.signal.SIG_BLOCK = 0SIG_BLOCK, &(local variable) core.sys.posix.signal.sigset_t signalssignals, &(local variable) core.sys.posix.signal.sigset_t previousprevious) == 0);
scope(exit) assert(int core.sys.posix.signal.pthread_sigmask(int, scope const(core.sys.posix.signal.sigset_t*), core.sys.posix.signal.sigset_t*) nothrow @nogcpthread_sigmask((constant) int core.sys.posix.signal.SIG_SETMASK = 2SIG_SETMASK, &(local variable) core.sys.posix.signal.sigset_t previousprevious, null) == 0);
assert(int core.sys.posix.signal.pthread_kill(ulong, int) nothrow @nogcpthread_kill(ulong core.sys.posix.pthread.pthread_self() nothrow @nogcpthread_self(), (constant) int core.sys.posix.signal.SIGUSR1 = 10SIGUSR1) == 0);
assert(int core.sys.posix.signal.sigwait(scope const(core.sys.posix.signal.sigset_t*), int*) nothrow @nogcsigwait(&(local variable) core.sys.posix.signal.sigset_t signalssignals, &(local variable) int receivedreceived) == 0);
});
assert((local variable) int receivedreceived == (constant) int core.sys.posix.signal.SIGUSR1 = 10SIGUSR1);
void std.stdio.writeln!string(string __param_0) @safeEquivalent to write(args, '\n'). Calling writeln without
arguments is valid and just prints a newline to the standard
output.
Example
Reads stdin and writes it to stdout with an argument
counter.
import std.stdio;
void main()
{
string line;
for (size_t count = 0; (line = readln) !is null; count++)
{
writeln("Input ", count, ": ", line);
}
}
writeln("got signal SIGUSR1");
}
void void vibe_signal.runWorker(void delegate() work)runWorker(void delegate() (parameter) void delegate() workwork)
{
import (package) vibevibe.(package) vibe.corecore.(module) vibe.core.syncEvent loop compatible task synchronization facilities.
This module provides replacement primitives for the modules in core.sync``
that do not block vibe.d's event loop in their wait states. These should
always be preferred over the ones in Druntime under usual circumstances.
Using a standard Mutex is possible as long as it is ensured that no event
loop based functionality (I/O, task interaction or anything that implicitly
calls vibe.core.core.yield) is executed within a section of code that is
protected by the mutex. Failure to do so may result in dead-locks and
high-level race-conditions!
sync : (alias) createSharedManualEvent = shared(vibe.core.sync.ManualEvent) vibe.core.sync.createSharedManualEvent() nothrow @trustedCreates a new signal that can be shared between fibers.
createSharedManualEvent;
import (package) corecore.(module) core.threadThe thread module provides support for thread creation and management.
Source
core/thread/package.d
thread : (class) core.thread.osthread.ThreadThis class encapsulates all threading functionality for the D
programming language. As thread manipulation is a required facility
for garbage collection, all user threads should derive from this
class, and instances of this class should never be explicitly deleted.
A new thread may be created using either derivation or composition, as
in the following example.
Thread;
auto (local variable) shared(vibe.core.sync.ManualEvent) completedcompleted = shared(vibe.core.sync.ManualEvent) vibe.core.sync.createSharedManualEvent() nothrow @trustedCreates a new signal that can be shared between fibers.
createSharedManualEvent();
auto (local variable) core.thread.osthread.Thread threadthread = new (class) core.thread.osthread.ThreadThis class encapsulates all threading functionality for the D
programming language. As thread manipulation is a required facility
for garbage collection, all user threads should derive from this
class, and instances of this class should never be explicitly deleted.
A new thread may be created using either derivation or composition, as
in the following example.
Thread({ (parameter) void delegate() workwork(); (local variable) shared(vibe.core.sync.ManualEvent) completedcompleted.int vibe.core.sync.ManualEvent.emit() shared nothrow @trustedEmits the signal, waking up all owners of the signal.
emit(); });
(local variable) core.thread.osthread.Thread threadthread.core.thread.osthread.Thread core.thread.osthread.Thread.start() nothrowStarts the thread and invokes the function or delegate passed upon
construction.
In
This routine may only be called once per thread instance.
start();
(local variable) shared(vibe.core.sync.ManualEvent) completedcompleted.int vibe.core.sync.ManualEvent.wait(int emit_count) shared @safeAcquires ownership and waits until the emit count differs from the
given one or until a timeout is reached.
wait(0); // parks the fiber, while the worker owns sigwait
(local variable) core.thread.osthread.Thread threadthread.object.Throwable core.thread.osthread.Thread.join(bool rethrow = true)Waits for this thread to complete. If the thread terminated as the
result of an unhandled exception, this exception will be rethrown.
join();
}