How to write a blocking ruby function in c?

Hello,

i must communicate with a c++ subsystem on windows.

The subystem has a socket like interface and i want to write some
kind of thread blocking: read/write calls.

But of course i know that behind the scenes the thread can’t block,
because we have cooperative threading with setjmp/longjmp.

So i think i should write the following function:

VALUE read_data (VALUE self, VALUE from)
{
if ( …from has no data…) rb_thread_stop();
data = … read data from subsystem
return data;
}

but how can i do the thread wakeup call ?

The problem is that for performance reason i can’t poll and i can’t
setup a timer to check frequently from the ruby operating system thread.
I also can’t use a signal, so is there any notification mechanism
that works on windows ?

···


Best regards,
Lothar mailto:mailinglists@scriptolutions.com

I think what you want is something like this:

  1. See if you have data waiting; if you do read and return.
  2. If there is no data waiting, then call rb_thread_select with your
    file descriptor to wait on data.
  3. When rb_thread_select returns, read and return the data. If not
    enough data is available, call rb_thread_select again and repeat.

Paul

···

On Fri, Mar 12, 2004 at 03:44:34PM +0900, Lothar Scholz wrote:

So i think i should write the following function:

VALUE read_data (VALUE self, VALUE from)
{
if ( …from has no data…) rb_thread_stop();
data = … read data from subsystem
return data;
}

but how can i do the thread wakeup call ?

Hello Paul,

Friday, March 12, 2004, 9:17:19 PM, you wrote:

I think what you want is something like this:

  1. See if you have data waiting; if you do read and return.

Right.

  2. If there is no data waiting, then call rb_thread_select with your
     file descriptor to wait on data.

The problem is that the data is comming from another thread, and i
don't have a file descriptor. But it seems that there is really only
the chance of using a file descriptor, maybe a pipe on Linux. But
"select" does not work on pipes or file descriptors. So what can i do
here ? I'm very sure that writing a socket server and a client just
for wake up purpose is an overkill.

In the end someone must rewrite the braindead event loop model. I just
found that TCL is now offering asynchronous write/read operation which
gives a huge performance increase. It's always disappointing that the
implementation of Ruby looks like it is the worst of the currently
available main stream script language.

···

--
Best regards,
Lothar mailto:mailinglists@scriptolutions.com