Prevent repeating call to proc before certain part of code is executed

129 views Asked by At

i have a tcl script used for test automation (with CarMaker ScriptControl interface)

In the script i am calling a proc where some code is executed that may take a few milliseconds before it returns. However, if at a certain point in this proc it gets called again, the second call will fail and cause an error.

I have no control about when the proc will be called, because the calls come from another application. What i am searching for is a way to prevent a parallel call to the function happening while a certain part of my code is being executed.

Something like this:

proc MyProc {args} {
    #Execute Code
    set val 0

    #---this part should not be interrupt
    #Execute non interrupt Code
    RegisterQuantities ($names)
    Foreach name {
        ReadQuantity $name
    }
    #------------------------------------

    return val
}

The calls come from a realtime PC that

1

There are 1 answers

1
Donal Fellows On

The simplest way of doing this is to create a global variable that says whether the code has reached the point where the procedure may be usefully called:

variable ready_for_call 0

proc ReadyForCall {} {
    variable ready_for_call
    set ready_for_call 1
}

proc MyProc {args} {
    variable ready_for_call
    if {!$ready_for_call} {
        # I don't know if you want a warning, or an error, or silent ignoring
        puts stderr "WARNING: called MyProc too early"
        # You might need to return a dummy value here; the default is an empty string
        return
    }
    #Execute Code
    ...
}

Then all you need to do is call ReadyForCall at the point where the command becomes callable (which you can probably characterise fairly well).