test/testthread.c
changeset 8535 e8ee0708ef5c
parent 8485 f0ede9ce6a44
parent 7639 9406b7dd2f2d
child 8582 c3e9a2b93517
     1.1 --- a/test/testthread.c	Sun Oct 27 14:31:57 2013 -0400
     1.2 +++ b/test/testthread.c	Sun Oct 27 21:26:46 2013 -0400
     1.3 @@ -1,96 +1,99 @@
     1.4 -/*
     1.5 -  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.6 -
     1.7 -  This software is provided 'as-is', without any express or implied
     1.8 -  warranty.  In no event will the authors be held liable for any damages
     1.9 -  arising from the use of this software.
    1.10 -
    1.11 -  Permission is granted to anyone to use this software for any purpose,
    1.12 -  including commercial applications, and to alter it and redistribute it
    1.13 -  freely.
    1.14 -*/
    1.15 -
    1.16 -/* Simple test of the SDL threading code */
    1.17 -
    1.18 -#include <stdio.h>
    1.19 -#include <stdlib.h>
    1.20 -#include <signal.h>
    1.21 -
    1.22 -#include "SDL.h"
    1.23 -#include "SDL_thread.h"
    1.24 -
    1.25 -static SDL_TLSID tls;
    1.26 -static int alive = 0;
    1.27 -
    1.28 -/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.29 -static void
    1.30 -quit(int rc)
    1.31 -{
    1.32 -    SDL_Quit();
    1.33 -    exit(rc);
    1.34 -}
    1.35 -
    1.36 -int SDLCALL
    1.37 -ThreadFunc(void *data)
    1.38 -{
    1.39 -    SDL_TLSSet(tls, "baby thread", NULL);
    1.40 -    SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n",
    1.41 -           (char *) data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
    1.42 -    while (alive) {
    1.43 -        SDL_Log("Thread '%s' is alive!\n", (char *) data);
    1.44 -        SDL_Delay(1 * 1000);
    1.45 -    }
    1.46 -    SDL_Log("Thread '%s' exiting!\n", (char *) data);
    1.47 -    return (0);
    1.48 -}
    1.49 -
    1.50 -static void
    1.51 -killed(int sig)
    1.52 -{
    1.53 -    SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n");
    1.54 -    SDL_Delay(5 * 1000);
    1.55 -    alive = 0;
    1.56 -    quit(0);
    1.57 -}
    1.58 -
    1.59 -int
    1.60 -main(int argc, char *argv[])
    1.61 -{
    1.62 -    SDL_Thread *thread;
    1.63 -
    1.64 -    /* Load the SDL library */
    1.65 -    if (SDL_Init(0) < 0) {
    1.66 -        SDL_Log("Couldn't initialize SDL: %s\n", SDL_GetError());
    1.67 -        return (1);
    1.68 -    }
    1.69 -
    1.70 -    tls = SDL_TLSCreate();
    1.71 -    SDL_assert(tls);
    1.72 -    SDL_TLSSet(tls, "main thread", NULL);
    1.73 -    SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
    1.74 -
    1.75 -    alive = 1;
    1.76 -    thread = SDL_CreateThread(ThreadFunc, "One", "#1");
    1.77 -    if (thread == NULL) {
    1.78 -        SDL_Log("Couldn't create thread: %s\n", SDL_GetError());
    1.79 -        quit(1);
    1.80 -    }
    1.81 -    SDL_Delay(5 * 1000);
    1.82 -    SDL_Log("Waiting for thread #1\n");
    1.83 -    alive = 0;
    1.84 -    SDL_WaitThread(thread, NULL);
    1.85 -
    1.86 -    SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
    1.87 -
    1.88 -    alive = 1;
    1.89 -    signal(SIGTERM, killed);
    1.90 -    thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
    1.91 -    if (thread == NULL) {
    1.92 -        SDL_Log("Couldn't create thread: %s\n", SDL_GetError());
    1.93 -        quit(1);
    1.94 -    }
    1.95 -    raise(SIGTERM);
    1.96 -
    1.97 -    SDL_Quit();                 /* Never reached */
    1.98 -    return (0);                 /* Never reached */
    1.99 -}
   1.100 +/*
   1.101 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
   1.102 +
   1.103 +  This software is provided 'as-is', without any express or implied
   1.104 +  warranty.  In no event will the authors be held liable for any damages
   1.105 +  arising from the use of this software.
   1.106 +
   1.107 +  Permission is granted to anyone to use this software for any purpose,
   1.108 +  including commercial applications, and to alter it and redistribute it
   1.109 +  freely.
   1.110 +*/
   1.111 +
   1.112 +/* Simple test of the SDL threading code */
   1.113 +
   1.114 +#include <stdio.h>
   1.115 +#include <stdlib.h>
   1.116 +#include <signal.h>
   1.117 +
   1.118 +#include "SDL.h"
   1.119 +#include "SDL_thread.h"
   1.120 +
   1.121 +static SDL_TLSID tls;
   1.122 +static int alive = 0;
   1.123 +
   1.124 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   1.125 +static void
   1.126 +quit(int rc)
   1.127 +{
   1.128 +    SDL_Quit();
   1.129 +    exit(rc);
   1.130 +}
   1.131 +
   1.132 +int SDLCALL
   1.133 +ThreadFunc(void *data)
   1.134 +{
   1.135 +    SDL_TLSSet(tls, "baby thread", NULL);
   1.136 +    SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n",
   1.137 +           (char *) data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls));
   1.138 +    while (alive) {
   1.139 +        SDL_Log("Thread '%s' is alive!\n", (char *) data);
   1.140 +        SDL_Delay(1 * 1000);
   1.141 +    }
   1.142 +    SDL_Log("Thread '%s' exiting!\n", (char *) data);
   1.143 +    return (0);
   1.144 +}
   1.145 +
   1.146 +static void
   1.147 +killed(int sig)
   1.148 +{
   1.149 +    SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n");
   1.150 +    SDL_Delay(5 * 1000);
   1.151 +    alive = 0;
   1.152 +    quit(0);
   1.153 +}
   1.154 +
   1.155 +int
   1.156 +main(int argc, char *argv[])
   1.157 +{
   1.158 +    SDL_Thread *thread;
   1.159 +
   1.160 +	/* Enable standard application logging */
   1.161 +    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   1.162 +
   1.163 +    /* Load the SDL library */
   1.164 +    if (SDL_Init(0) < 0) {
   1.165 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.166 +        return (1);
   1.167 +    }
   1.168 +
   1.169 +    tls = SDL_TLSCreate();
   1.170 +    SDL_assert(tls);
   1.171 +    SDL_TLSSet(tls, "main thread", NULL);
   1.172 +    SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls));
   1.173 +
   1.174 +    alive = 1;
   1.175 +    thread = SDL_CreateThread(ThreadFunc, "One", "#1");
   1.176 +    if (thread == NULL) {
   1.177 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
   1.178 +        quit(1);
   1.179 +    }
   1.180 +    SDL_Delay(5 * 1000);
   1.181 +    SDL_Log("Waiting for thread #1\n");
   1.182 +    alive = 0;
   1.183 +    SDL_WaitThread(thread, NULL);
   1.184 +
   1.185 +    SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls));
   1.186 +
   1.187 +    alive = 1;
   1.188 +    signal(SIGTERM, killed);
   1.189 +    thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
   1.190 +    if (thread == NULL) {
   1.191 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
   1.192 +        quit(1);
   1.193 +    }
   1.194 +    raise(SIGTERM);
   1.195 +
   1.196 +    SDL_Quit();                 /* Never reached */
   1.197 +    return (0);                 /* Never reached */
   1.198 +}