Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Writing event hooks called from C extensions
Matthijs Langenberg
5/27/2008 7:45:00 PM
Just for educational purposes, I'm trying to write a Ruby-C extension
that allows me to hook into Carbon's event handler. So I can handle
mouse clicks in Ruby.
Mouse clicks (like all events) happen asynchronously, meaning that
something needs to be running inside a seperate thread, but I find it
hard to grasp how this all works out.
I've already figured out the following code:
http://pastie....
.
This allows me to include the 'MouseEvents' module somewhere and call
'hook'. This works, and prints 'click' to my console, but it blocks
ruby.
Would it be possible to start a seperate Thread from inside the C
extension, which calls into ruby (or executes a block) asynchronously?
- Matt
2 Answers
Jan Dvorak
5/28/2008 1:20:00 PM
0
On Tuesday 27 May 2008 21:44:37 Matthijs Langenberg wrote:
> Just for educational purposes, I'm trying to write a Ruby-C extension
> that allows me to hook into Carbon's event handler. So I can handle
> mouse clicks in Ruby.
> Mouse clicks (like all events) happen asynchronously, meaning that
> something needs to be running inside a seperate thread, but I find it
> hard to grasp how this all works out.
>
> I've already figured out the following code:
http://pastie....
.
> This allows me to include the 'MouseEvents' module somewhere and call
> 'hook'. This works, and prints 'click' to my console, but it blocks
> ruby.
>
> Would it be possible to start a seperate Thread from inside the C
> extension, which calls into ruby (or executes a block) asynchronously?
Not really, the problem is, ruby isn't exactly multi-threading atm (somebody
correct me if i'm wrong), 1.8 doesn't use OS threads at all and concurrency
is done by timeslicing, 1.9 does use native threads but only one thread, but
VM is shared so only one thread can run at a time.
I don't know Carbon but as i see it you have two options - first, if you're
happy with using just the hooker functions, you can simply call the ruby
function from inside your myeventhandler(s), of course you can't run any
other code parallel to event handling (unless you start another ruby
interpreter and synchronize somehow).
Second, you can abandon RunApplicationEventLoop and use manual event polling
(i'm sure Carbon has one), then you can write the event handling as C
function that takes block and iterates it - it would then be used from ruby
as:
...
register_hooks()
...
loop_events() do
# do some stuff
end # here the C function polls next event and if any, calls the appropriate
ruby hooked functions,
Jan
Matthijs Langenberg
5/28/2008 2:37:00 PM
0
On #ruby-lang, drbrain explained to me that I should run my event loop
handler inside a seperate thread.
I understand why that should be, but I've no clue how to launch a
seperate thread from a C extension and communicatie back to the Ruby
thread.
Can anybody give me a head start?
- Matt
On Tue, May 27, 2008 at 9:44 PM, Matthijs Langenberg
<mlangenberg@gmail.com> wrote:
> Just for educational purposes, I'm trying to write a Ruby-C extension
> that allows me to hook into Carbon's event handler. So I can handle
> mouse clicks in Ruby.
> Mouse clicks (like all events) happen asynchronously, meaning that
> something needs to be running inside a seperate thread, but I find it
> hard to grasp how this all works out.
>
> I've already figured out the following code:
http://pastie....
.
> This allows me to include the 'MouseEvents' module somewhere and call
> 'hook'. This works, and prints 'click' to my console, but it blocks
> ruby.
>
> Would it be possible to start a seperate Thread from inside the C
> extension, which calls into ruby (or executes a block) asynchronously?
>
> - Matt
>
>
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Writing event hooks called from C extensions
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password