Using SDL_PushEvent

I want to push an SDL_Event of type SDL_USEREVENT onto the event queue using
SDL_PushEvent. My question is, is the entire SDL_UserEvent struct copied into
the queue, or just a pointer to the event? If the function that created the
SDL_UserEvent returns before the event handler (in another thread) polls it,
does a valid copy of that event remain in the queue? Or just a pointer to now-
invalid memory?

Thanks a bunch!

Anthony Kolb

I want to push an SDL_Event of type SDL_USEREVENT onto the event queue
using SDL_PushEvent. My question is, is the entire SDL_UserEvent struct
copied into the queue, or just a pointer to the event? If the function
that created the SDL_UserEvent returns before the event handler (in
another thread) polls it, does a valid copy of that event remain in the
queue? Or just a pointer to now- invalid memory?

AFAIK event struct is copied into queue, so it’s safe to
delete/free/whatever it after call to SDL_PushEvent.
Just remember that user event struct’s data fields are pointers, so only
pointers are copied to queue, not data they point to (so if You set them
to point to some data, and remove event, You should keep that data, until
You resolve event from queue, unless You use some other tricks :).

Regards
ahwayakchih

I want to push an SDL_Event of type SDL_USEREVENT onto the event queue
using SDL_PushEvent. My question is, is the entire SDL_UserEvent
struct
copied into the queue, or just a pointer to the event? If the
function
that created the SDL_UserEvent returns before the event handler (in
another thread) polls it, does a valid copy of that event remain in
the
queue? Or just a pointer to now- invalid memory?

AFAIK event struct is copied into queue, so it’s safe to
delete/free/whatever it after call to SDL_PushEvent.
Just remember that user event struct’s data fields are pointers, so
only
pointers are copied to queue, not data they point to (so if You set
them
to point to some data, and remove event, You should keep that data,
until
You resolve event from queue, unless You use some other tricks :).

Marcin, don’t touch the event stack from another thread, it isn’t safe.
http://sdldoc.csn.ul.ie/thread.phpOn Mar 16, 2005, at 3:38 AM, Marcin Konicki wrote:

Quoth Donny Viszneki , on 2005-03-16 20:21:43 -0500:

Marcin, don’t touch the event stack from another thread, it isn’t safe.
http://sdldoc.csn.ul.ie/thread.php

I seem to recall a reference specifically indicating that
SDL_PushEvent could be used to push events from other threads to the
main thread, but I can’t seem to find it in the docs, modulo a
reference to being able to use SDL_PushEvent from timer functions.

I see that SDL_PeepEvents can be used to add events to the main event
queue and is explicitly marked in the man page as being thread-safe.
Looking at the SDL 1.2.7 source, I see:

int SDL_PushEvent(SDL_Event *event)
{
if ( SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0 )
return -1;
return 0;
}

which means that SDL_PushEvent just wraps SDL_PeepEvents, so it should
be safe to use it from any thread. Is there any authoritative
documentation indicating that that’s guaranteed to be the case,
though?

—> Drake Wilson
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20050316/93f73236/attachment.pgp

Marcin, don’t touch the event stack from another thread, it isn’t safe.
http://sdldoc.csn.ul.ie/thread.php

SDL_PeepEvents is marked in SDL_events.h as thread-safe, and SDL_PushEvent
is just a wrapper for SDL_PeepEvents.
SDL_PeepEvents locks queue before doing anything with it, and other,
internal, functions like SDL_AddEvent are marked to be called with queue
locked. So it seems that queue is always locked before any changes.

So on platforms where locking (SDL_mutex) is implemented it should be safe
to use SDL_PushEvent from other threads :slight_smile:

Regards
ahwayakchih

Donny Viszneki <smirk thebuicksix.com> writes:

AFAIK event struct is copied into queue, so it’s safe to
delete/free/whatever it after call to SDL_PushEvent.
Just remember that user event struct’s data fields are pointers, so
only
pointers are copied to queue, not data they point to (so if You set
them
to point to some data, and remove event, You should keep that data,
until
You resolve event from queue, unless You use some other tricks :).

Marcin, don’t touch the event stack from another thread, it isn’t safe.
http://sdldoc.csn.ul.ie/thread.php

Marcin and Donny,
Thanks for your replies. Marcin, thanks for pointing out that even though
the event struct is copied, the data fields are not–I hadn’t thought of that.
Donny, I hadn’t seen that site before, but the comments at the bottom make
it a lot more useful that then documentation that comes with SDL. This page
from that site tells that SDL_PushEvent() is thread-safe:
http://sdldoc.csn.ul.ie/sdlpushevent.php

Regards,
Anthony> On Mar 16, 2005, at 3:38 AM, Marcin Konicki wrote:

I seem to recall a reference specifically indicating that SDL_PushEvent
could be used to push events from other threads to the main thread, but
I can’t seem to find it in the docs, modulo a reference to being able to
use SDL_PushEvent from timer functions.

I see that SDL_PeepEvents can be used to add events to the main event
queue and is explicitly marked in the man page as being thread-safe.
Looking at the SDL 1.2.7 source, I see:

