test/torturethread.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 18 Feb 2015 21:31:21 +0100
changeset 9356 e87d6e1e812a
parent 8149 681eb46b8ac4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Removed redundant include statements from test programs.

The needed header files are already included with SDL.h. Still including them in
the test programs is confusing because it somehow suggests they would be needed.
slouken@5535
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@0
    12
slouken@0
    13
/* Simple test of the SDL threading code */
slouken@0
    14
slouken@0
    15
#include <stdio.h>
slouken@0
    16
#include <stdlib.h>
slouken@0
    17
#include <signal.h>
slouken@0
    18
#include <string.h>
slouken@0
    19
slouken@0
    20
#include "SDL.h"
slouken@0
    21
slouken@0
    22
#define NUMTHREADS 10
slouken@0
    23
slouken@0
    24
static char volatile time_for_threads_to_die[NUMTHREADS];
slouken@0
    25
icculus@1151
    26
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    27
static void
slouken@1895
    28
quit(int rc)
icculus@1151
    29
{
slouken@1895
    30
    SDL_Quit();
slouken@1895
    31
    exit(rc);
icculus@1151
    32
}
icculus@1151
    33
slouken@1895
    34
int SDLCALL
slouken@1895
    35
SubThreadFunc(void *data)
slouken@1895
    36
{
slouken@1895
    37
    while (!*(int volatile *) data) {
gabomdq@7678
    38
        ;                       /* SDL_Delay(10); *//* do nothing */
slouken@1895
    39
    }
slouken@1895
    40
    return 0;
slouken@0
    41
}
slouken@0
    42
slouken@1895
    43
int SDLCALL
slouken@1895
    44
ThreadFunc(void *data)
slouken@1895
    45
{
slouken@1895
    46
    SDL_Thread *sub_threads[NUMTHREADS];
slouken@1895
    47
    int flags[NUMTHREADS];
slouken@1895
    48
    int i;
slouken@1895
    49
    int tid = (int) (uintptr_t) data;
slouken@0
    50
aschiffler@7639
    51
    SDL_Log("Creating Thread %d\n", tid);
slouken@0
    52
slouken@1895
    53
    for (i = 0; i < NUMTHREADS; i++) {
icculus@5969
    54
        char name[64];
icculus@5969
    55
        SDL_snprintf(name, sizeof (name), "Child%d_%d", tid, i);
slouken@1895
    56
        flags[i] = 0;
icculus@5969
    57
        sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
slouken@1895
    58
    }
slouken@0
    59
aschiffler@7639
    60
    SDL_Log("Thread '%d' waiting for signal\n", tid);
slouken@1895
    61
    while (time_for_threads_to_die[tid] != 1) {
slouken@1895
    62
        ;                       /* do nothing */
slouken@1895
    63
    }
slouken@0
    64
aschiffler@7639
    65
    SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
slouken@1895
    66
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1895
    67
        flags[i] = 1;
slouken@1895
    68
        SDL_WaitThread(sub_threads[i], NULL);
slouken@1895
    69
    }
slouken@0
    70
aschiffler@7639
    71
    SDL_Log("Thread '%d' exiting!\n", tid);
slouken@0
    72
slouken@1895
    73
    return 0;
slouken@0
    74
}
slouken@0
    75
slouken@1895
    76
int
slouken@1895
    77
main(int argc, char *argv[])
slouken@0
    78
{
slouken@1895
    79
    SDL_Thread *threads[NUMTHREADS];
slouken@1895
    80
    int i;
slouken@0
    81
aschiffler@7639
    82
	/* Enable standard application logging */
aschiffler@7639
    83
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    84
slouken@1895
    85
    /* Load the SDL library */
slouken@1895
    86
    if (SDL_Init(0) < 0) {
aschiffler@7639
    87
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    88
        return (1);
slouken@1895
    89
    }
slouken@0
    90
slouken@1895
    91
    signal(SIGSEGV, SIG_DFL);
slouken@1895
    92
    for (i = 0; i < NUMTHREADS; i++) {
icculus@5969
    93
        char name[64];
icculus@5969
    94
        SDL_snprintf(name, sizeof (name), "Parent%d", i);
slouken@1895
    95
        time_for_threads_to_die[i] = 0;
icculus@5969
    96
        threads[i] = SDL_CreateThread(ThreadFunc, name, (void*) (uintptr_t) i);
slouken@0
    97
slouken@1895
    98
        if (threads[i] == NULL) {
aschiffler@7639
    99
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
   100
            quit(1);
slouken@1895
   101
        }
slouken@1895
   102
    }
slouken@0
   103
slouken@1895
   104
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1895
   105
        time_for_threads_to_die[i] = 1;
slouken@1895
   106
    }
slouken@1895
   107
slouken@1895
   108
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1895
   109
        SDL_WaitThread(threads[i], NULL);
slouken@1895
   110
    }
slouken@1895
   111
    SDL_Quit();
slouken@1895
   112
    return (0);
slouken@0
   113
}