Re: GSource and file descriptors



Joel Becker <jlbec evilplan org> writes:

> 	Ok, so I'm trying to poll some file descriptors, and I'm finding
> Gsource to be a bit of a pain.  I'm using HEAD.  The GSourceFuncs have
> removed the source_data parameters etc.  This means that the GPollFD
> structure involved in a call is not passed to the prepare/check/dispatch
> functions.  Here's what I see:
> 
> Option 1:
> 	Make your GPollFDs global.  Have a GSourceFuncs for each FD, and
> set it up.  This includes global data and lots of functions (three per
> fd).
> 
> Option 2:
> 	Make GPollFDs global.  Have one GSourceFuncs that runs through
> each GPollFD checking revents and dispatching callbacks.  This includes
> global data, but a smaller number of functions.
> 
> Option 3:
> 	Access source->pollfds.  Run through each one and check revents,
> dispatching whatever.  This removes the global data need, but returns
> with direct structure access, which is also ugly (outside of glib
> itself).

You'd normally do something like:

====
static GSource *
g_io_unix_create_watch (GIOChannel   *channel,
			GIOCondition  condition)
{
  GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
  GSource *source;
  GIOUnixWatch *watch;


  source = g_source_new (&unix_watch_funcs, sizeof (GIOUnixWatch));
  watch = (GIOUnixWatch *)source;
  
  watch->channel = channel;
  g_io_channel_ref (channel);
  
  watch->condition = condition;

  watch->pollfd.fd = unix_channel->fd;
  watch->pollfd.events = condition;

  g_source_add_poll (source, &watch->pollfd);

  return source;
}
====

> 	In all these cases, the initialization steps required to simply
> poll a file descriptor are large.  I'm amazed there isn't a default
> GSource for descriptors like there exists for timeouts and idles.

It's called g_io_channel_add_watch().

> Would a patch for 2.0 be accepted that implements something like the
> following:
> 
> typedef gboolean (*GPollFunc)(GPollFD *pollfd, gpointer user_data);
> gint g_poll_add(GPollFD *pollfd,
>                 GPollFunc callback,
>                 gpointer user_data);
> 
> with similar semantics to timeouts and idles (ie, return FALSE removes
> the source)?  

I'd be hesitant, certainly. IO channel watches are the normal way to 
do this.

Regards,
                                        Owen




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]