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