int SDL_PushEvent(SDL_Event *event)
{
if ( SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0 )
return -1;
return 0;
}

which means that SDL_PushEvent just wraps SDL_PeepEvents, so it should
be safe to use it from any thread. Is there any authoritative
documentation indicating that that’s guaranteed to be the case, though?

Heh, i should have read all mails before replying :).

AFAIK there’s no official docs, but after looking into the source code, it
seems SDL_PushEvent and SDL_PeepEvents are safe to be called from any
thread - event queue is locked before any changes.

Regards
ahwayakchih

Please let old docs rot in peace =)

http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fPushEventOn Wed, Mar 16, 2005 at 08:21:43PM -0500, Donny Viszneki wrote:

Marcin, don’t touch the event stack from another thread, it isn’t safe.
http://sdldoc.csn.ul.ie/thread.php


Petri Latvala
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20050317/b63b8ae6/attachment.pgp

I want to push an SDL_Event of type SDL_USEREVENT onto the event queue
using SDL_PushEvent. My question is, is the entire SDL_UserEvent
struct
copied into the queue, or just a pointer to the event? If the
function
that created the SDL_UserEvent returns before the event handler (in
another thread) polls it, does a valid copy of that event remain in
the
queue? Or just a pointer to now- invalid memory?

AFAIK event struct is copied into queue, so it’s safe to
delete/free/whatever it after call to SDL_PushEvent.
Just remember that user event struct’s data fields are pointers, so
only
pointers are copied to queue, not data they point to (so if You set
them
to point to some data, and remove event, You should keep that data,
until
You resolve event from queue, unless You use some other tricks :).

Marcin, don’t touch the event stack from another thread, it isn’t safe.
http://sdldoc.csn.ul.ie/thread.php

Yes, you can call SDL_PushEvent() from another thread. It IS thread
safe as it plainly stated in the documentation
http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fPushEvent.

I do it all the time.

		Bob PendletonOn Wed, 2005-03-16 at 20:21 -0500, Donny Viszneki wrote:

On Mar 16, 2005, at 3:38 AM, Marcin Konicki wrote:


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I seem to recall a reference specifically indicating that SDL_PushEvent
could be used to push events from other threads to the main thread, but
I can’t seem to find it in the docs, modulo a reference to being able to
use SDL_PushEvent from timer functions.

I see that SDL_PeepEvents can be used to add events to the main event
queue and is explicitly marked in the man page as being thread-safe.
Looking at the SDL 1.2.7 source, I see:

int SDL_PushEvent(SDL_Event *event)
{
if ( SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0) <= 0 )
return -1;
return 0;
}

which means that SDL_PushEvent just wraps SDL_PeepEvents, so it should
be safe to use it from any thread. Is there any authoritative
documentation indicating that that’s guaranteed to be the case, though?

Heh, i should have read all mails before replying :).

AFAIK there’s no official docs,

There are official docs and they reside at:
http://www.libsdl.org/cgi/docwiki.cgi/ and are linked off of the SDL
webpage.

	Bob PendletonOn Thu, 2005-03-17 at 06:19 +0100, Marcin Konicki wrote:

but after looking into the source code, it
seems SDL_PushEvent and SDL_PeepEvents are safe to be called from any
thread - event queue is locked before any changes.

Regards
ahwayakchih


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Heh, I was just looking at that an hour or so ago, and am using it
from inside a timer callback, myself… (every time the timer ticks,
I get an SDL_USEREVENT event)

-bill!On Thu, Mar 17, 2005 at 12:36:20PM -0600, Bob Pendleton wrote:

Yes, you can call SDL_PushEvent() from another thread. It IS thread
safe as it plainly stated in the documentation
http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fPushEvent.

I do it all the time.

Yes, you can call SDL_PushEvent() from another thread. It IS thread
safe as it plainly stated in the documentation
http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fPushEvent.

I do it all the time.

Thanks to everyone who pointed out my mistake. Who is responsible for
that old documentation anyway?On Mar 17, 2005, at 1:36 PM, Bob Pendleton wrote:

On Mar 17, 2005, at 2:32 PM, Bill Kendrick wrote:

Heh, I was just looking at that an hour or so ago, and am using it
from inside a timer callback, myself… (every time the timer ticks,
I get an SDL_USEREVENT event)

Doesn’t SDL have a timer API?

Quoth Donny Viszneki , on 2005-03-17 22:51:56 -0500:

Heh, I was just looking at that an hour or so ago, and am using it
from inside a timer callback, myself… (every time the timer ticks,
I get an SDL_USEREVENT event)

Doesn’t SDL have a timer API?

I think that was what ey was using. From SDL_AddTimer(3):

 The  timer  callback  function  may run in a different thread than your
 main program, and so shouldn't call any functions from  within  itself.
 You may always call SDL_PushEvent, however.

—> Drake Wilson
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20050317/648057dc/attachment.pgp

Precisely :wink:

-bill!On Thu, Mar 17, 2005 at 11:38:33PM -0500, Drake Wilson wrote:

 The  timer  callback  function  may run in a different thread than your
 main program, and so shouldn't call any functions from  within  itself.
 You may always call SDL_PushEvent, however.