test/testlock.c
author Paul Hunkin <paul@bieh.net>
Mon, 17 May 2010 15:14:34 +1200
changeset 4695 b7918263da92
parent 3578 0d1b16ee0bca
child 5535 96594ac5fd1a
permissions -rw-r--r--
Initial commit, adding __ANDROID__ to the platform list
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@3578
    14
static SDL_threadID mainthread;
slouken@0
    15
static SDL_Thread *threads[6];
slouken@1769
    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@1895
    22
static void
slouken@1895
    23
SDL_Quit_Wrapper(void)
icculus@1151
    24
{
slouken@1895
    25
    SDL_Quit();
icculus@1151
    26
}
icculus@1151
    27
slouken@1895
    28
void
slouken@1895
    29
printid(void)
slouken@0
    30
{
slouken@3578
    31
    printf("Process %lu:  exiting\n", SDL_ThreadID());
slouken@0
    32
}
slouken@0
    33
slouken@1895
    34
void
slouken@1895
    35
terminate(int sig)
slouken@0
    36
{
slouken@1895
    37
    signal(SIGINT, terminate);
slouken@1895
    38
    doterminate = 1;
slouken@1895
    39
}
slouken@0
    40
slouken@1895
    41
void
slouken@1895
    42
closemutex(int sig)
slouken@1895
    43
{
slouken@3578
    44
    SDL_threadID id = SDL_ThreadID();
slouken@1895
    45
    int i;
slouken@3578
    46
    printf("Process %lu:  Cleaning up...\n", id == mainthread ? 0 : id);
slouken@2779
    47
    doterminate = 1;
slouken@1895
    48
    for (i = 0; i < 6; ++i)
slouken@2779
    49
        SDL_WaitThread(threads[i], NULL);
slouken@1895
    50
    SDL_DestroyMutex(mutex);
slouken@1895
    51
    exit(sig);
slouken@1895
    52
}
slouken@0
    53
slouken@1895
    54
int SDLCALL
slouken@1895
    55
Run(void *data)
slouken@1895
    56
{
slouken@1895
    57
    if (SDL_ThreadID() == mainthread)
slouken@1895
    58
        signal(SIGTERM, closemutex);
slouken@2779
    59
    while (!doterminate) {
slouken@3578
    60
        printf("Process %lu ready to work\n", SDL_ThreadID());
slouken@1895
    61
        if (SDL_mutexP(mutex) < 0) {
slouken@1895
    62
            fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
slouken@1895
    63
            exit(1);
slouken@1895
    64
        }
slouken@3578
    65
        printf("Process %lu, working!\n", SDL_ThreadID());
slouken@1895
    66
        SDL_Delay(1 * 1000);
slouken@3578
    67
        printf("Process %lu, done!\n", SDL_ThreadID());
slouken@1895
    68
        if (SDL_mutexV(mutex) < 0) {
slouken@1895
    69
            fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
slouken@1895
    70
            exit(1);
slouken@1895
    71
        }
slouken@1895
    72
        /* If this sleep isn't done, then threads may starve */
slouken@1895
    73
        SDL_Delay(10);
slouken@2779
    74
    }
slouken@2779
    75
    if (SDL_ThreadID() == mainthread && doterminate) {
slouken@3578
    76
        printf("Process %lu:  raising SIGTERM\n", SDL_ThreadID());
slouken@2779
    77
        raise(SIGTERM);
slouken@1895
    78
    }
slouken@1895
    79
    return (0);
slouken@1895
    80
}
slouken@0
    81
slouken@1895
    82
int
slouken@1895
    83
main(int argc, char *argv[])
slouken@1895
    84
{
slouken@1895
    85
    int i;
slouken@1895
    86
    int maxproc = 6;
slouken@0
    87
slouken@1895
    88
    /* Load the SDL library */
slouken@1895
    89
    if (SDL_Init(0) < 0) {
slouken@1895
    90
        fprintf(stderr, "%s\n", SDL_GetError());
slouken@1895
    91
        exit(1);
slouken@1895
    92
    }
slouken@1895
    93
    atexit(SDL_Quit_Wrapper);
slouken@1895
    94
slouken@1895
    95
    if ((mutex = SDL_CreateMutex()) == NULL) {
slouken@1895
    96
        fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
slouken@1895
    97
        exit(1);
slouken@1895
    98
    }
slouken@1895
    99
slouken@1895
   100
    mainthread = SDL_ThreadID();
slouken@3578
   101
    printf("Main thread: %lu\n", mainthread);
slouken@1895
   102
    atexit(printid);
slouken@1895
   103
    for (i = 0; i < maxproc; ++i) {
slouken@1895
   104
        if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL)
slouken@1895
   105
            fprintf(stderr, "Couldn't create thread!\n");
slouken@1895
   106
    }
slouken@1895
   107
    signal(SIGINT, terminate);
slouken@1895
   108
    Run(NULL);
slouken@1895
   109
slouken@1895
   110
    return (0);                 /* Never reached */
slouken@0
   111
}