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