test/torturethread.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Jan 2009 04:05:28 +0000
changeset 3017 3272431eeee2
parent 1895 c121d94672cb
child 5535 96594ac5fd1a
permissions -rw-r--r--
Added testresample.c
slouken@0
     1
slouken@0
     2
/* Simple test of the SDL threading code */
slouken@0
     3
slouken@0
     4
#include <stdio.h>
slouken@0
     5
#include <stdlib.h>
slouken@0
     6
#include <signal.h>
slouken@0
     7
#include <string.h>
slouken@0
     8
slouken@0
     9
#include "SDL.h"
slouken@0
    10
#include "SDL_thread.h"
slouken@0
    11
slouken@0
    12
#define NUMTHREADS 10
slouken@0
    13
slouken@0
    14
static char volatile time_for_threads_to_die[NUMTHREADS];
slouken@0
    15
icculus@1151
    16
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    17
static void
slouken@1895
    18
quit(int rc)
icculus@1151
    19
{
slouken@1895
    20
    SDL_Quit();
slouken@1895
    21
    exit(rc);
icculus@1151
    22
}
icculus@1151
    23
slouken@1895
    24
int SDLCALL
slouken@1895
    25
SubThreadFunc(void *data)
slouken@1895
    26
{
slouken@1895
    27
    while (!*(int volatile *) data) {
slouken@1895
    28
        ;                       /*SDL_Delay(10); *//* do nothing */
slouken@1895
    29
    }
slouken@1895
    30
    return 0;
slouken@0
    31
}
slouken@0
    32
slouken@1895
    33
int SDLCALL
slouken@1895
    34
ThreadFunc(void *data)
slouken@1895
    35
{
slouken@1895
    36
    SDL_Thread *sub_threads[NUMTHREADS];
slouken@1895
    37
    int flags[NUMTHREADS];
slouken@1895
    38
    int i;
slouken@1895
    39
    int tid = (int) (uintptr_t) data;
slouken@0
    40
slouken@1895
    41
    fprintf(stderr, "Creating Thread %d\n", tid);
slouken@0
    42
slouken@1895
    43
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1895
    44
        flags[i] = 0;
slouken@1895
    45
        sub_threads[i] = SDL_CreateThread(SubThreadFunc, &flags[i]);
slouken@1895
    46
    }
slouken@0
    47
slouken@1895
    48
    printf("Thread '%d' waiting for signal\n", tid);
slouken@1895
    49
    while (time_for_threads_to_die[tid] != 1) {
slouken@1895
    50
        ;                       /* do nothing */
slouken@1895
    51
    }
slouken@0
    52
slouken@1895
    53
    printf("Thread '%d' sending signals to subthreads\n", tid);
slouken@1895
    54
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1895
    55
        flags[i] = 1;
slouken@1895
    56
        SDL_WaitThread(sub_threads[i], NULL);
slouken@1895
    57
    }
slouken@0
    58
slouken@1895
    59
    printf("Thread '%d' exiting!\n", tid);
slouken@0
    60
slouken@1895
    61
    return 0;
slouken@0
    62
}
slouken@0
    63
slouken@1895
    64
int
slouken@1895
    65
main(int argc, char *argv[])
slouken@0
    66
{
slouken@1895
    67
    SDL_Thread *threads[NUMTHREADS];
slouken@1895
    68
    int i;
slouken@0
    69
slouken@1895
    70
    /* Load the SDL library */
slouken@1895
    71
    if (SDL_Init(0) < 0) {
slouken@1895
    72
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    73
        return (1);
slouken@1895
    74
    }
slouken@0
    75
slouken@1895
    76
    signal(SIGSEGV, SIG_DFL);
slouken@1895
    77
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1895
    78
        time_for_threads_to_die[i] = 0;
slouken@1895
    79
        threads[i] = SDL_CreateThread(ThreadFunc, (void *) (uintptr_t) i);
slouken@0
    80
slouken@1895
    81
        if (threads[i] == NULL) {
slouken@1895
    82
            fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
slouken@1895
    83
            quit(1);
slouken@1895
    84
        }
slouken@1895
    85
    }
slouken@0
    86
slouken@1895
    87
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1895
    88
        time_for_threads_to_die[i] = 1;
slouken@1895
    89
    }
slouken@1895
    90
slouken@1895
    91
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1895
    92
        SDL_WaitThread(threads[i], NULL);
slouken@1895
    93
    }
slouken@1895
    94
    SDL_Quit();
slouken@1895
    95
    return (0);
slouken@0
    96
}