-
-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API for getting the current context (and do we even want/need it) #268
Comments
Taking the inspiration from JNI (https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/invocation.html), I have a new proposal: new structure with pointer to function to get the current context, e.g.
Some code skeleton to better illustrate this:
Maybe HPyVM should not be a pointer, but passed by value everywhere. It may be OK because it will be small and it would solve issues with management of the memory allocated for it. The recommendation would still be to pass the context around if possible. |
We probably need to define more clearly when a context can re-used. E.g. This approach will likely break sub-interpreters because there will be global C state storing a single context, but each interpreter will have it's own. It will also potentially interfere with GIL-less operation within a single interpreter where one can imagine it might be very useful to have one context per thread. This is already an issue for static types, but it would be good not to duplicate those issues in HPy. |
The idea was that you never "reuse" context. The context returned from
Maybe this could be useful for us too. Or maybe we want to disallow running HPy API from non Python threads.
The global state will be implementation detail of the given HPy implementation. I envisage a thread local global variable and if subinterpreters are migrated between threads, then the value of variable would have to be updated, otherwise the getting a context would be relatively cheap read from this global variable. Additionally, the "global" context could be pointing to different set of functions that may do some extra work or that use some other indirection. |
Summary of the suggestions so far:
Open questions:
Current module or any HPy handle can be argument to the closure creation function:
For now I think the API should return |
Edit: see summary of the discussion so far: #268 (comment)
Use-cases for an API that would get the current context:
int myhelper(PyObject* x)
one would createint myhelper_hpy(HPyContext *ctx, HPy x)
used in the new HPy code andint myhelper(PyObject* x)
would delegate to thattp_dealloc
?Some things to consider:
Maybe we should first focus on the use-cases. Collect all the relevant use-cases and better understand them.
Some preliminary suggestions:
HPy_GetContext()
: simple, but may look like it's OK to "cache" the context somewhere in some global state. May encourage not passing the context around as a parameter to helper functions and such, which should be the best practice.Still simple, but I think it communicates that the context should be used for limited period of time. Allows the Python engine to dispose the context in case it's needed or to take whatever action it needs (unpin some objects, ...). Debug mode can check that the contexts created with this API are properly left, but not via the current debug context mechanism, it would have to be a different mechanism, because
HPy_EnterContext
does not dispatch on the context.Trampolines:
This is more complicated, and requires some boilerplate code, but that could be an advantage, because we want to discourage abuse of this API. Advantage of this approach is that even when we "out of thin air" need to call into HPy code, we are still calling via a trampoline connected with some context, so there does not have to be one global context, i.e., subinterpreters could run on the same thread and we can still distinguish in which subinterpreter we are supposed to run the HPy code. Disadvantage: there has to be some place where to store the trampoline.
All in all I think that the trampolines would be useful only if we wanted to some longer term "get current context" API. If the API is supposed to be available only in some "legacy" mode and discouraged everywhere, maybe we do not need such complex solution.
Alternative would be getting some token from a context and passing that to the
HPy_EnterContext
:The text was updated successfully, but these errors were encountered: