test/testlock.c
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 18 Jul 2008 19:23:13 +0000
branchgsoc2008_iphone
changeset 2377 e9a0bad0ea07
parent 1895 c121d94672cb
child 2779 4436464c4f51
permissions -rw-r--r--
Just renamed it...
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@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@1895
    31
    printf("Process %u:  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@1895
    44
    Uint32 id = SDL_ThreadID();
slouken@1895
    45
    int i;
slouken@1895
    46
    printf("Process %u:  Cleaning up...\n", id == mainthread ? 0 : id);
slouken@1895
    47
    for (i = 0; i < 6; ++i)
slouken@1895
    48
        SDL_KillThread(threads[i]);
slouken@1895
    49
    SDL_DestroyMutex(mutex);
slouken@1895
    50
    exit(sig);
slouken@1895
    51
}
slouken@0
    52
slouken@1895
    53
int SDLCALL
slouken@1895
    54
Run(void *data)
slouken@1895
    55
{
slouken@1895
    56
    if (SDL_ThreadID() == mainthread)
slouken@1895
    57
        signal(SIGTERM, closemutex);
slouken@1895
    58
    while (1) {
slouken@1895
    59
        printf("Process %u ready to work\n", SDL_ThreadID());
slouken@1895
    60
        if (SDL_mutexP(mutex) < 0) {
slouken@1895
    61
            fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
slouken@1895
    62
            exit(1);
slouken@1895
    63
        }
slouken@1895
    64
        printf("Process %u, working!\n", SDL_ThreadID());
slouken@1895
    65
        SDL_Delay(1 * 1000);
slouken@1895
    66
        printf("Process %u, done!\n", SDL_ThreadID());
slouken@1895
    67
        if (SDL_mutexV(mutex) < 0) {
slouken@1895
    68
            fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
slouken@1895
    69
            exit(1);
slouken@1895
    70
        }
slouken@1895
    71
        /* If this sleep isn't done, then threads may starve */
slouken@1895
    72
        SDL_Delay(10);
slouken@1895
    73
        if (SDL_ThreadID() == mainthread && doterminate) {
slouken@1895
    74
            printf("Process %u:  raising SIGTERM\n", SDL_ThreadID());
slouken@1895
    75
            raise(SIGTERM);
slouken@1895
    76
        }
slouken@1895
    77
    }
slouken@1895
    78
    return (0);
slouken@1895
    79
}
slouken@0
    80
slouken@1895
    81
int
slouken@1895
    82
main(int argc, char *argv[])
slouken@1895
    83
{
slouken@1895
    84
    int i;
slouken@1895
    85
    int maxproc = 6;
slouken@0
    86
slouken@1895
    87
    /* Load the SDL library */
slouken@1895
    88
    if (SDL_Init(0) < 0) {
slouken@1895
    89
        fprintf(stderr, "%s\n", SDL_GetError());
slouken@1895
    90
        exit(1);
slouken@1895
    91
    }
slouken@1895
    92
    atexit(SDL_Quit_Wrapper);
slouken@1895
    93
slouken@1895
    94
    if ((mutex = SDL_CreateMutex()) == NULL) {
slouken@1895
    95
        fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
slouken@1895
    96
        exit(1);
slouken@1895
    97
    }
slouken@1895
    98
slouken@1895
    99
    mainthread = SDL_ThreadID();
slouken@1895
   100
    printf("Main thread: %u\n", mainthread);
slouken@1895
   101
    atexit(printid);
slouken@1895
   102
    for (i = 0; i < maxproc; ++i) {
slouken@1895
   103
        if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL)
slouken@1895
   104
            fprintf(stderr, "Couldn't create thread!\n");
slouken@1895
   105
    }
slouken@1895
   106
    signal(SIGINT, terminate);
slouken@1895
   107
    Run(NULL);
slouken@1895
   108
slouken@1895
   109
    return (0);                 /* Never reached */
slouken@0
   110
}