src/events/SDL_events.c
changeset 10003 d91a2c45825e
parent 9998 f67cf37e9cd4
child 10060 739bc5c7d339
     1.1 --- a/src/events/SDL_events.c	Sat Jan 02 12:17:33 2016 -0800
     1.2 +++ b/src/events/SDL_events.c	Sun Jan 03 06:50:50 2016 -0500
     1.3 @@ -73,15 +73,15 @@
     1.4  static struct
     1.5  {
     1.6      SDL_mutex *lock;
     1.7 -    volatile SDL_bool active;
     1.8 -    volatile int count;
     1.9 -    volatile int max_events_seen;
    1.10 +    SDL_atomic_t active;
    1.11 +    SDL_atomic_t count;
    1.12 +    int max_events_seen;
    1.13      SDL_EventEntry *head;
    1.14      SDL_EventEntry *tail;
    1.15      SDL_EventEntry *free;
    1.16      SDL_SysWMEntry *wmmsg_used;
    1.17      SDL_SysWMEntry *wmmsg_free;
    1.18 -} SDL_EventQ = { NULL, SDL_TRUE, 0, 0, NULL, NULL, NULL, NULL, NULL };
    1.19 +} SDL_EventQ = { NULL, { 1 }, { 0 }, 0, NULL, NULL, NULL, NULL, NULL };
    1.20  
    1.21  
    1.22  /* Public functions */
    1.23 @@ -98,7 +98,7 @@
    1.24          SDL_LockMutex(SDL_EventQ.lock);
    1.25      }
    1.26  
    1.27 -    SDL_EventQ.active = SDL_FALSE;
    1.28 +    SDL_AtomicSet(&SDL_EventQ.active, 0);
    1.29  
    1.30      if (report && SDL_atoi(report)) {
    1.31          SDL_Log("SDL EVENT QUEUE: Maximum events in-flight: %d\n",
    1.32 @@ -127,7 +127,7 @@
    1.33          wmmsg = next;
    1.34      }
    1.35  
    1.36 -    SDL_EventQ.count = 0;
    1.37 +    SDL_AtomicSet(&SDL_EventQ.count, 0);
    1.38      SDL_EventQ.max_events_seen = 0;
    1.39      SDL_EventQ.head = NULL;
    1.40      SDL_EventQ.tail = NULL;
    1.41 @@ -171,7 +171,7 @@
    1.42          SDL_EventQ.lock = SDL_CreateMutex();
    1.43      }
    1.44      if (SDL_EventQ.lock == NULL) {
    1.45 -        return (-1);
    1.46 +        return -1;
    1.47      }
    1.48  #endif /* !SDL_THREADS_DISABLED */
    1.49  
    1.50 @@ -180,9 +180,9 @@
    1.51      SDL_EventState(SDL_TEXTEDITING, SDL_DISABLE);
    1.52      SDL_EventState(SDL_SYSWMEVENT, SDL_DISABLE);
    1.53  
    1.54 -    SDL_EventQ.active = SDL_TRUE;
    1.55 +    SDL_AtomicSet(&SDL_EventQ.active, 1);
    1.56  
    1.57 -    return (0);
    1.58 +    return 0;
    1.59  }
    1.60  
    1.61  
    1.62 @@ -191,9 +191,11 @@
    1.63  SDL_AddEvent(SDL_Event * event)
    1.64  {
    1.65      SDL_EventEntry *entry;
    1.66 +    const int initial_count = SDL_AtomicGet(&SDL_EventQ.count);
    1.67 +    int final_count;
    1.68  
    1.69 -    if (SDL_EventQ.count >= SDL_MAX_QUEUED_EVENTS) {
    1.70 -        SDL_SetError("Event queue is full (%d events)", SDL_EventQ.count);
    1.71 +    if (initial_count >= SDL_MAX_QUEUED_EVENTS) {
    1.72 +        SDL_SetError("Event queue is full (%d events)", initial_count);
    1.73          return 0;
    1.74      }
    1.75  
    1.76 @@ -225,10 +227,10 @@
    1.77          entry->prev = NULL;
    1.78          entry->next = NULL;
    1.79      }
    1.80 -    ++SDL_EventQ.count;
    1.81  
    1.82 -    if (SDL_EventQ.count > SDL_EventQ.max_events_seen) {
    1.83 -        SDL_EventQ.max_events_seen = SDL_EventQ.count;
    1.84 +    final_count = SDL_AtomicAdd(&SDL_EventQ.count, 1) + 1;
    1.85 +    if (final_count > SDL_EventQ.max_events_seen) {
    1.86 +        SDL_EventQ.max_events_seen = final_count;
    1.87      }
    1.88  
    1.89      return 1;
    1.90 @@ -256,8 +258,8 @@
    1.91  
    1.92      entry->next = SDL_EventQ.free;
    1.93      SDL_EventQ.free = entry;
    1.94 -    SDL_assert(SDL_EventQ.count > 0);
    1.95 -    --SDL_EventQ.count;
    1.96 +    SDL_assert(SDL_AtomicGet(&SDL_EventQ.count) > 0);
    1.97 +    SDL_AtomicAdd(&SDL_EventQ.count, -1);
    1.98  }
    1.99  
   1.100  /* Lock the event queue, take a peep at it, and unlock it */
   1.101 @@ -268,7 +270,7 @@
   1.102      int i, used;
   1.103  
   1.104      /* Don't look after we've quit */
   1.105 -    if (!SDL_EventQ.active) {
   1.106 +    if (!SDL_AtomicGet(&SDL_EventQ.active)) {
   1.107          /* We get a few spurious events at shutdown, so don't warn then */
   1.108          if (action != SDL_ADDEVENT) {
   1.109              SDL_SetError("The event system has been shut down");
   1.110 @@ -363,7 +365,7 @@
   1.111  SDL_FlushEvents(Uint32 minType, Uint32 maxType)
   1.112  {
   1.113      /* Don't look after we've quit */
   1.114 -    if (!SDL_EventQ.active) {
   1.115 +    if (!SDL_AtomicGet(&SDL_EventQ.active)) {
   1.116          return;
   1.117      }
   1.118