test/torturethread.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 17 Aug 2016 21:04:50 +0200
changeset 10275 ba7dc3ebb81e
parent 10003 d91a2c45825e
permissions -rw-r--r--
Emscripten: Fixed opening previously closed joystick.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
     1
/*
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9922
diff changeset
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
     3
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
     4
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
     6
  arising from the use of this software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
     7
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
    10
  freely.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
    11
*/
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    13
/* Simple test of the SDL threading code */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    14
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    15
#include <stdio.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    16
#include <stdlib.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    17
#include <signal.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    18
#include <string.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    19
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
#include "SDL.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    21
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
#define NUMTHREADS 10
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
10003
d91a2c45825e Remove almost all instances of "volatile" keyword.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    24
static SDL_atomic_t time_for_threads_to_die[NUMTHREADS];
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
1151
be9c9c8f6d53 Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
    26
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    27
static void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    28
quit(int rc)
1151
be9c9c8f6d53 Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
    29
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    30
    SDL_Quit();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    31
    exit(rc);
1151
be9c9c8f6d53 Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
    32
}
be9c9c8f6d53 Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents: 0
diff changeset
    33
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    34
int SDLCALL
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    35
SubThreadFunc(void *data)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    36
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    37
    while (!*(int volatile *) data) {
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7639
diff changeset
    38
        ;                       /* SDL_Delay(10); *//* do nothing */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    39
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    40
    return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    41
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    42
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    43
int SDLCALL
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    44
ThreadFunc(void *data)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    45
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    46
    SDL_Thread *sub_threads[NUMTHREADS];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    47
    int flags[NUMTHREADS];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    48
    int i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    49
    int tid = (int) (uintptr_t) data;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    50
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    51
    SDL_Log("Creating Thread %d\n", tid);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    52
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    53
    for (i = 0; i < NUMTHREADS; i++) {
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    54
        char name[64];
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    55
        SDL_snprintf(name, sizeof (name), "Child%d_%d", tid, i);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    56
        flags[i] = 0;
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    57
        sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    58
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    59
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    60
    SDL_Log("Thread '%d' waiting for signal\n", tid);
10003
d91a2c45825e Remove almost all instances of "volatile" keyword.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    61
    while (SDL_AtomicGet(&time_for_threads_to_die[tid]) != 1) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    62
        ;                       /* do nothing */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    63
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    64
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    65
    SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    66
    for (i = 0; i < NUMTHREADS; i++) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    67
        flags[i] = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    68
        SDL_WaitThread(sub_threads[i], NULL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    69
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    70
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    71
    SDL_Log("Thread '%d' exiting!\n", tid);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    72
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    73
    return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    74
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    75
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    76
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    77
main(int argc, char *argv[])
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    78
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    79
    SDL_Thread *threads[NUMTHREADS];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    80
    int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    81
9922
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9649
diff changeset
    82
    /* Enable standard application logging */
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    83
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    84
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    85
    /* Load the SDL library */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    86
    if (SDL_Init(0) < 0) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    87
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    88
        return (1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    89
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    90
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    91
    signal(SIGSEGV, SIG_DFL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    92
    for (i = 0; i < NUMTHREADS; i++) {
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    93
        char name[64];
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    94
        SDL_snprintf(name, sizeof (name), "Parent%d", i);
10003
d91a2c45825e Remove almost all instances of "volatile" keyword.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    95
        SDL_AtomicSet(&time_for_threads_to_die[i], 0);
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    96
        threads[i] = SDL_CreateThread(ThreadFunc, name, (void*) (uintptr_t) i);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    97
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    98
        if (threads[i] == NULL) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    99
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   100
            quit(1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   101
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   102
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   103
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   104
    for (i = 0; i < NUMTHREADS; i++) {
10003
d91a2c45825e Remove almost all instances of "volatile" keyword.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   105
        SDL_AtomicSet(&time_for_threads_to_die[i], 1);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   106
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   107
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   108
    for (i = 0; i < NUMTHREADS; i++) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   109
        SDL_WaitThread(threads[i], NULL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   110
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   111
    SDL_Quit();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   112
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   113
}