RejectedSoftware Forums

Sign up

Understanding fibers/tasks

Hello,

I'm trying to understand how fibers/tasks work in vibe.d.

module app;

import std.stdio;
import vibe.core.core;
import core.time;

void main()
{
    runTask({
        foreach(immutable i; 0..5)
        {
            writeln(i);
            sleep(dur!"msecs"(100));
        }
    });

    runTask({
        foreach(immutable i; 5..10)
        {
            writeln(i);
            sleep(dur!"msecs"(1000));
        }
    });

    runEventLoop();
}

This source code produces

0
5
6
7
8
9
1
2
3
4

Which makes sense. However, when adding a yield like so:

module app;

import std.stdio;
import vibe.core.core;
import core.time;

void main()
{
    runTask({
        foreach(immutable i; 0..5)
        {
            writeln(i);
            sleep(dur!"msecs"(100));
        }
    });

    runTask({
        foreach(immutable i; 5..10)
        {
            writeln(i);
            sleep(dur!"msecs"(1000));
            yield;
        }
    });

    runEventLoop();
}

It produces this:

0
5
1
6
2
7
3
8
4
9

I would expect:

0
5
1
2
3
4
6
7
8
9

Because after printing 5 (the first number), it yields and the other loop is the active fiber. The 5..10 foreach loop would continue execution after that fiber is done. But it seems like that 0..5 foreach loop seems to 'yield' at every sleep.

What am I missing here?

Re: Understanding fibers/tasks

Am 05.02.2014 15:49, schrieb Kelet:

Hello,

I'm trying to understand how fibers/tasks work in vibe.d.
(...)

I think you have discovered a bug in the new timer code. I'll
look into it.

Generally, that yield() shouldn't make a difference, except if the
other task also called yield() just before, or if the other task has
an event pending that would wake it up. But in this scenario, where both
tasks are mostly sleeping (i.e. they are already yielded), it should
really have no effect.

Re: Understanding fibers/tasks

On Wed, 05 Feb 2014 17:05:28 +0100, Sönke Ludwig wrote:

Am 05.02.2014 15:49, schrieb Kelet:

Hello,

I'm trying to understand how fibers/tasks work in vibe.d.
(...)

I think you have discovered a bug in the new timer code. I'll
look into it.

Generally, that yield() shouldn't make a difference, except if the
other task also called yield() just before, or if the other task has
an event pending that would wake it up. But in this scenario, where both
tasks are mostly sleeping (i.e. they are already yielded), it should
really have no effect.

Have you been using the latest GIT master of vibe.d on DMD 2.064.2 by any chance? There was an error in the DMD <2.065 compatibility code, that is now fixed with cae5a1a.

Re: Understanding fibers/tasks

On Wed, 05 Feb 2014 16:27:29 GMT, Sönke Ludwig wrote:

Have you been using the latest GIT master of vibe.d on DMD 2.064.2 by any chance? There was an error in the DMD <2.065 compatibility code, that is now fixed with cae5a1a.

Thanks for the fix,
Yes, I am using Git master w/DMD 2.064.2. I need the fix for UDP recv that you've committed. Are you planning on making a new tag or beta release soon? That would be very helpful for me.

Re: Understanding fibers/tasks

Am 05.02.2014 17:59, schrieb Kelet:

On Wed, 05 Feb 2014 16:27:29 GMT, Sönke Ludwig wrote:

Have you been using the latest GIT master of vibe.d on DMD 2.064.2 by any chance? There was an error in the DMD <2.065 compatibility code, that is now fixed with cae5a1a.

Thanks for the fix,
Yes, I am using Git master w/DMD 2.064.2. I need the fix for UDP recv that you've committed. Are you planning on making a new tag or beta release soon? That would be very helpful for me.

I'd just like to fix the "unowned TCP connection" error that you got and
finish the SSL certificate validation branch (not much work). After
that, I'd tag another beta or RC and then soon afterwards make a new
release.