test/torturethread.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 28 Dec 2014 22:00:24 +0100
changeset 9301 7377a9a3aed6
parent 8149 681eb46b8ac4
child 9356 e87d6e1e812a
permissions -rw-r--r--
Renamed README-emscripten.txt to README-emscripten.md.
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
#include "SDL_thread.h"
slouken@0
    22
slouken@0
    23
#define NUMTHREADS 10
slouken@0
    24
slouken@0
    25
static char volatile time_for_threads_to_die[NUMTHREADS];
slouken@0
    26
icculus@1151
    27
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    28
static void
slouken@1895
    29
quit(int rc)
icculus@1151
    30
{
slouken@1895
    31
    SDL_Quit();
slouken@1895
    32
    exit(rc);
icculus@1151
    33
}
icculus@1151
    34
slouken@1895
    35
int SDLCALL
slouken@1895
    36
SubThreadFunc(void *data)
slouken@1895
    37
{
slouken@1895
    38
    while (!*(int volatile *) data) {
gabomdq@7678
    39
        ;                       /* SDL_Delay(10); *//* do nothing */
slouken@1895
    40
    }
slouken@1895
    41
    return 0;
slouken@0
    42
}
slouken@0
    43
slouken@1895
    44
int SDLCALL
slouken@1895
    45
ThreadFunc(void *data)
slouken@1895
    46
{
slouken@1895
    47
    SDL_Thread *sub_threads[NUMTHREADS];
slouken@1895
    48
    int flags[NUMTHREADS];
slouken@1895
    49
    int i;
slouken@1895
    50
    int tid = (int) (uintptr_t) data;
slouken@0
    51
aschiffler@7639
    52
    SDL_Log("Creating Thread %d\n", tid);
slouken@0
    53
slouken@1895
    54
    for (i = 0; i < NUMTHREADS; i++) {
icculus@5969
    55
        char name[64];
icculus@5969
    56
        SDL_snprintf(name, sizeof (name), "Child%d_%d", tid, i);
slouken@1895
    57
        flags[i] = 0;
icculus@5969
    58
        sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
slouken@1895
    59
    }
slouken@0
    60
aschiffler@7639
    61
    SDL_Log("Thread '%d' waiting for signal\n", tid);
slouken@1895
    62
    while (time_for_threads_to_die[tid] != 1) {
slouken@1895
    63
        ;                       /* do nothing */
slouken@1895
    64
    }
slouken@0
    65
aschiffler@7639
    66
    SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
slouken@1895
    67
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1895
    68
        flags[i] = 1;
slouken@1895
    69
        SDL_WaitThread(sub_threads[i], NULL);
slouken@1895
    70
    }
slouken@0
    71
aschiffler@7639
    72
    SDL_Log("Thread '%d' exiting!\n", tid);
slouken@0
    73
slouken@1895
    74
    return 0;
slouken@0
    75
}
slouken@0
    76
slouken@1895
    77
int
slouken@1895
    78
main(int argc, char *argv[])
slouken@0
    79
{
slouken@1895
    80
    SDL_Thread *threads[NUMTHREADS];
slouken@1895
    81
    int i;
slouken@0
    82
aschiffler@7639
    83
	/* Enable standard application logging */
aschiffler@7639
    84
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    85
slouken@1895
    86
    /* Load the SDL library */
slouken@1895
    87
    if (SDL_Init(0) < 0) {
aschiffler@7639
    88
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    89
        return (1);
slouken@1895
    90
    }
slouken@0
    91
slouken@1895
    92
    signal(SIGSEGV, SIG_DFL);
slouken@1895
    93
    for (i = 0; i < NUMTHREADS; i++) {
icculus@5969
    94
        char name[64];
icculus@5969
    95
        SDL_snprintf(name, sizeof (name), "Parent%d", i);
slouken@1895
    96
        time_for_threads_to_die[i] = 0;
icculus@5969
    97
        threads[i] = SDL_CreateThread(ThreadFunc, name, (void*) (uintptr_t) i);
slouken@0
    98
slouken@1895
    99
        if (threads[i] == NULL) {
aschiffler@7639
   100
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
   101
            quit(1);
slouken@1895
   102
        }
slouken@1895
   103
    }
slouken@0
   104
slouken@1895
   105
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1895
   106
        time_for_threads_to_die[i] = 1;
slouken@1895
   107
    }
slouken@1895
   108
slouken@1895
   109
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1895
   110
        SDL_WaitThread(threads[i], NULL);
slouken@1895
   111
    }
slouken@1895
   112
    SDL_Quit();
slouken@1895
   113
    return (0);
slouken@0
   114
}