Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 0f5051e4f2d68be3fbf6fa54fb25a76aec42c9c6
Ticket: 3008307fffffffffffffffffffffffffffffffff
callerPtr chain does not traverse coro floor
User & Date: msofer on 2010-05-31 00:46:35
Changes

  1. icomment:
    Without yet looking at the tracing code, what SHOULD happen in that
    simplest script in terms of CF and Tcl stack management is [1]:
    
    1. interp is created with its main Tcl stack (execEnv0) and CF ptr rootFramePtr (frame0); both things are allocated on the heap. The script is evaluated in this "context"
    
    2. When [boom] is invoked at the end of the script:
    .    - a frame1 (allocated in execEnv0) is pushed: [boom]'s CF
    .    - [boom]'s body is evaluated
    .    - [cc] is invoked in frame1
    
    3. When [cc] is invoked from [boom]'s body:
    .    - a frame2 (allocated in execEnv0) is pushed: [cc]'s CF
    .    - [cc]'s body is evaluated
    .    - [coroutine] is invoked in frame2
    
    4. When [coroutine] is invoked from [cc]'s body:
    .    - the coroutineData (CD) struct is allocated on the heap, the command
    [C] is created
    .    - frame2 is saved in CD as the caller context; the running context is
    set to NULL
    .    - a new Tcl stack (execEnv1) is created for the coroutine; the
    interp's execEnv is set to execEnv1 so that all new stack allocs will
    happen here. The old execEnv0 is saved in CD->callerEEPtr
    .    - the coroutine command "c" is evaluated AS A SCRIPT [2] in frame2: it
    is first compiled, then the bytecodes are sent to TEBC
    .    - TEBC sees that this is a coroutine's first run, and sets the
    interp's CF context to the root frame0 (so that the coro runs in uplevel
    #0)
    .    - the command [c] is invoked in frame0
    
    5. When [c] is invoked from [coroutine]'s C implementation:
    .    - a frame3 (allocated in execEnv1) is pushed: [c]'s CF
    .    - [c]'s body is evaluated in frame3
    .    - [yield] is invoked in frame 3
    
    6. When [yield] is invoked from [c]'s body:
    .    - TEBC suspends execution of [c]'s bytecodes, saves its internal state
    in execEnv1 and then resets the interp's execEnv to execEnv0
    .    - NRCoroutineCallerCallback (the last callback in execEnv0's nre
    stack) saves the coroutine's running CF "context" (frame3) in the CD
    struct, then restores the interp's CF context to the caller's (frame2)
    .    - the caller script ([cc]'s body) resumes evaluation; nothing left to
    do, TEBC returns
    .    - InterpProcNR2 pops frame2 from execEnv0's Tcl stack; interp is back
    to (execEnv0, frame1)
    .    - control is back to [boom]'s script: [C] is invoked in frame1
    
    7. When [C] is invoked from [boom]'s body:
    .    - the current context frame1 is saved in CD->caller
    .    - CD->running (frame3) is set as the interp's current CF context
    .    - execEnv1 is set as the current tcl stack, execEnv0 is saved in CD->callerEEPtr
    .    - TEBC resumes execution of [c]' bytecodes: nothing left to do, it
    returns
    .    - InterpProcNR2 pops frame3 from execEnv1's Tcl stack; interp is now
    at (execEnv1, rootFramePtr)
    
    8. The return causes execEnv1 to wind down completely: [c] returns, the
    first nre callback in execEnv1 (NRCoroutineExitCallback) is run:
    .    - the [C] command is deleted
    .    - execEnv1 is freed, interp's execEnv is set execEnv0
    .    - the context is set to CD->caller (ie, frame1)
    
    9. Control returns to execEnv0's nre stack: NRCoroutineCallerCallback
    notices that the coro is dead, it frees the CD struct
    
    10. TEBC continues execution of [boom]'s body
    .    - nothing left to do, it returns
    .    - InterpProcNR2 pops frame1 from execEnv0's Tcl stack and frees it
    .    - frame0 is back to being the current CF
    
    Step 7. is the crucial one: the CF stack is left in a "very difficult to
    navigate" state, the expectation is that nobody should navigate it. My
    guess is that the crash probably happens on error, when the Tip 348
    mechanism does try to make it through the chasm. The coro mechanism does a
    lot of the actual stack management through nre callbacks, saving data in the CD
    struct; this is not reflected directly in the CF stack.
    
    [1] the NRE callback stack plays an important role too, it is partially
    omitted from this description to clarify the happenings in the CF and
    execEnv subworlds
    
    [2] technical detail: we need to evaluate it as a compiled script in order
    to insure that TEBC is running; this is needed for proper management of
    yet-another stack of structs that is maintained by TEBC. You don't (yet?)
    want to know.
    
    
  2. login: "msofer"
  3. mimetype: "text/plain"
  4. username: "msofer"