You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Problem statement/motivation:
An old problem: Not being able to pass a "void *context" along with a callback function, when calling into existing APIs which did not plan for such a context argument (maybe third party code with C ABI).
Still inexplicably to me, WNDPROC is such an example.
Most developers targeting any form of C-like ABI nowadays think of this, but not all. For some things, like proprietary lab equipment drivers, public APIs are often more of an afterthought.
Thunks as potential general solution:
Closures can solve this when implemented in a specific way, but not without bringing the same issues as thunks.
C/C++ in particular have occasionally been coping with this problem by resorting to thunk generation through third party libraries (Windows ATL offers a way, there are various other, awkwardly platform dependent libraries for x86).
Due to the nature of the problem, this is at the very least ISA-dependent.
I haven't found a way to emulate what thunks can do in Odin, and I haven't found any discussion about the concept either.
Thunks are a major headache when trying to apply them generally, and I suspect Odin is such a case. Reasons include AOT platforms, security concerns, debugger/stack trace support, problems with stack unwinding (including crazy stuff like longjmp), calling convention conflicts, and conflicts with certain optimizations. And that's on top of the much easier application-level problems like thread-safety and having to explicitly manage that memory.
I'd be interested to know whether this is something that is deemed useful enough to even be discussed further.
Additional notes:
There are other, fundamentally different solutions to the problem; I have always believed this one to be the least amount of friction.
Doing this through the implicit context doesn't solve the problem, unless it could have been trivially solved in a different way to begin with, such as a global variable, or thread local storage.
I have seen runtimes that support debugging thunks, or at least preserving them in the stack trace. I do not know how that works but I image it's complicated.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Problem statement/motivation:
An old problem: Not being able to pass a "
void *context
" along with a callback function, when calling into existing APIs which did not plan for such a context argument (maybe third party code with C ABI).Still inexplicably to me,
WNDPROC
is such an example.Sometimes, very specific workarounds exist.
Most developers targeting any form of C-like ABI nowadays think of this, but not all. For some things, like proprietary lab equipment drivers, public APIs are often more of an afterthought.
Thunks as potential general solution:
Closures can solve this when implemented in a specific way, but not without bringing the same issues as thunks.
C/C++ in particular have occasionally been coping with this problem by resorting to thunk generation through third party libraries (Windows ATL offers a way, there are various other, awkwardly platform dependent libraries for x86).
Due to the nature of the problem, this is at the very least ISA-dependent.
I haven't found a way to emulate what thunks can do in Odin, and I haven't found any discussion about the concept either.
Thunks are a major headache when trying to apply them generally, and I suspect Odin is such a case. Reasons include AOT platforms, security concerns, debugger/stack trace support, problems with stack unwinding (including crazy stuff like
longjmp
), calling convention conflicts, and conflicts with certain optimizations. And that's on top of the much easier application-level problems like thread-safety and having to explicitly manage that memory.I'd be interested to know whether this is something that is deemed useful enough to even be discussed further.
Additional notes:
There are other, fundamentally different solutions to the problem; I have always believed this one to be the least amount of friction.
Doing this through the implicit context doesn't solve the problem, unless it could have been trivially solved in a different way to begin with, such as a global variable, or thread local storage.
I have seen runtimes that support debugging thunks, or at least preserving them in the stack trace. I do not know how that works but I image it's complicated.
Beta Was this translation helpful? Give feedback.
All reactions