Failure at SDL_AddEvent()

hello,
in my attempts to use SDL timers in my code, I have a structure which
contains a SDL_Event variable and a timer callback:

static Uint32 sdl_timer_callback(ep_session *session) {
SDL_PushEvent(&session->event);
return (55);
}

which uses SDL_PushEvent() to push an event into that structure’s event.
The only thing in the main thread is SDL_WaitEvent(), and for some
reason, when SDL_PushEvent() is run, the code seg faults, and a
backtrace shows it’s in SDL_AddEvent(). Am I missing something here? I
thought it was always safe to call SDL_PushEvent() from a SDL timer
callback.–
Chris Thielen <@Christopher_Thielen>

Should work as long as you never ever modify session->event from the main
thread while timers are on. Maybe try something like to make sure you
aren’t tripping yourself up on the threading:

static Uint32 sdl_timer_callback(ep_session *session) {
SDL_Event event;

SDL_PushEvent(&event);
return (55);
}

Cheers,
Dan.> ----- Original Message -----

From: chris@luethy.net (Christopher Thielen)
To:
Sent: Saturday, November 09, 2002 4:32 PM
Subject: [SDL] failure at SDL_AddEvent()

hello,
in my attempts to use SDL timers in my code, I have a structure which
contains a SDL_Event variable and a timer callback:

static Uint32 sdl_timer_callback(ep_session *session) {
SDL_PushEvent(&session->event);
return (55);
}

which uses SDL_PushEvent() to push an event into that structure’s event.
The only thing in the main thread is SDL_WaitEvent(), and for some
reason, when SDL_PushEvent() is run, the code seg faults, and a
backtrace shows it’s in SDL_AddEvent(). Am I missing something here? I
thought it was always safe to call SDL_PushEvent() from a SDL timer
callback.


Chris Thielen


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

static Uint32 sdl_timer_callback(ep_session *session) {
SDL_Event event;

SDL_PushEvent(&event);
return (55);
}

Unless I’m missing something, the address of “event” will be invalid
(i.e. will not point to an SDL_Event) at the point of
sdl_timer_callback’s return (since event is on the stack). This would
be a big problem unless SDL_PushEvent causes a context switch to the
waiting thread (meaning that “event” is consumed before it is no longer
on the stack).

Even in that case, it would appear to be MP-unsafe. Is this correct?

best,
wbOn Saturday, November 9, 2002, at 05:06 PM, Daniel Goertzen wrote:

Hmmm, you had to make me look at the sdl source to confirm this one. :slight_smile:

From SDL_Events.c:

/* Private data – event queue */
#define MAXEVENTS 128
static struct {
SDL_mutex *lock;
int active;
int head;
int tail;
SDL_Event event[MAXEVENTS]; ***********
int wmmsg_next;
struct SDL_SysWMmsg wmmsg[MAXEVENTS];
} SDL_EventQ;

The event queue stores the whole event itself, not pointers to events. The
callback below is valid because SDL_PushEvent will copy the event into the
queue before it goes out of scope. The documentation should be more
explicit about this.

Cheers,
Dan.> ----- Original Message -----

From: willb@cs.wisc.edu (Will Benton)
To:
Sent: Saturday, November 09, 2002 6:00 PM
Subject: Re: [SDL] failure at SDL_AddEvent()

On Saturday, November 9, 2002, at 05:06 PM, Daniel Goertzen wrote:

static Uint32 sdl_timer_callback(ep_session *session) {
SDL_Event event;

SDL_PushEvent(&event);
return (55);
}

Unless I’m missing something, the address of “event” will be invalid
(i.e. will not point to an SDL_Event) at the point of
sdl_timer_callback’s return (since event is on the stack). This would
be a big problem unless SDL_PushEvent causes a context switch to the
waiting thread (meaning that “event” is consumed before it is no longer
on the stack).

Even in that case, it would appear to be MP-unsafe. Is this correct?

best,
wb


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

Great, this works perfectly. Thanks guys.On Sun, 2002-11-10 at 08:37, Daniel Goertzen wrote:

Hmmm, you had to make me look at the sdl source to confirm this one. :slight_smile:

From SDL_Events.c:

/* Private data – event queue */
#define MAXEVENTS 128
static struct {
SDL_mutex *lock;
int active;
int head;
int tail;
SDL_Event event[MAXEVENTS]; ***********
int wmmsg_next;
struct SDL_SysWMmsg wmmsg[MAXEVENTS];
} SDL_EventQ;

The event queue stores the whole event itself, not pointers to events. The
callback below is valid because SDL_PushEvent will copy the event into the
queue before it goes out of scope. The documentation should be more
explicit about this.

Cheers,
Dan.

----- Original Message -----
From: “Will Benton”
To:
Sent: Saturday, November 09, 2002 6:00 PM
Subject: Re: [SDL] failure at SDL_AddEvent()

On Saturday, November 9, 2002, at 05:06 PM, Daniel Goertzen wrote:

static Uint32 sdl_timer_callback(ep_session *session) {
SDL_Event event;

SDL_PushEvent(&event);
return (55);
}

Unless I’m missing something, the address of “event” will be invalid
(i.e. will not point to an SDL_Event) at the point of
sdl_timer_callback’s return (since event is on the stack). This would
be a big problem unless SDL_PushEvent causes a context switch to the
waiting thread (meaning that “event” is consumed before it is no longer
on the stack).

Even in that case, it would appear to be MP-unsafe. Is this correct?

best,
wb


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


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

Chris Thielen <@Christopher_Thielen>