test/testthread.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:47 -0800
changeset 11731 30f337dc8c74
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Added iOS and OSX versions of the Metal shaders
slouken@8582
     1
/*
slouken@10737
     2
  Copyright (C) 1997-2017 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
slouken@8582
    21
static SDL_TLSID tls;
slouken@8582
    22
static int alive = 0;
slouken@8582
    23
slouken@8582
    24
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@8582
    25
static void
slouken@8582
    26
quit(int rc)
slouken@8582
    27
{
slouken@8582
    28
    SDL_Quit();
slouken@8582
    29
    exit(rc);
slouken@8582
    30
}
slouken@8582
    31
slouken@8582
    32
int SDLCALL
slouken@8582
    33
ThreadFunc(void *data)
slouken@8582
    34
{
slouken@8582
    35
    SDL_TLSSet(tls, "baby thread", NULL);
slouken@8582
    36
    SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n",
slouken@8582
    37
           (char *) data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
slouken@8582
    38
    while (alive) {
slouken@8582
    39
        SDL_Log("Thread '%s' is alive!\n", (char *) data);
slouken@8582
    40
        SDL_Delay(1 * 1000);
slouken@8582
    41
    }
slouken@8582
    42
    SDL_Log("Thread '%s' exiting!\n", (char *) data);
slouken@8582
    43
    return (0);
slouken@8582
    44
}
slouken@8582
    45
slouken@8582
    46
static void
slouken@8582
    47
killed(int sig)
slouken@8582
    48
{
slouken@8582
    49
    SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n");
slouken@8582
    50
    SDL_Delay(5 * 1000);
slouken@8582
    51
    alive = 0;
slouken@8582
    52
    quit(0);
slouken@8582
    53
}
slouken@8582
    54
slouken@8582
    55
int
slouken@8582
    56
main(int argc, char *argv[])
slouken@8582
    57
{
slouken@8582
    58
    SDL_Thread *thread;
slouken@8582
    59
philipp@9922
    60
    /* Enable standard application logging */
slouken@8582
    61
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@8582
    62
slouken@8582
    63
    /* Load the SDL library */
slouken@8582
    64
    if (SDL_Init(0) < 0) {
slouken@8582
    65
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@8582
    66
        return (1);
slouken@8582
    67
    }
slouken@8582
    68
slouken@8582
    69
    tls = SDL_TLSCreate();
slouken@8582
    70
    SDL_assert(tls);
slouken@8582
    71
    SDL_TLSSet(tls, "main thread", NULL);
slouken@8582
    72
    SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
slouken@8582
    73
slouken@8582
    74
    alive = 1;
slouken@8582
    75
    thread = SDL_CreateThread(ThreadFunc, "One", "#1");
slouken@8582
    76
    if (thread == NULL) {
slouken@8582
    77
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
slouken@8582
    78
        quit(1);
slouken@8582
    79
    }
slouken@8582
    80
    SDL_Delay(5 * 1000);
slouken@8582
    81
    SDL_Log("Waiting for thread #1\n");
slouken@8582
    82
    alive = 0;
slouken@8582
    83
    SDL_WaitThread(thread, NULL);
slouken@8582
    84
slouken@8582
    85
    SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
slouken@8582
    86
slouken@8582
    87
    alive = 1;
slouken@8582
    88
    signal(SIGTERM, killed);
slouken@8582
    89
    thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
slouken@8582
    90
    if (thread == NULL) {
slouken@8582
    91
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
slouken@8582
    92
        quit(1);
slouken@8582
    93
    }
slouken@8582
    94
    raise(SIGTERM);
slouken@8582
    95
slouken@8582
    96
    SDL_Quit();                 /* Never reached */
slouken@8582
    97
    return (0);                 /* Never reached */
slouken@8582
    98
}