Writing event hooks called from C extensions

  • Thread starter Matthijs Langenberg
  • Start date
M

Matthijs Langenberg

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.org/204180.
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
 
J

Jan Dvorak

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.org/204180.
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
 
M

Matthijs Langenberg

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top