Possibility of implementing a threadsafe/non-threadsafe switch

I'm currently exploring the possiblity of introducing Igor threads in a project of mine.

As the project is quite vast we make use of debugging techniques like assertions using a user defined function ASSERT. These assertions use GetRtStackInfo to printout the call chain. Now GetRtStackInfo is not threadsafe and thus I think I'm currently stuck with either removing GetRtStackInfo from ASSERT and thus loosing valuable information on error, or completely duplicating all callers of ASSERT, one call ASSERT and threadsafe ones call ASSERT_THREADSAFE.

What I would like to do, and tried also in an XOP is
E.g.
threadsafe Function proto_ts()
    print "proto_ts called"
End

Function proto()
    print "proto called"
End

Function test()
    print "test"
    Abort
End

threadsafe Function test_ts()
    print "test_ts"
    AbortOnValue 1, 1
End

threadsafe Function RunningInMainThread()
    // can be easily implemented in XOP
    return 1
End

/// Function should execute `f` if running
/// in the main thread and `f_ts` otherwise
threadsafe Function MySwitch(f, f_ts)
    string f, f_ts
    if(RunningInMainThread())
        FUNCREF proto func = $f
        // does not complile!
        func()
    else
        FUNCREF proto_ts func_ts = $f_ts
        func_ts()
    endif
End


Call as MySwitch("test", "test_ts"). The idea is that the function test can use non-threadsafe functions and is only called from the main thread and test_ts is called from preemptive threads.

But minor problem of my idea is that the code does not compile. Any ideas of ways to achieve what I want to do?

One way I've found, but which is useless for GetRTStackInfo, is to use Execute if called from the main thread.