Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Inside functions wait until does not work #722

Closed
ZiwKerman opened this issue Apr 3, 2015 · 3 comments
Closed

BUG: Inside functions wait until does not work #722

ZiwKerman opened this issue Apr 3, 2015 · 3 comments

Comments

@ZiwKerman
Copy link
Contributor

To reproduce run the following script

    declare function bugtest1 {

        print "waiting for 3 seconds".
        wait 3.
        print "done!".

        declare t to time:seconds + 5.

        print "waiting until time:seconds > t = time:seconds + 5".
        wait until time:seconds > t.
        print "done 5!".
    }

    bugtest1().

The second wait finishes instantly.

@Dunbaratu
Copy link
Member

wait until is implemented as a trigger.

Just to give an idea, here's how complex it is now:

at the top it makes this trigger:

push $time     ------.  <-- $wait-0000003 is a variable with this instruction's address location in it.
push "seconds"       |
getmember            |--- from your expression
push $t              |      "time:seconds < t"
gt             ------'
br.zero 4
endwait  // changes the CPU state back to "Running"
push $wait-0000003  // id of this trigger, i.e. the 3rd trigger the compiler made.
removeytrigger
EOF

Then down in the body where the wait statement actually existed, it turns that trigger on with:

push $wait-0000003 // id of this trigger
addtrigger true  // 'true' means "do turn on the CPU's Wait state".

So it sets the CPU's state to Waiting due to the 'true' parameter to addtrigger, and waits for the trigger to turn it back off again.

What's causing the problem is the fact that the trigger's scope is outside the scope of the function, so it's not allowed to see the functions' variables in its expression check. (it's probably falling back to a more global $t instead of the local one).

I see no reason at all for this to be implemented on the back of the already beleaguered trigger system, with its upper limit on instructions allowed and so on, when you could just do all the logic right there inline in the code like so:

wait 0.0000001
push $time     ------.
push "seconds"       |
getmember            |--- from your expression
push $t              |      "time:seconds < t"
gt             ------'
br.zero -6  // jump negative 6 instructions (up to wait 0.0000001)).

So it basically just spins in a loop forever checking the expression right there in the inline code, but in a way that only loops once per update rather than busy polling.

So much cleaner. Doing this will also solve issue #688, as it is caused by accidentally triggering the "trigger" logic when it should have not done so (using a zero wait makes the system think it's a WAIT UNTIL when it isn't).

@Dunbaratu
Copy link
Member

actually I think I got the gt/lt 's backward in my above examples typed out manually, but you get the gist of it.

Dunbaratu added a commit to Dunbaratu/KOS-1 that referenced this issue Apr 4, 2015
@Dunbaratu Dunbaratu added this to the v0.17.0 Punchlist milestone Apr 4, 2015
@Dunbaratu Dunbaratu self-assigned this Apr 4, 2015
@Dunbaratu Dunbaratu removed their assignment Apr 6, 2015
@Dunbaratu
Copy link
Member

Fixed when PR #726 got merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants