help on creating plugin

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

help on creating plugin

EstebanLM
 
Hi,
I need some help/advice for a plugin I'm writing.
I'm already downloaded VMMaker and managed to compile a new VM in OS/X,
and I already write and compile and test a simple plugin (and I have to
say the process is very simple, and all my problems were because I
never used Xcode before), but now, with the plugin running, comes the
"optimization" phase, and there I have many doubts:

1) An external plugin blocks the VM when primitive is called? if so,
how do I manage to avoid this?
2) what is the right way to use strings? now, I'm obtaining them as
parameters, but the string comes without final zero, so C does not
works fine... because of that, I'm copying them into a new variable
(memory allocated with malloc and freed after use), but it does not
seems to be very performant... is there a better way?
3) in general, what is the best way to return Strings into Squeak? I
have to create an instance with interpreterProxy>>instantiateClass:
each time?

Well, I have many doubts about how to optimize my code... where can I
look to see some example code?

Thanks,
Esteban


Reply | Threaded
Open this post in threaded view
|

Re: help on creating plugin

Igor Stasenko
 
2008/9/14 Esteban Lorenzano <[hidden email]>:

>
> Hi,
> I need some help/advice for a plugin I'm writing.
> I'm already downloaded VMMaker and managed to compile a new VM in OS/X, and
> I already write and compile and test a simple plugin (and I have to say the
> process is very simple, and all my problems were because I never used Xcode
> before), but now, with the plugin running, comes the "optimization" phase,
> and there I have many doubts:
>
> 1) An external plugin blocks the VM when primitive is called? if so, how do
> I manage to avoid this?

No way. Primitive implements the behavior of your method. You can't
run interpreter before it will return.
Of course you can use pthreads: create own native thread and pass all
you need to this thread using own synchronization scheme. Then you can
avoid blocking. But this comes handy if your task is parallelisable.
Some are simply can't be parallelised.

> 2) what is the right way to use strings? now, I'm obtaining them as
> parameters, but the string comes without final zero, so C does not works
> fine... because of that, I'm copying them into a new variable (memory
> allocated with malloc and freed after use), but it does not seems to be very
> performant... is there a better way?

you have to copy string into buffer and terminate it with 0 character.

> 3) in general, what is the best way to return Strings into Squeak? I have to
> create an instance with interpreterProxy>>instantiateClass: each time?
>
yes

> Well, I have many doubts about how to optimize my code... where can I look
> to see some example code?
>
you can look at plugins code, which you already having in VM.

> Thanks,
> Esteban
>
>
>



--
Best regards,
Igor Stasenko AKA sig.
Reply | Threaded
Open this post in threaded view
|

Re: help on creating plugin

Joshua Gargus-2
 
Igor Stasenko wrote:
 
2008/9/14 Esteban Lorenzano [hidden email]:
  
Hi,
I need some help/advice for a plugin I'm writing.
I'm already downloaded VMMaker and managed to compile a new VM in OS/X, and
I already write and compile and test a simple plugin (and I have to say the
process is very simple, and all my problems were because I never used Xcode
before), but now, with the plugin running, comes the "optimization" phase,
and there I have many doubts:

1) An external plugin blocks the VM when primitive is called? if so, how do
I manage to avoid this?
    

No way. Primitive implements the behavior of your method. You can't
run interpreter before it will return.
Of course you can use pthreads: create own native thread and pass all
you need to this thread using own synchronization scheme. Then you can
avoid blocking. But this comes handy if your task is parallelisable.
Some are simply can't be parallelised.

  

To elaborate on Igor's response, Squeak has a way to register a Semaphore object with the VM.  If you want native code to work asynchronously in another thread, you would write a primitive that would queue the task up for the worker thread, then returns immediately (so that bytecode interpretation can resume).  Then, when your worker thread is finished, it signals the semaphore.  Typically, you will have a Squeak Process looping and blocking on that semaphore; when the semaphore is signalled you know that there is data available to read back from the plugin (you would use a different primitive to read the data back).

As Igor says, you will often just want to wait for the primitive to return.  A good rule-of-thumb is to ask yourself, if your whole program was written in C, would this be a place that you would use a separate worker thread.  If not, then you probably don't want to do it for a Squeak primitive either.

Cheers,
Josh
Reply | Threaded
Open this post in threaded view
|

Re: help on creating plugin

EstebanLM
In reply to this post by EstebanLM
 
Hi,
Thanks for the answers... I'm trying to advance my plugin with your
advices, but is a little hard to understand (and no much time
available).
I have a question, maybe very basic, but I'm a newbie at plugin development:

Is there a place where I can see all the Slang defined methods? I'm
asking because in some places I see a "self strlen: blah" and I would
like to know which ones are available for use,  instead of fill my code
of "self cCode: 'blah') sentences.

Many thanks,
Esteban


Reply | Threaded
Open this post in threaded view
|

Re: Re: help on creating plugin

johnmci
 
There is no slang defined methods.

The rule is

self blafoobar: thing
becomes blafoobar(thing)   when it builds the C code because it is  
methodName: argument  becomes methodName(argument)

For methods with more than one argument SLANG concatinates the parts  
into one Name.
Thus you can construct something like

self mem:  foo c: bar py: too

then becomes
   memcpy(foo,bar,too);

Or so I remember

You can also use this to code procedures in SLANG for the plugin.
For example in the GStreamer plugin I code  a SLANG method

