test/testthread.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 09 Mar 2014 11:36:47 -0700
changeset 8583 fb2933ca805f
parent 8149 681eb46b8ac4
parent 8582 c3e9a2b93517
child 9356 e87d6e1e812a
permissions -rw-r--r--
Integrated David Ludwig's support for Windows RT
slouken@8582
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@8582
     3
slouken@8582
     4
  This software is provided 'as-is', without any express or implied
slouken@8582
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@8582
     6
  arising from the use of this software.
slouken@8582
     7
slouken@8582
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@8582
     9
  including commercial applications, and to alter it and redistribute it
slouken@8582
    10
  freely.
slouken@8582
    11
*/
slouken@8582
    12
slouken@8582
    13
/* Simple test of the SDL threading code */
slouken@8582
    14
slouken@8582
    15
#include <stdio.h>
slouken@8582
    16
#include <stdlib.h>
slouken@8582
    17
#include <signal.h>
slouken@8582
    18
slouken@8582
    19
#include "SDL.h"
slouken@8582
    20
#include "SDL_thread.h"
slouken@8582
    21
slouken@8582
    22
static SDL_TLSID tls;
slouken@8582
    23
static int alive = 0;
slouken@8582
    24
slouken@8582
    25
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@8582
    26
static void
slouken@8582
    27
quit(int rc)
slouken@8582
    28
{
slouken@8582
    29
    SDL_Quit();
slouken@8582
    30
    exit(rc);
slouken@8582
    31
}
slouken@8582
    32
slouken@8582
    33
int SDLCALL
slouken@8582
    34
ThreadFunc(void *data)
slouken@8582
    35
{
slouken@8582
    36
    SDL_TLSSet(tls, "baby thread", NULL);
slouken@8582
    37
    SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n",
slouken@8582
    38
           (char *) data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
slouken@8582
    39
    while (alive) {
slouken@8582
    40
        SDL_Log("Thread '%s' is alive!\n", (char *) data);
slouken@8582
    41
        SDL_Delay(1 * 1000);
slouken@8582
    42
    }
slouken@8582
    43
    SDL_Log("Thread '%s' exiting!\n", (char *) data);
slouken@8582
    44
    return (0);
slouken@8582
    45
}
slouken@8582
    46
slouken@8582
    47
static void
slouken@8582
    48
killed(int sig)
slouken@8582
    49
{
slouken@8582
    50
    SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n");
slouken@8582
    51
    SDL_Delay(5 * 1000);
slouken@8582
    52
    alive = 0;
slouken@8582
    53
    quit(0);
slouken@8582
    54
}
slouken@8582
    55
slouken@8582
    56
int
slouken@8582
    57
main(int argc, char *argv[])
slouken@8582
    58
{
slouken@8582
    59
    SDL_Thread *thread;
slouken@8582
    60
slouken@8582
    61
	/* Enable standard application logging */
slouken@8582
    62
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@8582
    63
slouken@8582
    64
    /* Load the SDL library */
slouken@8582
    65
    if (SDL_Init(0) < 0) {
slouken@8582
    66
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@8582
    67
        return (1);
slouken@8582
    68
    }
slouken@8582
    69
slouken@8582
    70
    tls = SDL_TLSCreate();
slouken@8582
    71
    SDL_assert(tls);
slouken@8582
    72
    SDL_TLSSet(tls, "main thread", NULL);
slouken@8582
    73
    SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
slouken@8582
    74
slouken@8582
    75
    alive = 1;
slouken@8582
    76
    thread = SDL_CreateThread(ThreadFunc, "One", "#1");
slouken@8582
    77
    if (thread == NULL) {
slouken@8582
    78
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
slouken@8582
    79
        quit(1);
slouken@8582
    80
    }
slouken@8582
    81
    SDL_Delay(5 * 1000);
slouken@8582
    82
    SDL_Log("Waiting for thread #1\n");
slouken@8582
    83
    alive = 0;
slouken@8582
    84
    SDL_WaitThread(thread, NULL);
slouken@8582
    85
slouken@8582
    86
    SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
slouken@8582
    87
slouken@8582
    88
    alive = 1;
slouken@8582
    89
    signal(SIGTERM, killed);
slouken@8582
    90
    thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
slouken@8582
    91
    if (thread == NULL) {
slouken@8582
    92
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
slouken@8582
    93
        quit(1);
slouken@8582
    94
    }
slouken@8582
    95
    raise(SIGTERM);
slouken@8582
    96
slouken@8582
    97
    SDL_Quit();                 /* Never reached */
slouken@8582
    98
    return (0);                 /* Never reached */
slouken@8582
    99
}