test/testlock.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
slouken@0
     1
slouken@0
     2
/* Test the thread and mutex locking functions 
slouken@0
     3
   Also exercises the system's signal/thread interaction
slouken@0
     4
*/
slouken@0
     5
slouken@0
     6
#include <signal.h>
slouken@0
     7
#include <stdio.h>
slouken@0
     8
slouken@0
     9
#include "SDL.h"
slouken@0
    10
#include "SDL_mutex.h"
slouken@0
    11
#include "SDL_thread.h"
slouken@0
    12
slouken@0
    13
static SDL_mutex *mutex = NULL;
slouken@0
    14
static Uint32 mainthread;
slouken@0
    15
static SDL_Thread *threads[6];
slouken@1659
    16
static volatile int doterminate = 0;
slouken@0
    17
icculus@1151
    18
/*
icculus@1151
    19
 * SDL_Quit() shouldn't be used with atexit() directly because
icculus@1151
    20
 *  calling conventions may differ...
icculus@1151
    21
 */
slouken@1662
    22
static void
slouken@1668
    23
SDL_Quit_Wrapper(void)
icculus@1151
    24
{
slouken@1668
    25
    SDL_Quit();
icculus@1151
    26
}
icculus@1151
    27
slouken@1662
    28
void
slouken@1668
    29
printid(void)
slouken@0
    30
{
slouken@1668
    31
    printf("Process %u:  exiting\n", SDL_ThreadID());
slouken@0
    32
}
slouken@0
    33
slouken@1662
    34
void
slouken@1668
    35
terminate(int sig)
slouken@0
    36
{
slouken@1668
    37
    signal(SIGINT, terminate);
slouken@1662
    38
    doterminate = 1;
slouken@1662
    39
}
slouken@0
    40
slouken@1662
    41
void
slouken@1668
    42
closemutex(int sig)
slouken@1662
    43
{
slouken@1668
    44
    Uint32 id = SDL_ThreadID();
slouken@1662
    45
    int i;
slouken@1668
    46
    printf("Process %u:  Cleaning up...\n", id == mainthread ? 0 : id);
slouken@1662
    47
    for (i = 0; i < 6; ++i)
slouken@1668
    48
        SDL_KillThread(threads[i]);
slouken@1668
    49
    SDL_DestroyMutex(mutex);
slouken@1668
    50
    exit(sig);
slouken@1662
    51
}
slouken@0
    52
slouken@1662
    53
int SDLCALL
slouken@1668
    54
Run(void *data)
slouken@1662
    55
{
slouken@1668
    56
    if (SDL_ThreadID() == mainthread)
slouken@1668
    57
        signal(SIGTERM, closemutex);
slouken@1662
    58
    while (1) {
slouken@1668
    59
        printf("Process %u ready to work\n", SDL_ThreadID());
slouken@1668
    60
        if (SDL_mutexP(mutex) < 0) {
slouken@1668
    61
            fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
slouken@1668
    62
            exit(1);
slouken@1662
    63
        }
slouken@1668
    64
        printf("Process %u, working!\n", SDL_ThreadID());
slouken@1668
    65
        SDL_Delay(1 * 1000);
slouken@1668
    66
        printf("Process %u, done!\n", SDL_ThreadID());
slouken@1668
    67
        if (SDL_mutexV(mutex) < 0) {
slouken@1668
    68
            fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
slouken@1668
    69
            exit(1);
slouken@1662
    70
        }
slouken@1662
    71
        /* If this sleep isn't done, then threads may starve */
slouken@1668
    72
        SDL_Delay(10);
slouken@1668
    73
        if (SDL_ThreadID() == mainthread && doterminate) {
slouken@1668
    74
            printf("Process %u:  raising SIGTERM\n", SDL_ThreadID());
slouken@1668
    75
            raise(SIGTERM);
slouken@1662
    76
        }
slouken@1662
    77
    }
slouken@1662
    78
    return (0);
slouken@1662
    79
}
slouken@0
    80
slouken@1662
    81
int
slouken@1668
    82
main(int argc, char *argv[])
slouken@1662
    83
{
slouken@1662
    84
    int i;
slouken@1662
    85
    int maxproc = 6;
slouken@0
    86
slouken@1662
    87
    /* Load the SDL library */
slouken@1668
    88
    if (SDL_Init(0) < 0) {
slouken@1668
    89
        fprintf(stderr, "%s\n", SDL_GetError());
slouken@1668
    90
        exit(1);
slouken@1662
    91
    }
slouken@1668
    92
    atexit(SDL_Quit_Wrapper);
slouken@1662
    93
slouken@1668
    94
    if ((mutex = SDL_CreateMutex()) == NULL) {
slouken@1668
    95
        fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
slouken@1668
    96
        exit(1);
slouken@1662
    97
    }
slouken@1662
    98
slouken@1668
    99
    mainthread = SDL_ThreadID();
slouken@1668
   100
    printf("Main thread: %u\n", mainthread);
slouken@1668
   101
    atexit(printid);
slouken@1662
   102
    for (i = 0; i < maxproc; ++i) {
slouken@1668
   103
        if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL)
slouken@1668
   104
            fprintf(stderr, "Couldn't create thread!\n");
slouken@1662
   105
    }
slouken@1668
   106
    signal(SIGINT, terminate);
slouken@1668
   107
    Run(NULL);
slouken@1662
   108
slouken@1662
   109
    return (0);                 /* Never reached */
slouken@0
   110
}