test/torturethread.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
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@1662
    17
static void
slouken@1662
    18
quit (int rc)
icculus@1151
    19
{
slouken@1662
    20
    SDL_Quit ();
slouken@1662
    21
    exit (rc);
icculus@1151
    22
}
icculus@1151
    23
slouken@1662
    24
int SDLCALL
slouken@1662
    25
SubThreadFunc (void *data)
slouken@1662
    26
{
slouken@1662
    27
    while (!*(int volatile *) data) {
slouken@1662
    28
        ;                       /*SDL_Delay(10); *//* do nothing */
slouken@1662
    29
    }
slouken@1662
    30
    return 0;
slouken@0
    31
}
slouken@0
    32
slouken@1662
    33
int SDLCALL
slouken@1662
    34
ThreadFunc (void *data)
slouken@1662
    35
{
slouken@1662
    36
    SDL_Thread *sub_threads[NUMTHREADS];
slouken@1662
    37
    int flags[NUMTHREADS];
slouken@1662
    38
    int i;
slouken@1662
    39
    int tid = (int) (uintptr_t) data;
slouken@0
    40
slouken@1662
    41
    fprintf (stderr, "Creating Thread %d\n", tid);
slouken@0
    42
slouken@1662
    43
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1662
    44
        flags[i] = 0;
slouken@1662
    45
        sub_threads[i] = SDL_CreateThread (SubThreadFunc, &flags[i]);
slouken@1662
    46
    }
slouken@0
    47
slouken@1662
    48
    printf ("Thread '%d' waiting for signal\n", tid);
slouken@1662
    49
    while (time_for_threads_to_die[tid] != 1) {
slouken@1662
    50
        ;                       /* do nothing */
slouken@1662
    51
    }
slouken@0
    52
slouken@1662
    53
    printf ("Thread '%d' sending signals to subthreads\n", tid);
slouken@1662
    54
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1662
    55
        flags[i] = 1;
slouken@1662
    56
        SDL_WaitThread (sub_threads[i], NULL);
slouken@1662
    57
    }
slouken@0
    58
slouken@1662
    59
    printf ("Thread '%d' exiting!\n", tid);
slouken@0
    60
slouken@1662
    61
    return 0;
slouken@0
    62
}
slouken@0
    63
slouken@1662
    64
int
slouken@1662
    65
main (int argc, char *argv[])
slouken@0
    66
{
slouken@1662
    67
    SDL_Thread *threads[NUMTHREADS];
slouken@1662
    68
    int i;
slouken@0
    69
slouken@1662
    70
    /* Load the SDL library */
slouken@1662
    71
    if (SDL_Init (0) < 0) {
slouken@1662
    72
        fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
slouken@1662
    73
        return (1);
slouken@1662
    74
    }
slouken@0
    75
slouken@1662
    76
    signal (SIGSEGV, SIG_DFL);
slouken@1662
    77
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1662
    78
        time_for_threads_to_die[i] = 0;
slouken@1662
    79
        threads[i] = SDL_CreateThread (ThreadFunc, (void *) (uintptr_t) i);
slouken@0
    80
slouken@1662
    81
        if (threads[i] == NULL) {
slouken@1662
    82
            fprintf (stderr, "Couldn't create thread: %s\n", SDL_GetError ());
slouken@1662
    83
            quit (1);
slouken@1662
    84
        }
slouken@1662
    85
    }
slouken@0
    86
slouken@1662
    87
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1662
    88
        time_for_threads_to_die[i] = 1;
slouken@1662
    89
    }
slouken@1662
    90
slouken@1662
    91
    for (i = 0; i < NUMTHREADS; i++) {
slouken@1662
    92
        SDL_WaitThread (threads[i], NULL);
slouken@1662
    93
    }
slouken@1662
    94
    SDL_Quit ();
slouken@1662
    95
    return (0);
slouken@0
    96
}