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