test/testthread.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Jul 2013 09:51:21 -0700
changeset 7517 965d57022c01
parent 7393 358696c354a8
child 7639 9406b7dd2f2d
child 8477 ad08f0d710f3
permissions -rw-r--r--
Updated the copyright year for the test programs
slouken@5535
     1
/*
slouken@7517
     2
  Copyright (C) 1997-2013 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);
slouken@7391
    37
    printf("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) {
slouken@1895
    40
        printf("Thread '%s' is alive!\n", (char *) data);
slouken@1895
    41
        SDL_Delay(1 * 1000);
slouken@1895
    42
    }
slouken@1895
    43
    printf("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
{
slouken@1895
    50
    printf("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
slouken@1895
    61
    /* Load the SDL library */
slouken@1895
    62
    if (SDL_Init(0) < 0) {
slouken@1895
    63
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    64
        return (1);
slouken@1895
    65
    }
slouken@0
    66
slouken@7391
    67
    tls = SDL_TLSCreate();
slouken@7391
    68
    SDL_assert(tls);
slouken@7393
    69
    SDL_TLSSet(tls, "main thread", NULL);
slouken@7391
    70
    printf("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
slouken@7391
    71
slouken@1895
    72
    alive = 1;
icculus@5969
    73
    thread = SDL_CreateThread(ThreadFunc, "One", "#1");
slouken@1895
    74
    if (thread == NULL) {
slouken@1895
    75
        fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
    76
        quit(1);
slouken@1895
    77
    }
slouken@1895
    78
    SDL_Delay(5 * 1000);
slouken@1895
    79
    printf("Waiting for thread #1\n");
slouken@1895
    80
    alive = 0;
slouken@1895
    81
    SDL_WaitThread(thread, NULL);
slouken@0
    82
slouken@7391
    83
    printf("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
slouken@7391
    84
slouken@1895
    85
    alive = 1;
slouken@2779
    86
    signal(SIGTERM, killed);
icculus@5969
    87
    thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
slouken@1895
    88
    if (thread == NULL) {
slouken@1895
    89
        fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
    90
        quit(1);
slouken@1895
    91
    }
slouken@1895
    92
    raise(SIGTERM);
slouken@0
    93
slouken@1895
    94
    SDL_Quit();                 /* Never reached */
slouken@1895
    95
    return (0);                 /* Never reached */
slouken@0
    96
}