test/testthread.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 01 Nov 2018 12:31:45 -0400
changeset 12382 03d0bddca61b
parent 11952 d82e6e63c632
child 12503 806492103856
permissions -rw-r--r--
merge fallout: Patched to compile, fixed some compiler warnings, etc.
slouken@8582
     1
/*
slouken@11811
     2
  Copyright (C) 1997-2018 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@11946
    23
static int testprio = 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@11946
    33
static const char *
slouken@11946
    34
getprioritystr(SDL_ThreadPriority priority)
slouken@11946
    35
{
slouken@11946
    36
    switch(priority)
slouken@11946
    37
    {
slouken@11946
    38
    case SDL_THREAD_PRIORITY_LOW: return "SDL_THREAD_PRIORITY_LOW";
slouken@11946
    39
    case SDL_THREAD_PRIORITY_NORMAL: return "SDL_THREAD_PRIORITY_NORMAL";
slouken@11946
    40
    case SDL_THREAD_PRIORITY_HIGH: return "SDL_THREAD_PRIORITY_HIGH";
slouken@11952
    41
    case SDL_THREAD_PRIORITY_TIME_CRITICAL: return "SDL_THREAD_PRIORITY_TIME_CRITICAL";
slouken@11946
    42
    }
slouken@11946
    43
slouken@11946
    44
    return "???";
slouken@11946
    45
}
slouken@11946
    46
slouken@8582
    47
int SDLCALL
slouken@8582
    48
ThreadFunc(void *data)
slouken@8582
    49
{
slouken@11946
    50
    SDL_ThreadPriority prio = SDL_THREAD_PRIORITY_NORMAL;
slouken@11946
    51
slouken@8582
    52
    SDL_TLSSet(tls, "baby thread", NULL);
slouken@8582
    53
    SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n",
slouken@8582
    54
           (char *) data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
slouken@8582
    55
    while (alive) {
slouken@8582
    56
        SDL_Log("Thread '%s' is alive!\n", (char *) data);
slouken@11946
    57
slouken@11946
    58
        if (testprio) {
slouken@11946
    59
            SDL_Log("SDL_SetThreadPriority(%s):%d\n", getprioritystr(prio), SDL_SetThreadPriority(prio));
slouken@11952
    60
            if (++prio > SDL_THREAD_PRIORITY_TIME_CRITICAL)
slouken@11946
    61
                prio = SDL_THREAD_PRIORITY_LOW;
slouken@11946
    62
        }
slouken@11946
    63
slouken@8582
    64
        SDL_Delay(1 * 1000);
slouken@8582
    65
    }
slouken@8582
    66
    SDL_Log("Thread '%s' exiting!\n", (char *) data);
slouken@8582
    67
    return (0);
slouken@8582
    68
}
slouken@8582
    69
slouken@8582
    70
static void
slouken@8582
    71
killed(int sig)
slouken@8582
    72
{
slouken@8582
    73
    SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n");
slouken@8582
    74
    SDL_Delay(5 * 1000);
slouken@8582
    75
    alive = 0;
slouken@8582
    76
    quit(0);
slouken@8582
    77
}
slouken@8582
    78
slouken@8582
    79
int
slouken@8582
    80
main(int argc, char *argv[])
slouken@8582
    81
{
slouken@11946
    82
    int arg = 1;
slouken@8582
    83
    SDL_Thread *thread;
slouken@8582
    84
philipp@9922
    85
    /* Enable standard application logging */
slouken@8582
    86
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@8582
    87
slouken@8582
    88
    /* Load the SDL library */
slouken@8582
    89
    if (SDL_Init(0) < 0) {
slouken@8582
    90
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@8582
    91
        return (1);
slouken@8582
    92
    }
slouken@8582
    93
slouken@11946
    94
    while (argv[arg] && *argv[arg] == '-') {
slouken@11946
    95
        if (SDL_strcmp(argv[arg], "--prio") == 0) {
slouken@11946
    96
            testprio = 1;
slouken@11946
    97
        }
slouken@11946
    98
        ++arg;
slouken@11946
    99
    }
slouken@11946
   100
slouken@8582
   101
    tls = SDL_TLSCreate();
slouken@8582
   102
    SDL_assert(tls);
slouken@8582
   103
    SDL_TLSSet(tls, "main thread", NULL);
slouken@8582
   104
    SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
slouken@8582
   105
slouken@8582
   106
    alive = 1;
slouken@8582
   107
    thread = SDL_CreateThread(ThreadFunc, "One", "#1");
slouken@8582
   108
    if (thread == NULL) {
slouken@8582
   109
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
slouken@8582
   110
        quit(1);
slouken@8582
   111
    }
slouken@8582
   112
    SDL_Delay(5 * 1000);
slouken@8582
   113
    SDL_Log("Waiting for thread #1\n");
slouken@8582
   114
    alive = 0;
slouken@8582
   115
    SDL_WaitThread(thread, NULL);
slouken@8582
   116
slouken@8582
   117
    SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
slouken@8582
   118
slouken@8582
   119
    alive = 1;
slouken@8582
   120
    signal(SIGTERM, killed);
slouken@8582
   121
    thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
slouken@8582
   122
    if (thread == NULL) {
slouken@8582
   123
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
slouken@8582
   124
        quit(1);
slouken@8582
   125
    }
slouken@8582
   126
    raise(SIGTERM);
slouken@8582
   127
slouken@8582
   128
    SDL_Quit();                 /* Never reached */
slouken@8582
   129
    return (0);                 /* Never reached */
slouken@8582
   130
}