test/testthread.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 10 Jul 2013 18:31:17 -0700
changeset 7393 358696c354a8
parent 7391 a29895dc5e9a
child 7517 965d57022c01
permissions -rw-r--r--
Added release/acquire memory barriers to the atomic API
* Added a destructor to clean up TLS memory at thread shutdown
* Refactored the TLS code to have platform independent code and a small platform dependent core with a fallback to generic code if platform dependent functions fail.
* Fixed recursion issues with SDL_GetErrBuf()
     1 /*
     2   Copyright (C) 1997-2011 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     printf("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         printf("Thread '%s' is alive!\n", (char *) data);
    41         SDL_Delay(1 * 1000);
    42     }
    43     printf("Thread '%s' exiting!\n", (char *) data);
    44     return (0);
    45 }
    46 
    47 static void
    48 killed(int sig)
    49 {
    50     printf("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         fprintf(stderr, "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     printf("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         fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    76         quit(1);
    77     }
    78     SDL_Delay(5 * 1000);
    79     printf("Waiting for thread #1\n");
    80     alive = 0;
    81     SDL_WaitThread(thread, NULL);
    82 
    83     printf("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         fprintf(stderr, "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 }