test/testsem.c
author Patrice Mandin <patmandin@gmail.com>
Fri, 05 Jan 2007 19:12:50 +0000
changeset 2082 e6cd882e3ac0
parent 1895 c121d94672cb
child 5106 d547877e355e
permissions -rw-r--r--
Alpha sort the test list, add missing ones, remove removed ones
slouken@0
     1
slouken@0
     2
/* Simple test of the SDL semaphore code */
slouken@0
     3
slouken@0
     4
#include <stdio.h>
slouken@0
     5
#include <stdlib.h>
slouken@0
     6
#include <signal.h>
slouken@0
     7
slouken@0
     8
#include "SDL.h"
slouken@0
     9
#include "SDL_thread.h"
slouken@0
    10
slouken@0
    11
#define NUM_THREADS 10
slouken@0
    12
slouken@0
    13
static SDL_sem *sem;
slouken@0
    14
int alive = 1;
slouken@0
    15
slouken@1895
    16
int SDLCALL
slouken@1895
    17
ThreadFunc(void *data)
slouken@0
    18
{
slouken@1895
    19
    int threadnum = (int) (uintptr_t) data;
slouken@1895
    20
    while (alive) {
slouken@1895
    21
        SDL_SemWait(sem);
slouken@1895
    22
        fprintf(stderr,
slouken@1895
    23
                "Thread number %d has got the semaphore (value = %d)!\n",
slouken@1895
    24
                threadnum, SDL_SemValue(sem));
slouken@1895
    25
        SDL_Delay(200);
slouken@1895
    26
        SDL_SemPost(sem);
slouken@1895
    27
        fprintf(stderr,
slouken@1895
    28
                "Thread number %d has released the semaphore (value = %d)!\n",
slouken@1895
    29
                threadnum, SDL_SemValue(sem));
slouken@1895
    30
        SDL_Delay(1);           /* For the scheduler */
slouken@1895
    31
    }
slouken@1895
    32
    printf("Thread number %d exiting.\n", threadnum);
slouken@1895
    33
    return 0;
slouken@0
    34
}
slouken@0
    35
slouken@1895
    36
static void
slouken@1895
    37
killed(int sig)
slouken@0
    38
{
slouken@1895
    39
    alive = 0;
slouken@0
    40
}
slouken@0
    41
slouken@1895
    42
int
slouken@1895
    43
main(int argc, char **argv)
slouken@0
    44
{
slouken@1895
    45
    SDL_Thread *threads[NUM_THREADS];
slouken@1895
    46
    uintptr_t i;
slouken@1895
    47
    int init_sem;
slouken@0
    48
slouken@1895
    49
    if (argc < 2) {
slouken@1895
    50
        fprintf(stderr, "Usage: %s init_value\n", argv[0]);
slouken@1895
    51
        return (1);
slouken@1895
    52
    }
slouken@0
    53
slouken@1895
    54
    /* Load the SDL library */
slouken@1895
    55
    if (SDL_Init(0) < 0) {
slouken@1895
    56
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    57
        return (1);
slouken@1895
    58
    }
slouken@1895
    59
    signal(SIGTERM, killed);
slouken@1895
    60
    signal(SIGINT, killed);
slouken@0
    61
slouken@1895
    62
    init_sem = atoi(argv[1]);
slouken@1895
    63
    sem = SDL_CreateSemaphore(init_sem);
slouken@0
    64
slouken@1895
    65
    printf("Running %d threads, semaphore value = %d\n", NUM_THREADS,
slouken@1895
    66
           init_sem);
slouken@1895
    67
    /* Create all the threads */
slouken@1895
    68
    for (i = 0; i < NUM_THREADS; ++i) {
slouken@1895
    69
        threads[i] = SDL_CreateThread(ThreadFunc, (void *) i);
slouken@1895
    70
    }
slouken@0
    71
slouken@1895
    72
    /* Wait 10 seconds */
slouken@1895
    73
    SDL_Delay(10 * 1000);
slouken@1895
    74
slouken@1895
    75
    /* Wait for all threads to finish */
slouken@1895
    76
    printf("Waiting for threads to finish\n");
slouken@1895
    77
    alive = 0;
slouken@1895
    78
    for (i = 0; i < NUM_THREADS; ++i) {
slouken@1895
    79
        SDL_WaitThread(threads[i], NULL);
slouken@1895
    80
    }
slouken@1895
    81
    printf("Finished waiting for threads\n");
slouken@1895
    82
slouken@1895
    83
    SDL_DestroySemaphore(sem);
slouken@1895
    84
    SDL_Quit();
slouken@1895
    85
    return (0);
slouken@0
    86
}