ChezScheme icon indicating copy to clipboard operation
ChezScheme copied to clipboard

Multiple embedded sessions

Open actondev opened this issue 5 years ago • 4 comments

I'm wishing to embed chez in a c++ application. Is it possible to initiate multiple independent scheme sessions?

I see in https://github.com/cisco/ChezScheme/blob/b7f161b/c/scheme.c#L38 that the heap_state is static. So.. I guess not possible? Would there be any plans to support this?

actondev avatar May 08 '20 19:05 actondev

One thing you can do right now is spawn different environments and execute your code in those environments with eval. It's not quite the same as completely different sessions since you can change global and thread parameters that are shared across all environments, but it's close and you can hide the effects of modifying parameters with parameterize.

You can either construct fresh environments with environment or copy the interaction-environment with copy-environment

gwatt avatar May 08 '20 20:05 gwatt

thanks @gwatt ! Wasn't aware of the environments thing, will look into that at some point. For now my issue can be solved, but however I think the modularization of the code (eliminating static) should be done at some point.

More specifically, my issue was when making a dynamic C++ library using scheme, to be loaded by a host application. The host application could instantiate multiple instances of this library. (more specifically it's about doing a VST plugin, being loaded by a VST host)

The issue that I stumbled upon was that scheme was already initiated when a 2nd instance of my plugin is loaded, thus crashing.

Thanks again.

actondev avatar May 19 '20 10:05 actondev

If it's acceptable to have plugins sharing the same scheme runtime but with their own scheme environments, you can check that the scheme system is already initialized and then skip the rest as appropriate. Sscheme_init checks the current state and then calls a given procedure if the initialization process is underway or complete. If the given procedure is NULL, the system aborts.

int initialized = 0;

void set_initialized(void) {
    initialized = 1;
}

// on plugin initialization
Sscheme_init(&set_initialized);
if (!initialized) {
    Sregister_bootfile(...);
    Sbuild_heap(...);
}

gwatt avatar May 19 '20 15:05 gwatt

Yeah that's more or less what I thought of doing. One note: I think the init function should be moved inside the if block.

For reference, that's the error I was getting error (Sscheme_init): call Sscheme_deinit first to terminate

actondev avatar May 19 '20 18:05 actondev