From: "Mauricio Fernández" Date: 2002-11-13T04:06:22+09:00 Subject: Re: Threading in I/O libraries On Tue, Nov 12, 2002 at 08:11:31PM +0900, Brian Candler wrote: > On Tue, Nov 12, 2002 at 04:21:03AM +0900, Yohanes Santoso wrote: > > > I'd like to ask: how integrated is threading in Ruby's I/O libraries? > > > > > > In particular, > > > > > > (1) if I do a read from a socket, will Ruby call select() first to ensure > > > that there is data available (to prevent blocking), or do I have to do that > > > myself? > > > > Determine existence of data by yourself to prevent blocking on that thread. > > That's not quite what I meant. It doesn't matter if that one thread blocks - > that's actually what I want. > > However, if IO#read simply called the Unix read() function, then the entire > Ruby interpreter would be locked out (including all other threads). > > So what I was asking is, does IO#read actually do something else? For > example a kernel#select followed by a read? Then, does the same apply to all > the I/O functions, like sysread for example? Use the source, Luke! :) io.c ************ static long io_fread(ptr, len, f) char *ptr; long len; FILE *f; { long n = len; int c; while (n > 0) { #ifdef READ_DATA_PENDING_COUNT int i = READ_DATA_PENDING_COUNT(f); if (i <= 0) { rb_thread_wait_fd(fileno(f)); i = READ_DATA_PENDING_COUNT(f); } if (i > 0) { if (i > n) i = n; TRAP_BEG; c = fread(ptr, 1, i, f); TRAP_END; if (c < 0) goto eof; ptr += c; n -= c; if (c < i) goto eof; continue; } #else if (!READ_DATA_PENDING(f)) { rb_thread_wait_fd(fileno(f)); } #endif ************ Seems Ruby does The Right Thing. -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Why use Windows when you can have air conditioning? Why use Windows, when you can leave through the door? -- Konrad Blum