vibe_concurrency.dhover×31all
#!/usr/bin/env dub
/+ dub.sdl:
    name "vibe_concurrency"
    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_concurrency.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.concurrency

Functions and structures for dealing with threads and concurrent access.

This module is modeled after std.concurrency, but provides a fiber-aware alternative to it. All blocking operations will yield the calling fiber instead of blocking it.

@copyright© 2013-2014 Sönke Ludwig@licenseSubject to the terms of the MIT license, as written in the included LICENSE.txt file.@authorsSönke Ludwig
concurrency
:
(alias template) vibe_concurrency.async = vibe.core.concurrency.async(CALLABLE, ARGS...)(CALLABLE callable, ARGS args) if (is(typeof(callable(args)) == ReturnType!CALLABLE))

Starts an asynchronous computation and returns a future for the result value.

If the supplied callable and arguments are all weakly isolated, vibe.core.core.runWorkerTask will be used to perform the computation in a separate worker thread. Otherwise, vibe.core.core.runTask will be used and the result is computed within a separate task within the calling thread.

@paramcallable A callable value, can be either a function, a delegate, or a user defined type that defines an opCall.@paramargs Arguments to pass to the callable.@returnsReturns a Future object that can be used to access the result.@seeisWeaklyIsolated
async
;
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) vibe_concurrency.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
;
void
void D main()
main
()
{ auto
(local variable) vibe.core.concurrency.Future!int slow
slow
=
vibe.core.concurrency.Future!int vibe.core.concurrency.async!(int function() @safe)(int function() @safe callable) nothrow @safe

Starts an asynchronous computation and returns a future for the result value.

If the supplied callable and arguments are all weakly isolated, vibe.core.core.runWorkerTask will be used to perform the computation in a separate worker thread. Otherwise, vibe.core.core.runTask will be used and the result is computed within a separate task within the calling thread.

Examples

	import vibe.core.core;
	import vibe.core.log;

	void test()
	{
		auto val = async({
			logInfo("Starting to compute value in worker task.");
			sleep(500.msecs); // simulate some lengthy computation
			logInfo("Finished computing value in worker task.");
			return 32;
		});

		logInfo("Starting computation in main task");
		sleep(200.msecs); // simulate some lengthy computation
		logInfo("Finished computation in main task. Waiting for async value.");
		logInfo("Result: %s", val.getResult());
	}
	int sum(int a, int b)
	{
		return a + b;
	}

	static int sum2(int a, int b)
	{
		return a + b;
	}

	void test()
	{
		// Using a delegate will use runTask internally
		assert(async(&sum, 2, 3).getResult() == 5);

		// Using a static function will use runTaskWorker internally,
		// if all arguments are weakly isolated
		assert(async(&sum2, 2, 3).getResult() == 5);
	}
@paramcallable A callable value, can be either a function, a delegate, or a user defined type that defines an opCall.@paramargs Arguments to pass to the callable.@returnsReturns a Future object that can be used to access the result.@seeisWeaklyIsolated
async
({
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
(30.msecs);
return 1; }); auto
(local variable) vibe.core.concurrency.Future!int fast
fast
=
vibe.core.concurrency.Future!int vibe.core.concurrency.async!(int function() @safe)(int function() @safe callable) nothrow @safe

Starts an asynchronous computation and returns a future for the result value.

If the supplied callable and arguments are all weakly isolated, vibe.core.core.runWorkerTask will be used to perform the computation in a separate worker thread. Otherwise, vibe.core.core.runTask will be used and the result is computed within a separate task within the calling thread.

Examples

	import vibe.core.core;
	import vibe.core.log;

	void test()
	{
		auto val = async({
			logInfo("Starting to compute value in worker task.");
			sleep(500.msecs); // simulate some lengthy computation
			logInfo("Finished computing value in worker task.");
			return 32;
		});

		logInfo("Starting computation in main task");
		sleep(200.msecs); // simulate some lengthy computation
		logInfo("Finished computation in main task. Waiting for async value.");
		logInfo("Result: %s", val.getResult());
	}
	int sum(int a, int b)
	{
		return a + b;
	}

	static int sum2(int a, int b)
	{
		return a + b;
	}

	void test()
	{
		// Using a delegate will use runTask internally
		assert(async(&sum, 2, 3).getResult() == 5);

		// Using a static function will use runTaskWorker internally,
		// if all arguments are weakly isolated
		assert(async(&sum2, 2, 3).getResult() == 5);
	}
@paramcallable A callable value, can be either a function, a delegate, or a user defined type that defines an opCall.@paramargs Arguments to pass to the callable.@returnsReturns a Future object that can be used to access the result.@seeisWeaklyIsolated
async
({
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
(5.msecs);
return 2; }); const
(local variable) const(int) a
a
=
(local variable) vibe.core.concurrency.Future!int slow
slow
.
int vibe.core.concurrency.Future!int.getResult() ref @safe

Returns the computed value.

This function waits for the computation to finish, if necessary, and then returns the final value. In case of an uncaught exception happening during the computation, the exception will be thrown instead.

getResult
();
const
(local variable) const(int) b
b
=
(local variable) vibe.core.concurrency.Future!int fast
fast
.
int vibe.core.concurrency.Future!int.getResult() ref @safe

Returns the computed value.

This function waits for the computation to finish, if necessary, and then returns the final value. In case of an uncaught exception happening during the computation, the exception will be thrown instead.

getResult
();
assert(
(local variable) const(int) a
a
* 10 +
(local variable) const(int) b
b
== 12);
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
("joined: ",
(local variable) const(int) a
a
* 10 +
(local variable) const(int) b
b
);
}