vibe_channel.dhover×59all
#!/usr/bin/env dub
/+ dub.sdl:
    name "vibe_channel"
    dependency "vibe-core" version="2.14.0"
    buildType "checked" {
        buildOptions "optimize" "inline" "debugInfo"
    }
    platforms "linux"
+/
import 
(package) core
core
.
(module) core.time

Module containing core time functionality, such as Duration (which represents a duration of time) or MonoTime (which represents a timestamp of the system's monotonic clock).

Various functions take a string (or strings) to represent a unit of time (e.g. convert!("days", "hours")(numDays)). The valid strings to use with such functions are "years", "months", "weeks", "days", "hours", "minutes", "seconds", "msecs" (milliseconds), "usecs" (microseconds), "hnsecs" (hecto-nanoseconds - i.e. 100 ns) or some subset thereof. There are a few functions that also allow "nsecs", but very little actually has precision greater than hnsecs.

Symbol Description
Types
Duration Represents a duration of time of weeks or less (kept internally as hnsecs). (e.g. 22 days or 700 seconds).
TickDuration DEPRECATED Represents a duration of time in system clock ticks, using the highest precision that the system provides.
MonoTime Represents a monotonic timestamp in system clock ticks, using the highest precision that the system provides.
Functions
convert Generic way of converting between two time units.
dur Allows constructing a Duration from the given time units with the given length.
weeks days hours

minutes seconds msecs

usecs hnsecs nsecs | Convenience aliases for dur. | | abs | Returns the absolute value of a duration. |

From Duration
From TickDuration
From units
To Duration
tickDuration.to, std,conv!Duration()
dur!"msecs"(5) or 5.msecs()

| To TickDuration | duration.to, std,conv!TickDuration() |

  • | TickDuration.from!"msecs"(msecs) |

| To units | duration.total!"days" | tickDuration.msecs | convert!("days", "msecs")(msecs) |

Source

core/time.d

@copyrightCopyright 2010 - 2012@licenseBoost License 1.0.@authorsJonathan M Davis and Kato Shoichi
time
: msecs;
import
(package) std
std
.
(module) std.stdio
Category 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:

  1. The lowest layer is the operating system layer. The two main schemes are Windows and Posix.

  2. C's stdio.h which unifies the two operating system schemes.

  3. std.stdio, this module, unifies the various stdio.h implementations into a high level package for D programs.

Source

std/stdio.d

@copyrightCopyright The D Language Foundation 2007-.@licenseBoost License 1.0.@authorsWalter Bright, Andrei Alexandrescu, Alex Rønne Petersen
stdio
:
(alias template) vibe_channel.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);
    }
}
@paramargs the items to write to stdout@throwsIn case of an I/O error, throws an StdioException.
writeln
;
import
(package) vibe
vibe
.
(package) vibe.core
core
.
(module) vibe.core.channel

Implements a thread-safe, typed producer-consumer queue.

@copyright© 2017-2019 Sönke Ludwig@authorsSönke Ludwig@licenseSubject to the terms of the MIT license, as written in the included LICENSE.txt file.
channel
:
(alias template) vibe_channel.createChannel = vibe.core.channel.createChannel(T, ulong buffer_size = 100)(ChannelConfig config = ChannelConfig.init)

Creates a new channel suitable for cross-task and cross-thread communication.

createChannel
;
import
(package) vibe
vibe
.
(package) vibe.core
core
.
(module) vibe.core.core

This module contains the core functionality of the vibe.d framework.

@copyright© 2012-2020 Sönke Ludwig@licenseSubject to the terms of the MIT license, as written in the included LICENSE.txt file.@authorsSönke Ludwig
core
:
(alias template) vibe_channel.runTask = vibe.core.core.runTask(ARGS...)(void delegate(ARGS) nothrow @safe task, auto ref ARGS args)

Runs a new asynchronous task.

task will be called synchronously from within the vibeRunTask call. It will continue to run until vibeYield() or any of the I/O or wait functions is called.

Note that the maximum size of all args must not exceed maxTaskParameterSize.

runTask
,
(alias) vibe_channel.sleep = void vibe.core.core.sleep(core.time.Duration timeout) @safe

Suspends the execution of the calling task for the specified amount of time.

Note that other tasks of the same thread will continue to run during the wait time, in contrast to core.thread.Thread.sleep``, which shouldn't be used in vibe.d applications.

Repeated sleep

As this method creates a new Timer every time, it is not recommended to use it in a tight loop. For functions that calls sleep frequently, it is preferable to instantiate a single Timer and reuse it, as shown in the following example:

void myPollingFunction () {
 Timer waiter = createTimer(null); // Create a re-usable timer
 while (true) {
  // Your awesome code goes here
  timer.rearm(timeout, false);
  timer.wait();
 }
}
@throwsMay throw an InterruptException if the task gets interrupted using Task.interrupt().
sleep
,
(alias) vibe_channel.yield = void vibe.core.core.yield() @safe

Suspends the execution of the calling task to let other tasks and events be handled.

Calling this function in short intervals is recommended if long CPU computations are carried out by a task. It can also be used in conjunction with Signals to implement cross-fiber events with no polling.