cb: element new: newPad pad: data
        "Ok the trick here is on the signal pad_add we may have multiple pads  
being created.
        Therefore we must find the sink pad that can handle the request. Note  
for ogg you could have the
        audio and video stream for example"

        | max result gstPadValue gstCapsValue |

        self var: #gstPadValue declareC: 'GstPad*  gstPadValue'.
        self var: #gstCapsValue declareC: 'GstCaps*  gstCapsValue'.
        self var: #data declareC: 'sqInt  *data'.


that becomes a C procedure
static sqInt cbnewpad(sqInt element, sqInt newPad, sqInt  *data)


On Sep 16, 2008, at 8:17 PM, Esteban Lorenzano wrote:

> Hi,
> Thanks for the answers... I'm trying to advance my plugin with your  
> advices, but is a little hard to understand (and no much time  
> available).
> I have a question, maybe very basic, but I'm a newbie at plugin  
> development:
>
> Is there a place where I can see all the Slang defined methods? I'm  
> asking because in some places I see a "self strlen: blah" and I  
> would like to know which ones are available for use,  instead of  
> fill my code of "self cCode: 'blah') sentences.
>
> Many thanks,
> Esteban
>
>

--
=
=
=
========================================================================
John M. McIntosh <[hidden email]>
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
=
=
=
========================================================================


Reply | Threaded
Open this post in threaded view
|

Re: Re: help on creating plugin

Bert Freudenberg
 
In the RomePlugin I added a hack that translates

        self cairoFooBar: x baz: y
as

        cairo_foo_bar(x, y)

by matching selectors beginning with "cairo". Very handy for  
underscore-hating Slang hackers, though not exactly a beginner's  
topic ;)

- Bert -

Am 17.09.2008 um 17:22 schrieb John M McIntosh:

> There is no slang defined methods.
>
> The rule is
>
> self blafoobar: thing
> becomes blafoobar(thing)   when it builds the C code because it is  
> methodName: argument  becomes methodName(argument)
>
> For methods with more than one argument SLANG concatinates the parts  
> into one Name.
> Thus you can construct something like
>
> self mem:  foo c: bar py: too
>
> then becomes
>  memcpy(foo,bar,too);
>
> Or so I remember
>
> You can also use this to code procedures in SLANG for the plugin.
> For example in the GStreamer plugin I code  a SLANG method
>
> cb: element new: newPad pad: data
> "Ok the trick here is on the signal pad_add we may have multiple  
> pads being created.
> Therefore we must find the sink pad that can handle the request.  
> Note for ogg you could have the
> audio and video stream for example"
>
> | max result gstPadValue gstCapsValue |
>
> self var: #gstPadValue declareC: 'GstPad*  gstPadValue'.
> self var: #gstCapsValue declareC: 'GstCaps*  gstCapsValue'.
> self var: #data declareC: 'sqInt  *data'.
>
>
> that becomes a C procedure
> static sqInt cbnewpad(sqInt element, sqInt newPad, sqInt  *data)
>
>
> On Sep 16, 2008, at 8:17 PM, Esteban Lorenzano wrote:
>
>> Hi,
>> Thanks for the answers... I'm trying to advance my plugin with your  
>> advices, but is a little hard to understand (and no much time  
>> available).
>> I have a question, maybe very basic, but I'm a newbie at plugin  
>> development:
>>
>> Is there a place where I can see all the Slang defined methods? I'm  
>> asking because in some places I see a "self strlen: blah" and I  
>> would like to know which ones are available for use,  instead of  
>> fill my code of "self cCode: 'blah') sentences.
>>
>> Many thanks,
>> Esteban
>>
>>
>
> --
> =
> =
> =
> =
> =
> ======================================================================
> John M. McIntosh <[hidden email]>
> Corporate Smalltalk Consulting Ltd.  http://
> www.smalltalkconsulting.com
> =
> =
> =
> =
> =
> ======================================================================
>
>

Reply | Threaded
Open this post in threaded view
|

Re: help on creating plugin

Yoshiki Ohshima-2
In reply to this post by Igor Stasenko
 
> > 2) what is the right way to use strings? now, I'm obtaining them as
> > parameters, but the string comes without final zero, so C does not works
> > fine... because of that, I'm copying them into a new variable (memory
> > allocated with malloc and freed after use), but it does not seems to be very
> > performant... is there a better way?
>
> you have to copy string into buffer and terminate it with 0
> character.

  Yes.

  But if possible, avoid using malloc() and free().  Instead, use the
memory in Squeak's object memory.  That way you can avoid
fragmentation.  Just right before the call to primitive (or in the
primitive), copy the content of the orignal string into another String
whose size is one bigger than original (by #replaceFrom:to:with:), and
do basicAt: 0 at the end.  You could possibly keep the buffer around
and reuse it, as long as the size of the String fits in the buffer.

> > 3) in general, what is the best way to return Strings into Squeak? I have to
> > create an instance with interpreterProxy>>instantiateClass: each time?
> >
> yes

  If you know the size of string that is returned, again you can pass
an instance of String from image to the primitive and fill it in.
Primitive code looks cleaner in that way.

> > Well, I have many doubts about how to optimize my code... where can I look
> > to see some example code?
> >
> you can look at plugins code, which you already having in VM.

  Or, you can just show the code to us!

-- Yoshiki