test/testlock.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 3578 0d1b16ee0bca
child 5969 3a041d215edc
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@0
    12
slouken@0
    13
/* Test the thread and mutex locking functions 
slouken@0
    14
   Also exercises the system's signal/thread interaction
slouken@0
    15
*/
slouken@0
    16
slouken@0
    17
#include <signal.h>
slouken@0
    18
#include <stdio.h>
slouken@0
    19
slouken@0
    20
#include "SDL.h"
slouken@0
    21
#include "SDL_mutex.h"
slouken@0
    22
#include "SDL_thread.h"
slouken@0
    23
slouken@0
    24
static SDL_mutex *mutex = NULL;
slouken@3578
    25
static SDL_threadID mainthread;
slouken@0
    26
static SDL_Thread *threads[6];
slouken@1769
    27
static volatile int doterminate = 0;
slouken@0
    28
icculus@1151
    29
/*
icculus@1151
    30
 * SDL_Quit() shouldn't be used with atexit() directly because
icculus@1151
    31
 *  calling conventions may differ...
icculus@1151
    32
 */
slouken@1895
    33
static void
slouken@1895
    34
SDL_Quit_Wrapper(void)
icculus@1151
    35
{
slouken@1895
    36
    SDL_Quit();
icculus@1151
    37
}
icculus@1151
    38
slouken@1895
    39
void
slouken@1895
    40
printid(void)
slouken@0
    41
{
slouken@3578
    42
    printf("Process %lu:  exiting\n", SDL_ThreadID());
slouken@0
    43
}
slouken@0
    44
slouken@1895
    45
void
slouken@1895
    46
terminate(int sig)
slouken@0
    47
{
slouken@1895
    48
    signal(SIGINT, terminate);
slouken@1895
    49
    doterminate = 1;
slouken@1895
    50
}
slouken@0
    51
slouken@1895
    52
void
slouken@1895
    53
closemutex(int sig)
slouken@1895
    54
{
slouken@3578
    55
    SDL_threadID id = SDL_ThreadID();
slouken@1895
    56
    int i;
slouken@3578
    57
    printf("Process %lu:  Cleaning up...\n", id == mainthread ? 0 : id);
slouken@2779
    58
    doterminate = 1;
slouken@1895
    59
    for (i = 0; i < 6; ++i)
slouken@2779
    60
        SDL_WaitThread(threads[i], NULL);
slouken@1895
    61
    SDL_DestroyMutex(mutex);
slouken@1895
    62
    exit(sig);
slouken@1895
    63
}
slouken@0
    64
slouken@1895
    65
int SDLCALL
slouken@1895
    66
Run(void *data)
slouken@1895
    67
{
slouken@1895
    68
    if (SDL_ThreadID() == mainthread)
slouken@1895
    69
        signal(SIGTERM, closemutex);
slouken@2779
    70
    while (!doterminate) {
slouken@3578
    71
        printf("Process %lu ready to work\n", SDL_ThreadID());
slouken@1895
    72
        if (SDL_mutexP(mutex) < 0) {
slouken@1895
    73
            fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
slouken@1895
    74
            exit(1);
slouken@1895
    75
        }
slouken@3578
    76
        printf("Process %lu, working!\n", SDL_ThreadID());
slouken@1895
    77
        SDL_Delay(1 * 1000);
slouken@3578
    78
        printf("Process %lu, done!\n", SDL_ThreadID());
slouken@1895
    79
        if (SDL_mutexV(mutex) < 0) {
slouken@1895
    80
            fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
slouken@1895
    81
            exit(1);
slouken@1895
    82
        }
slouken@1895
    83
        /* If this sleep isn't done, then threads may starve */
slouken@1895
    84
        SDL_Delay(10);
slouken@2779
    85
    }
slouken@2779
    86
    if (SDL_ThreadID() == mainthread && doterminate) {
slouken@3578
    87
        printf("Process %lu:  raising SIGTERM\n", SDL_ThreadID());
slouken@2779
    88
        raise(SIGTERM);
slouken@1895
    89
    }
slouken@1895
    90
    return (0);
slouken@1895
    91
}
slouken@0
    92
slouken@1895
    93
int
slouken@1895
    94
main(int argc, char *argv[])
slouken@1895
    95
{
slouken@1895
    96
    int i;
slouken@1895
    97
    int maxproc = 6;
slouken@0
    98
slouken@1895
    99
    /* Load the SDL library */
slouken@1895
   100
    if (SDL_Init(0) < 0) {
slouken@1895
   101
        fprintf(stderr, "%s\n", SDL_GetError());
slouken@1895
   102
        exit(1);
slouken@1895
   103
    }
slouken@1895
   104
    atexit(SDL_Quit_Wrapper);
slouken@1895
   105
slouken@1895
   106
    if ((mutex = SDL_CreateMutex()) == NULL) {
slouken@1895
   107
        fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
slouken@1895
   108
        exit(1);
slouken@1895
   109
    }
slouken@1895
   110
slouken@1895
   111
    mainthread = SDL_ThreadID();
slouken@3578
   112
    printf("Main thread: %lu\n", mainthread);
slouken@1895
   113
    atexit(printid);
slouken@1895
   114
    for (i = 0; i < maxproc; ++i) {
slouken@1895
   115
        if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL)
slouken@1895
   116
            fprintf(stderr, "Couldn't create thread!\n");
slouken@1895
   117
    }
slouken@1895
   118
    signal(SIGINT, terminate);
slouken@1895
   119
    Run(NULL);
slouken@1895
   120
slouken@1895
   121
    return (0);                 /* Never reached */
slouken@0
   122
}