test/torturethread.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 02 Oct 2011 00:29:16 -0400
changeset 5969 3a041d215edc
parent 5535 96594ac5fd1a
child 7517 965d57022c01
permissions -rw-r--r--
1.3 API CHANGE: Add support for naming threads.
     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 #include <string.h>
    19 
    20 #include "SDL.h"
    21 #include "SDL_thread.h"
    22 
    23 #define NUMTHREADS 10
    24 
    25 static char volatile time_for_threads_to_die[NUMTHREADS];
    26 
    27 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    28 static void
    29 quit(int rc)
    30 {
    31     SDL_Quit();
    32     exit(rc);
    33 }
    34 
    35 int SDLCALL
    36 SubThreadFunc(void *data)
    37 {
    38     while (!*(int volatile *) data) {
    39         ;                       /*SDL_Delay(10); *//* do nothing */
    40     }
    41     return 0;
    42 }
    43 
    44 int SDLCALL
    45 ThreadFunc(void *data)
    46 {
    47     SDL_Thread *sub_threads[NUMTHREADS];
    48     int flags[NUMTHREADS];
    49     int i;
    50     int tid = (int) (uintptr_t) data;
    51 
    52     fprintf(stderr, "Creating Thread %d\n", tid);
    53 
    54     for (i = 0; i < NUMTHREADS; i++) {
    55         char name[64];
    56         SDL_snprintf(name, sizeof (name), "Child%d_%d", tid, i);
    57         flags[i] = 0;
    58         sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
    59     }
    60 
    61     printf("Thread '%d' waiting for signal\n", tid);
    62     while (time_for_threads_to_die[tid] != 1) {
    63         ;                       /* do nothing */
    64     }
    65 
    66     printf("Thread '%d' sending signals to subthreads\n", tid);
    67     for (i = 0; i < NUMTHREADS; i++) {
    68         flags[i] = 1;
    69         SDL_WaitThread(sub_threads[i], NULL);
    70     }
    71 
    72     printf("Thread '%d' exiting!\n", tid);
    73 
    74     return 0;
    75 }
    76 
    77 int
    78 main(int argc, char *argv[])
    79 {
    80     SDL_Thread *threads[NUMTHREADS];
    81     int i;
    82 
    83     /* Load the SDL library */
    84     if (SDL_Init(0) < 0) {
    85         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    86         return (1);
    87     }
    88 
    89     signal(SIGSEGV, SIG_DFL);
    90     for (i = 0; i < NUMTHREADS; i++) {
    91         char name[64];
    92         SDL_snprintf(name, sizeof (name), "Parent%d", i);
    93         time_for_threads_to_die[i] = 0;
    94         threads[i] = SDL_CreateThread(ThreadFunc, name, (void*) (uintptr_t) i);
    95 
    96         if (threads[i] == NULL) {
    97             fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
    98             quit(1);
    99         }
   100     }
   101 
   102     for (i = 0; i < NUMTHREADS; i++) {
   103         time_for_threads_to_die[i] = 1;
   104     }
   105 
   106     for (i = 0; i < NUMTHREADS; i++) {
   107         SDL_WaitThread(threads[i], NULL);
   108     }
   109     SDL_Quit();
   110     return (0);
   111 }