test/testerror.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 22 Jul 2014 21:41:49 -0400
changeset 9012 aa058c87737b
parent 8149 681eb46b8ac4
child 9356 e87d6e1e812a
permissions -rw-r--r--
Added audio device buffer queueing API.
slouken@5535
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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
#include "SDL_thread.h"
slouken@0
    21
slouken@0
    22
static int alive = 0;
slouken@0
    23
icculus@1151
    24
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    25
static void
slouken@1895
    26
quit(int rc)
icculus@1151
    27
{
slouken@1895
    28
    SDL_Quit();
slouken@1895
    29
    exit(rc);
icculus@1151
    30
}
icculus@1151
    31
slouken@1895
    32
int SDLCALL
slouken@1895
    33
ThreadFunc(void *data)
slouken@0
    34
{
slouken@1895
    35
    /* Set the child thread error string */
slouken@3578
    36
    SDL_SetError("Thread %s (%lu) had a problem: %s",
slouken@1895
    37
                 (char *) data, SDL_ThreadID(), "nevermind");
slouken@1895
    38
    while (alive) {
aschiffler@7639
    39
        SDL_Log("Thread '%s' is alive!\n", (char *) data);
slouken@1895
    40
        SDL_Delay(1 * 1000);
slouken@1895
    41
    }
aschiffler@7639
    42
    SDL_Log("Child thread error string: %s\n", SDL_GetError());
slouken@1895
    43
    return (0);
slouken@0
    44
}
slouken@0
    45
slouken@1895
    46
int
slouken@1895
    47
main(int argc, char *argv[])
slouken@0
    48
{
slouken@1895
    49
    SDL_Thread *thread;
slouken@0
    50
aschiffler@7639
    51
    /* Enable standard application logging */
aschiffler@7639
    52
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    53
slouken@1895
    54
    /* Load the SDL library */
slouken@1895
    55
    if (SDL_Init(0) < 0) {
aschiffler@7639
    56
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    57
        return (1);
slouken@1895
    58
    }
slouken@0
    59
slouken@1895
    60
    /* Set the error value for the main thread */
slouken@1895
    61
    SDL_SetError("No worries");
slouken@0
    62
slouken@1895
    63
    alive = 1;
icculus@5969
    64
    thread = SDL_CreateThread(ThreadFunc, NULL, "#1");
slouken@1895
    65
    if (thread == NULL) {
aschiffler@7639
    66
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
    67
        quit(1);
slouken@1895
    68
    }
slouken@1895
    69
    SDL_Delay(5 * 1000);
aschiffler@7639
    70
    SDL_Log("Waiting for thread #1\n");
slouken@1895
    71
    alive = 0;
slouken@1895
    72
    SDL_WaitThread(thread, NULL);
slouken@0
    73
aschiffler@7639
    74
    SDL_Log("Main thread error string: %s\n", SDL_GetError());
slouken@0
    75
slouken@1895
    76
    SDL_Quit();
slouken@1895
    77
    return (0);
slouken@0
    78
}