Home | History | Annotate | Download | only in test
      1 /*
      2   Copyright (C) 1997-2014 Sam Lantinga <slouken (at) libsdl.org>
      3 
      4   This software is provided 'as-is', without any express or implied
      5   warranty.  In no event will the authors be held liable for any damages
      6   arising from the use of this software.
      7 
      8   Permission is granted to anyone to use this software for any purpose,
      9   including commercial applications, and to alter it and redistribute it
     10   freely.
     11 */
     12 
     13 /* Simple test of the SDL threading code and error handling */
     14 
     15 #include <stdio.h>
     16 #include <stdlib.h>
     17 #include <signal.h>
     18 
     19 #include "SDL.h"
     20 #include "SDL_thread.h"
     21 
     22 static int alive = 0;
     23 
     24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     25 static void
     26 quit(int rc)
     27 {
     28     SDL_Quit();
     29     exit(rc);
     30 }
     31 
     32 int SDLCALL
     33 ThreadFunc(void *data)
     34 {
     35     /* Set the child thread error string */
     36     SDL_SetError("Thread %s (%lu) had a problem: %s",
     37                  (char *) data, SDL_ThreadID(), "nevermind");
     38     while (alive) {
     39         SDL_Log("Thread '%s' is alive!\n", (char *) data);
     40         SDL_Delay(1 * 1000);
     41     }
     42     SDL_Log("Child thread error string: %s\n", SDL_GetError());
     43     return (0);
     44 }
     45 
     46 int
     47 main(int argc, char *argv[])
     48 {
     49     SDL_Thread *thread;
     50 
     51     /* Enable standard application logging */
     52     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
     53 
     54     /* Load the SDL library */
     55     if (SDL_Init(0) < 0) {
     56         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
     57         return (1);
     58     }
     59 
     60     /* Set the error value for the main thread */
     61     SDL_SetError("No worries");
     62 
     63     alive = 1;
     64     thread = SDL_CreateThread(ThreadFunc, NULL, "#1");
     65     if (thread == NULL) {
     66         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
     67         quit(1);
     68     }
     69     SDL_Delay(5 * 1000);
     70     SDL_Log("Waiting for thread #1\n");
     71     alive = 0;
     72     SDL_WaitThread(thread, NULL);
     73 
     74     SDL_Log("Main thread error string: %s\n", SDL_GetError());
     75 
     76     SDL_Quit();
     77     return (0);
     78 }
     79