www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4790] New: Wrong code when updating struct member value from fiber

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4790

           Summary: Wrong code when updating struct member value from
                    fiber
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: dsimcha yahoo.com


--- Comment #0 from David Simcha <dsimcha yahoo.com> 2010-09-02 20:31:20 PDT ---
This is a test case produced from an experiment with converting opApply to
ranges using fibers:

import core.thread, std.traits, std.range, std.stdio;

struct OpApplyToRange(Iterable) {
    Fiber fiber;
    ForeachType!Iterable _front;
    Iterable iterable;

    void doLoop() {
        stderr.writeln("_front's address from fiber:  ", &_front);
        foreach(elem; iterable) {
            stderr.writefln("Assigning %s to front", elem);
            _front = elem;
            stderr.writeln("_front has value ", _front);
            Fiber.yield();
            stderr.writeln("Resumed with value ", _front);
        }

        stderr.writeln("doLoop done.");
    }

    this(Iterable iterable) {
        this.iterable = iterable;
        fiber = new Fiber(&doLoop);
        fiber.call();
    }

    void popFront() {
        fiber.call();
    }
}

void main() {
    auto oar = OpApplyToRange!(int[])([1,2,3]);
    stderr.writeln("_front's address from main:  ", &oar._front);

    foreach(i; 0..3) {
        stderr.writeln("Calling fiber sees:  ", oar._front);
        oar.popFront();
    }
}

_front's address from fiber:  18FE34
Assigning 1 to front
_front has value 1
_front's address from main:  18FE24
Calling fiber sees:  1
Resumed with value 1
Assigning 2 to front
_front has value 2
Calling fiber sees:  1
Resumed with value 2
Assigning 3 to front
_front has value 3
Calling fiber sees:  1
Resumed with value 3
doLoop done.

Apparently the same variable somehow has a different address when viewed from
the main thread vs. fron a fiber, leading to some rather interesting
consequences, like updates made from the fiber not being visible in the main
function. (??????????)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 02 2010
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4790


David Simcha <dsimcha yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #1 from David Simcha <dsimcha yahoo.com> 2010-09-03 16:22:15 PDT ---
(Slaps self in forehead.)  Duh, of course.  When I take the address of doLoop()
it's taking the address of a stack variable.  Of course this isn't going to
work.  It needs to be a class.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 03 2010