test/testthread.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 7639 9406b7dd2f2d
child 8583 fb2933ca805f
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

Is it that time already??
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 */
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@7391
    22
static SDL_TLSID tls;
slouken@0
    23
static int alive = 0;
slouken@0
    24
icculus@1151
    25
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    26
static void
slouken@1895
    27
quit(int rc)
icculus@1151
    28
{
slouken@1895
    29
    SDL_Quit();
slouken@1895
    30
    exit(rc);
icculus@1151
    31
}
icculus@1151
    32
slouken@1895
    33
int SDLCALL
slouken@1895
    34
ThreadFunc(void *data)
slouken@0
    35
{
slouken@7393
    36
    SDL_TLSSet(tls, "baby thread", NULL);
aschiffler@7639
    37
    SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n",
slouken@7391
    38
           (char *) data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
slouken@1895
    39
    while (alive) {
aschiffler@7639
    40
        SDL_Log("Thread '%s' is alive!\n", (char *) data);
slouken@1895
    41
        SDL_Delay(1 * 1000);
slouken@1895
    42
    }
aschiffler@7639
    43
    SDL_Log("Thread '%s' exiting!\n", (char *) data);
slouken@1895
    44
    return (0);
slouken@0
    45
}
slouken@0
    46
slouken@1895
    47
static void
slouken@1895
    48
killed(int sig)
slouken@0
    49
{
aschiffler@7639
    50
    SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n");
slouken@1895
    51
    SDL_Delay(5 * 1000);
slouken@1895
    52
    alive = 0;
slouken@1895
    53
    quit(0);
slouken@0
    54
}
slouken@0
    55
slouken@1895
    56
int
slouken@1895
    57
main(int argc, char *argv[])
slouken@0
    58
{
slouken@1895
    59
    SDL_Thread *thread;
slouken@0
    60
aschiffler@7639
    61
	/* Enable standard application logging */
aschiffler@7639
    62
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    63
slouken@1895
    64
    /* Load the SDL library */
slouken@1895
    65
    if (SDL_Init(0) < 0) {
aschiffler@7639
    66
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    67
        return (1);
slouken@1895
    68
    }
slouken@0
    69
slouken@7391
    70
    tls = SDL_TLSCreate();
slouken@7391
    71
    SDL_assert(tls);
slouken@7393
    72
    SDL_TLSSet(tls, "main thread", NULL);
aschiffler@7639
    73
    SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
slouken@7391
    74
slouken@1895
    75
    alive = 1;
icculus@5969
    76
    thread = SDL_CreateThread(ThreadFunc, "One", "#1");
slouken@1895
    77
    if (thread == NULL) {
aschiffler@7639
    78
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
    79
        quit(1);
slouken@1895
    80
    }
slouken@1895
    81
    SDL_Delay(5 * 1000);
aschiffler@7639
    82
    SDL_Log("Waiting for thread #1\n");
slouken@1895
    83
    alive = 0;
slouken@1895
    84
    SDL_WaitThread(thread, NULL);
slouken@0
    85
aschiffler@7639
    86
    SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
slouken@7391
    87
slouken@1895
    88
    alive = 1;
slouken@2779
    89
    signal(SIGTERM, killed);
icculus@5969
    90
    thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
slouken@1895
    91
    if (thread == NULL) {
aschiffler@7639
    92
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
    93
        quit(1);
slouken@1895
    94
    }
slouken@1895
    95
    raise(SIGTERM);
slouken@0
    96
slouken@1895
    97
    SDL_Quit();                 /* Never reached */
slouken@1895
    98
    return (0);                 /* Never reached */
slouken@0
    99
}