[Mono-dev] Mixed Mode Assemblies

Tom Spink tspink at gmail.com
Wed Jul 20 11:17:52 EDT 2011

On 20 July 2011 16:05, Robert Jordan <robertj at gmx.net> wrote:

> Hi Tom!

Hi Robert!

Nice :) Not exactly "Mixed Mode Assemblies" though, because the
> really interesting part (calling unmanaged code w/out having to
> go through p/invoke) remains the same pain.
Absolutely - I've been thinking a lot about that direction, and I think
you'd probably need to write/extend a compiler to do it
properly/efficiently.  What I've made is just a wrapper around the Mono
embedding API, exposing classes as symbols in a shared object.

To properly build a mixed-mode assembly, I guess you'd have to have a
compiler that was aware of it - I think.

> Out of curiosity: How are you handling more than one of those
> "mixed assemblies" in the same process? Is there a function
> inside libmono that can be used to detect an already initialized
> runtime?

An interesting question - because I was just pondering that myself.  Answer
is, I haven't thought about it yet and so haven't tackled the problem.  I am
hacking away at it just now, so eventually I'll come across the issue!

> On the generated stubs: are you using thunks or mono_runtime_invoke?
> Are you rewriting the signature of the stubs to be HRESULT-like
> (to be able to catch/detect managed exceptions)?

I'm using mono_method_get_unmanaged_thunk, and at the moment if any
exception was raised (by use of the exception output parameter in the
thunk), it just gets re-thrown in situ, and hence not handled by the calling
code.  What I've done is make GCC put the stubs into their own linker
section that's writable (so as to make it possible to patch the stubs at
runtime), and to this end, I'd like to re-write the entire stub on first
call, after it's gotten the unmanaged thunk, to directly call the function
pointer, thus reducing the overhead of the stub.

It's an interesting problem to tackle, because you need to provide this
exception to the calling code somehow, but I'm trying to limit the amount of
information exposed to the callers, as I wanted it to be as close to native
as possible.  So,the stubs, at the moment, are just direct copies of the
managed method signatures (plus a *this parameter for instance methods).

Let me see if I can get the code uploaded somewhere.

Oh, and another interesting problem to solve is that of multi-threading,
firstly as you need to call mono_thread_attach when using threads and the
embedding API, and secondly race conditions between initialising the runtime
and calling other functions.  It may have to be some kind of terrible lock,
or something.

> Robert

-- Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20110720/1e306cf8/attachment-0001.html 

More information about the Mono-devel-list mailing list