Re: (*prepare) and (*check) need user_data

On 23 Mar 1999, Owen Taylor wrote:

> Tim Janik <> writes:
> > hi owen,
> > 
> > i discovered that for most applications of GSource in conjunction
> > with a poll fd, you'd want to add the pollfd right away with the source
> > and remove it again, like:
> > 
> > static void
> > destroy_poll_fd (gpointer data)
> > {
> >   GPollFD *pfd = data;
> > 
> >   g_main_remove_poll (pfd);
> >   g_free (pfd);
> > }
> > 
> > 
> >   pfd = g_new0 (GPollFD, 1);
> >   pfd->fd = some_fd;
> >   pfd->events |= G_IO_IN | G_IO_OUT | G_IO_PRI;
> >   g_main_add_poll (pfd, X_PRIORITY);
> >   g_source_add (X_PRIORITY, TRUE, &x_funcs, x, pfd, destroy_poll_fd);
> > 
> > to properly make use of the pollfd in prepare and check, the user_data pointer
> > needs to be provided though, this can be achived while keeping binary
> > compatibility with 1.2.0.
> OK, I don't understand this at all. The user data  pointer
> is only used to dispatch the call back to the user..
> This should only be done in the dispatch functtion, or
> you're breaking the model...

nope, i'm not breaking the model. i just use sources differently from the
way e.g. timeouts or idles work. that is, i'm not using source_data for a function
pointer and user_data as an argument to dispatch that function, rather i'm passing
an object in as source_data and use user_data for a GPollFd*.

in (*prepare), i'm adjusting the field acccording to the object's

in (*check), i'm evaluating the object state again *and* GPollFD.revents, and from
that, i figure whether dispatching is required.

finally, (*dispatch) amounts to something as simple as object->dispatch().

what the above example meant to show is that user_data is extremely convenient
to hold GPollFDs for fds that have the same life time as their corresponding
GSource (which is pretty common when dealing with GPollFDs), given that you use
sources differently than in the timout/idle way.

but to actually make use of the GPollFD, i.e. to evaluate .revents, you need a
pointer to it, and that's what the user_data in (*check) and (*prepare) is for.

genrally speaking, user_data is just as much an opaque pointer to the GSource
implementation as source_data is, both covering destruction notification.
which one the programmer makes use of in (*check), (*prepare) or (*dispatch),
or whether he actually needs both in some of these functions, isn't up to the
GSource implemntation, and thus it shouldn't restrict use of either pointer in
any of the callbacks.
(this does not have any implications on the prepare-check-dispatch model).

> What gives?
>                                         Owen
> [ Sorry for the short reply - I'm typing over a 
>   trans-pacific link... ]


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