Re: [AD] user-defined events

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Fri, 2008-10-17 at 21:45 +1100, Peter Wang wrote:
> On 2008-10-16, Elias Pschernig <elias.pschernig@xxxxxxxxxx> wrote:
> > On Fri, 2008-10-17 at 00:06 +1100, Peter Wang wrote:
> > > 
> > > When is an event supposed to be destroyed?  al_wait_for_event() returns a
> > > copy of the event, probably on the user's stack.  Allegro doesn't know
> > > when that copy is no longer needed, so the user needs to explicitly call
> > > an unref function.
> > 
> > My idea was, Allegro calls the destroy_event() callback whenever it
> > loses a (not necessarily the last) reference to an event. Because it
> > calls the copy_event() callback whenever a copy is made, it should be
> > 100% correct - users can do their own reference counting inside the
> > copy/destroy callbacks, or make actual copies of the user data and free
> > them. Is it not going to work?
> 
> I can't see how it would work.  Probably I didn't understand your
> description.
> 

Basically, the same your patch does, just that al_unref_user_event would
do nothing but call a user callback (destroy_callback), and whenever the
reference count is increased, another user callback (copy_callback)
would be called. (So users could implement their own reference counting
if they so wish, or alternatively call free(event->data1) in the
destroy_callback and make an actual copy (with malloc) of event->data1
in copy_callback, again if for some reason they would want that
instead.)

> > > Implementing reference counting for user events wouldn't actually be
> > > hard, but I'm still unsure.  If you use atomic instructions then you
> > > would at least avoid having an extra mutex per event.  Maybe do it as an
> > > exercise and see?
> > > 
> > 
> > Yes, likely to call the callbacks at the correct location, it would be
> > as much effort as implementing reference counting - haven't looked at
> > the code yet.
> > 
> > > Or use a conservative garbage collector..
> > > 
> > 
> > I wonder if that would be easy, if I want to only collect my user events
> > - seems overkill in any case.
> 
> Definitely overkill :)
> 
> The following patch implements (optional) reference counting for user
> events.  al_emit_user_event() takes a destructor argument,
> and the user needs to call al_unref_user_event().
> 

That works just as good of course. I don't particulary like the malloc
in al_emit_user_event, but it shouldn't really matter. And I wonder what
to do about __sync_fetch_and_add and __sync_sub_and_fetch for MSVC..

-- 
Elias Pschernig <elias@xxxxxxxxxx>





Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/