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