src/SDL_assert.c
changeset 11015 b00c4088f687
parent 10737 3406a0f8b041
child 11017 411bf9f7f908
equal deleted inserted replaced
11014:4210074a33c8 11015:b00c4088f687
    51  * We keep all triggered assertions in a singly-linked list so we can
    51  * We keep all triggered assertions in a singly-linked list so we can
    52  *  generate a report later.
    52  *  generate a report later.
    53  */
    53  */
    54 static SDL_assert_data *triggered_assertions = NULL;
    54 static SDL_assert_data *triggered_assertions = NULL;
    55 
    55 
       
    56 #ifndef SDL_THREADS_DISABLED
    56 static SDL_mutex *assertion_mutex = NULL;
    57 static SDL_mutex *assertion_mutex = NULL;
       
    58 #endif
       
    59 
    57 static SDL_AssertionHandler assertion_handler = SDL_PromptAssertion;
    60 static SDL_AssertionHandler assertion_handler = SDL_PromptAssertion;
    58 static void *assertion_userdata = NULL;
    61 static void *assertion_userdata = NULL;
    59 
    62 
    60 #ifdef __GNUC__
    63 #ifdef __GNUC__
    61 static void
    64 static void
   263 
   266 
   264 SDL_assert_state
   267 SDL_assert_state
   265 SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
   268 SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
   266                     int line)
   269                     int line)
   267 {
   270 {
       
   271     SDL_assert_state state = SDL_ASSERTION_IGNORE;
   268     static int assertion_running = 0;
   272     static int assertion_running = 0;
       
   273 
       
   274 #ifndef SDL_THREADS_DISABLED
   269     static SDL_SpinLock spinlock = 0;
   275     static SDL_SpinLock spinlock = 0;
   270     SDL_assert_state state = SDL_ASSERTION_IGNORE;
       
   271 
       
   272     SDL_AtomicLock(&spinlock);
   276     SDL_AtomicLock(&spinlock);
   273     if (assertion_mutex == NULL) { /* never called SDL_Init()? */
   277     if (assertion_mutex == NULL) { /* never called SDL_Init()? */
   274         assertion_mutex = SDL_CreateMutex();
   278         assertion_mutex = SDL_CreateMutex();
   275         if (assertion_mutex == NULL) {
   279         if (assertion_mutex == NULL) {
   276             SDL_AtomicUnlock(&spinlock);
   280             SDL_AtomicUnlock(&spinlock);
   280     SDL_AtomicUnlock(&spinlock);
   284     SDL_AtomicUnlock(&spinlock);
   281 
   285 
   282     if (SDL_LockMutex(assertion_mutex) < 0) {
   286     if (SDL_LockMutex(assertion_mutex) < 0) {
   283         return SDL_ASSERTION_IGNORE;   /* oh well, I guess. */
   287         return SDL_ASSERTION_IGNORE;   /* oh well, I guess. */
   284     }
   288     }
       
   289 #endif
   285 
   290 
   286     /* doing this because Visual C is upset over assigning in the macro. */
   291     /* doing this because Visual C is upset over assigning in the macro. */
   287     if (data->trigger_count == 0) {
   292     if (data->trigger_count == 0) {
   288         data->function = func;
   293         data->function = func;
   289         data->filename = file;
   294         data->filename = file;
   323         case SDL_ASSERTION_BREAK:
   328         case SDL_ASSERTION_BREAK:
   324             break;  /* macro handles these. */
   329             break;  /* macro handles these. */
   325     }
   330     }
   326 
   331 
   327     assertion_running--;
   332     assertion_running--;
       
   333 
       
   334 #ifndef SDL_THREADS_DISABLED
   328     SDL_UnlockMutex(assertion_mutex);
   335     SDL_UnlockMutex(assertion_mutex);
       
   336 #endif
   329 
   337 
   330     return state;
   338     return state;
   331 }
   339 }
   332 
   340 
   333 
   341 
   334 void SDL_AssertionsQuit(void)
   342 void SDL_AssertionsQuit(void)
   335 {
   343 {
   336     SDL_GenerateAssertionReport();
   344     SDL_GenerateAssertionReport();
       
   345 #ifndef SDL_THREADS_DISABLED
   337     if (assertion_mutex != NULL) {
   346     if (assertion_mutex != NULL) {
   338         SDL_DestroyMutex(assertion_mutex);
   347         SDL_DestroyMutex(assertion_mutex);
   339         assertion_mutex = NULL;
   348         assertion_mutex = NULL;
   340     }
   349     }
       
   350 #endif
   341 }
   351 }
   342 
   352 
   343 void SDL_SetAssertionHandler(SDL_AssertionHandler handler, void *userdata)
   353 void SDL_SetAssertionHandler(SDL_AssertionHandler handler, void *userdata)
   344 {
   354 {
   345     if (handler != NULL) {
   355     if (handler != NULL) {