comparison src/events/SDL_events.c @ 5113:481dabb098ef

Improved timer implementation The new timer model is formalized as using a separate thread to handle timer callbacks. This was the case on almost every platform before, but it's now a requirement, and simplifies the implementation and makes it perform consistently across platforms. Goals: * Minimize timer thread blocking * Dispatch timers as accurately as possible * SDL_AddTimer() and SDL_RemoveTimer() are completely threadsafe * SDL_RemoveTimer() doesn't crash with a timer that's expired or removed
author Sam Lantinga <slouken@libsdl.org>
date Thu, 27 Jan 2011 14:45:06 -0800
parents c2539ff054c8
children dc0dfdd58f27
comparison
equal deleted inserted replaced
5112:0846f18eb625 5113:481dabb098ef
121 } 121 }
122 #endif 122 #endif
123 123
124 /* Give up the CPU for the rest of our timeslice */ 124 /* Give up the CPU for the rest of our timeslice */
125 SDL_EventLock.safe = 1; 125 SDL_EventLock.safe = 1;
126 if (SDL_timer_running) {
127 SDL_ThreadedTimerCheck();
128 }
129 SDL_Delay(1); 126 SDL_Delay(1);
130 127
131 /* Check for event locking. 128 /* Check for event locking.
132 On the P of the lock mutex, if the lock is held, this thread 129 On the P of the lock mutex, if the lock is held, this thread
133 will wait until the lock is released before continuing. The 130 will wait until the lock is released before continuing. The
138 */ 135 */
139 SDL_mutexP(SDL_EventLock.lock); 136 SDL_mutexP(SDL_EventLock.lock);
140 SDL_EventLock.safe = 0; 137 SDL_EventLock.safe = 0;
141 SDL_mutexV(SDL_EventLock.lock); 138 SDL_mutexV(SDL_EventLock.lock);
142 } 139 }
143 SDL_SetTimerThreaded(0);
144 event_thread = 0; 140 event_thread = 0;
145 return (0); 141 return (0);
146 } 142 }
147 143
148 static int 144 static int
166 if (SDL_EventLock.lock == NULL) { 162 if (SDL_EventLock.lock == NULL) {
167 return (-1); 163 return (-1);
168 } 164 }
169 SDL_EventLock.safe = 0; 165 SDL_EventLock.safe = 0;
170 166
171 /* The event thread will handle timers too */
172 SDL_SetTimerThreaded(2);
173 #if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC) 167 #if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC)
174 #undef SDL_CreateThread 168 #undef SDL_CreateThread
175 SDL_EventThread = 169 SDL_EventThread =
176 SDL_CreateThread(SDL_GobbleEvents, NULL, NULL, NULL); 170 SDL_CreateThread(SDL_GobbleEvents, NULL, NULL, NULL);
177 #else 171 #else