@throwsMay throw an InterruptException if Task.interrupt() gets called on the calling task.@seeyieldUninterruptible
yield
;
void
void D main()
main
()
{ auto
(local variable) vibe.core.channel.Channel!(int, 2LU) items
items
=
vibe.core.channel.Channel!(int, 2LU) vibe.core.channel.createChannel!(int, 2LU)(vibe.core.channel.ChannelConfig config = ChannelConfig(ChannelPriority.latency)) nothrow @safe

Creates a new channel suitable for cross-task and cross-thread communication.

createChannel
!(int, 2)();
int
(local variable) int produced
produced
;
bool
(local variable) bool attemptingThird
attemptingThird
;
auto
(local variable) vibe.core.task.Task producer
producer
=
vibe.core.task.Task vibe.core.core.runTask!()(void delegate() nothrow @safe task) nothrow @safe

Runs a new asynchronous task.

task will be called synchronously from within the vibeRunTask call. It will continue to run until vibeYield() or any of the I/O or wait functions is called.

Note that the maximum size of all args must not exceed maxTaskParameterSize.

runTask
(() nothrow {
try { foreach (
(local variable) int i
i
; 1 .. 6)
{ if (
(local variable) int i
i
== 3)
(local variable) bool attemptingThird
attemptingThird
= true;
(local variable) vibe.core.channel.Channel!(int, 2LU) items
items
.
void vibe.core.channel.Channel!(int, 2LU).put(int item) @safe

Enqueues an element.

This function may block the the event that the internal buffer is full.

put
(
(local variable) int i
i
);
(local variable) int produced
produced
=
(local variable) int i
i
;
}
(local variable) vibe.core.channel.Channel!(int, 2LU) items
items
.
void vibe.core.channel.Channel!(int, 2LU).close() nothrow @safe

Closes the channel.

A closed channel does not accept any new items enqueued using put and causes empty to return fals as soon as all preceeding elements have been consumed.

close
();
} catch (
(class) object.Exception

The base class of all errors that are safe to catch and handle.

In principle, only thrown objects derived from this class are safe to catch inside a catch block. Thrown objects not derived from Exception represent runtime errors that should not be caught, as certain runtime guarantees may not hold, making it unsafe to continue program execution.

Examples

bool gotCaught;
try
{
    throw new Exception("msg");
}
catch (Exception e)
{
    gotCaught = true;
    assert(e.msg == "msg");
}
assert(gotCaught);
Exception
(local variable) object.Exception error
error
) { assert(false,
(local variable) object.Exception error
error
.
(field) string object.Throwable.msg

A message describing the error.

msg
); }
}); while (!
(local variable) bool attemptingThird
attemptingThird
)
void vibe.core.core.yield() @safe

Suspends the execution of the calling task to let other tasks and events be handled.

Calling this function in short intervals is recommended if long CPU computations are carried out by a task. It can also be used in conjunction with Signals to implement cross-fiber events with no polling.

@throwsMay throw an InterruptException if Task.interrupt() gets called on the calling task.@seeyieldUninterruptible
yield
();
assert(
(local variable) int produced
produced
== 2 &&
(local variable) vibe.core.channel.Channel!(int, 2LU) items
items
.
ulong vibe.core.channel.Channel!(int, 2LU).bufferFill() nothrow @nogc @property @safe

Returns the current count of items in the buffer.

This function is useful for diagnostic purposes.

bufferFill
== 2);
int
(local variable) int sum
sum
= 0;
int
(local variable) int expected
expected
= 1;
int
(local variable) int next
next
;
while (
(local variable) vibe.core.channel.Channel!(int, 2LU) items
items
.
bool vibe.core.channel.Channel!(int, 2LU).tryConsumeOne(ref int dst, core.time.Duration timeout = max()) nothrow @safe

Attempts to consume a single element.

If no more elements are available and the channel has been closed, false is returned and dst is left untouched.

tryConsumeOne
(
(local variable) int next
next
))
{ assert(
(local variable) int next
next
==
(local variable) int expected
expected
++);
void vibe.core.core.sleep(core.time.Duration timeout) @safe

Suspends the execution of the calling task for the specified amount of time.

Note that other tasks of the same thread will continue to run during the wait time, in contrast to core.thread.Thread.sleep``, which shouldn't be used in vibe.d applications.

Repeated sleep

As this method creates a new Timer every time, it is not recommended to use it in a tight loop. For functions that calls sleep frequently, it is preferable to instantiate a single Timer and reuse it, as shown in the following example:

void myPollingFunction () {
 Timer waiter = createTimer(null); // Create a re-usable timer
 while (true) {
  // Your awesome code goes here
  timer.rearm(timeout, false);
  timer.wait();
 }
}

Examples

	import vibe.core.core : sleep;
	import vibe.core.log : logInfo;
	import core.time : msecs;

	void test()
	{
		logInfo("Sleeping for half a second...");
		sleep(500.msecs);
		logInfo("Done sleeping.");
	}
@throwsMay throw an InterruptException if the task gets interrupted using Task.interrupt().
sleep
(2.msecs);
(local variable) int sum
sum
+=
(local variable) int next
next
;
}
(local variable) vibe.core.task.Task producer
producer
.
void vibe.core.task.Task.join() @trusted
join
();
assert(
(local variable) int sum
sum
== 15);
assert(
(local variable) int produced
produced
== 5 &&
(local variable) int expected
expected
== 6);
void std.stdio.writeln!(string, int)(string __param_0, int __param_1) @safe

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);
    }
}
@paramargs the items to write to stdout@throwsIn case of an I/O error, throws an StdioException.
writeln
("consumed: ",
(local variable) int sum
sum
);
}