Last active
July 11, 2016 04:16
-
-
Save yyamasak/105fba57aafa7f2f2f57 to your computer and use it in GitHub Desktop.
An example to replace vwait with coroutine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| proc log {msg} { | |
| set ts [clock format [clock seconds] -format %T] | |
| puts "\[$ts\] $msg" | |
| } | |
| proc backtrace {} { | |
| set frames [list] | |
| for {set i 1} {$i < [info level]} {incr i} { | |
| set frameinfo [info frame $i] | |
| if {[dict exists $frameinfo cmd]} { | |
| lappend frames [dict get $frameinfo cmd] | |
| } | |
| } | |
| return $frames | |
| } | |
| proc init_process {} { | |
| global proceed | |
| set proceed wait | |
| } | |
| proc start_process {} { | |
| after 2000 end_process | |
| } | |
| proc end_process {} { | |
| global proceed | |
| set proceed go | |
| } | |
| proc sequence_1 {} { | |
| global proceed | |
| init_process | |
| start_process | |
| log "proceed=$proceed" | |
| vwait proceed | |
| log "proceed=$proceed" | |
| } | |
| proc sequence_2 {} { | |
| global proceed | |
| init_process | |
| set co [info coroutine] | |
| trace add variable ::proceed write [list traceproc $co] | |
| start_process | |
| log "proceed=$proceed" | |
| yield | |
| log "proceed=$proceed" | |
| } | |
| proc traceproc {co var key op} { | |
| trace remove variable ::proceed write [list traceproc $co] | |
| $co | |
| } | |
| sequence_1 | |
| coroutine co_sequence sequence_2 | |
| vwait forever | |
| # (bin) 2 % sequence_1 | |
| # [19:02:44] proceed=wait | |
| # [19:02:46] proceed=go | |
| # (bin) 3 % coroutine co_sequence sequence_2 | |
| # [19:02:46] proceed=wait | |
| # [19:02:48] proceed=go |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment