[Mono-dev] slow mono_runtime_invoke

Zoltan Varga vargaz at gmail.com
Fri Mar 10 13:33:19 EST 2006


                           Hi,

  You can create delegates from static methods as well, and pass them to
native code in the same manner.

               Zoltan


On 3/10/06, Thomas Grill <gr at grrrr.org> wrote:
> Hi Miguel,
> thank you, that's very helpful for my general understanding.
> Now, there's a complication. In your example, the delegate is bound to a
> specific instance of the MyMethod class. Is there a fast way to call a
> specific class method on an instance of the class just when i want to
> call it?
> In my application, i use a number of callbacks which are class, not
> instance specific. It would be great to save the memory needed to keep
> the delegates for each instance.
>
> greetings,
> Thomas
>
>
> Miguel de Icaza schrieb:
> > Hello,
> >
> >
> >> i'd like to use mono inside a realtime system, which means that i need
> >> to make the native->managed transition as cheap as possible.
> >> Since mono_runtime_invoke is very slow, i'd like to ask whether there
> >> are strategies to speed this up.
> >>
> >
> > Do not use mono_runtime_invoke, which is dynamic.
> >
> > Instead define a delegate in C#, assign a value to it (to point to the
> > method you want to call), then P/Invoke into unmanaged land to register
> > the delegate, and use that in unmanaged land to call back.
> >
> > Something like:
> >
> > class Helper {
> >       delegate void Callback (void);
> >
> >       [DllImport ("binding")]
> >       extern static void RegisterCallback (Callback cb);
> > }
> >
> > class MyMethod {
> >       Callback cb;
> >
> >       MyMethod ()
> >       {
> >               cb = new Callback (my_callback);
> >               Helper.RegisterCallback (cb);
> >       }
> >
> >       void my_callback ()
> >       {
> >       }
> > }
> >
> > On the C side, you use:
> >
> > typedef void (*Callback)(void);
> > Callback the_cb;
> >
> > void RegisterCallback (Callback cb)
> > {
> >       the_cb = cb;
> > }
> >
> > Whenever you need to call into "my_callback" in managed land, you just
> > call "the_cb" from C:
> >
> > void Do ()
> > {
> >       (*the_cb) ();
> > }
> >
> >
> >
>
> --
> Thomas Grill
> http://grrrr.org
>
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>



More information about the Mono-devel-list mailing list