test/testsem.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
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@1662
    16
int SDLCALL
slouken@1662
    17
ThreadFunc (void *data)
slouken@0
    18
{
slouken@1662
    19
    int threadnum = (int) (uintptr_t) data;
slouken@1662
    20
    while (alive) {
slouken@1662
    21
        SDL_SemWait (sem);
slouken@1662
    22
        fprintf (stderr,
slouken@1662
    23
                 "Thread number %d has got the semaphore (value = %d)!\n",
slouken@1662
    24
                 threadnum, SDL_SemValue (sem));
slouken@1662
    25
        SDL_Delay (200);
slouken@1662
    26
        SDL_SemPost (sem);
slouken@1662
    27
        fprintf (stderr,
slouken@1662
    28
                 "Thread number %d has released the semaphore (value = %d)!\n",
slouken@1662
    29
                 threadnum, SDL_SemValue (sem));
slouken@1662
    30
        SDL_Delay (1);          /* For the scheduler */
slouken@1662
    31
    }
slouken@1662
    32
    printf ("Thread number %d exiting.\n", threadnum);
slouken@1662
    33
    return 0;
slouken@0
    34
}
slouken@0
    35
slouken@1662
    36
static void
slouken@1662
    37
killed (int sig)
slouken@0
    38
{
slouken@1662
    39
    alive = 0;
slouken@0
    40
}
slouken@0
    41
slouken@1662
    42
int
slouken@1662
    43
main (int argc, char **argv)
slouken@0
    44
{
slouken@1662
    45
    SDL_Thread *threads[NUM_THREADS];
slouken@1662
    46
    uintptr_t i;
slouken@1662
    47
    int init_sem;
slouken@0
    48
slouken@1662
    49
    if (argc < 2) {
slouken@1662
    50
        fprintf (stderr, "Usage: %s init_value\n", argv[0]);
slouken@1662
    51
        return (1);
slouken@1662
    52
    }
slouken@0
    53
slouken@1662
    54
    /* Load the SDL library */
slouken@1662
    55
    if (SDL_Init (0) < 0) {
slouken@1662
    56
        fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
slouken@1662
    57
        return (1);
slouken@1662
    58
    }
slouken@1662
    59
    signal (SIGTERM, killed);
slouken@1662
    60
    signal (SIGINT, killed);
slouken@0
    61
slouken@1662
    62
    init_sem = atoi (argv[1]);
slouken@1662
    63
    sem = SDL_CreateSemaphore (init_sem);
slouken@0
    64
slouken@1662
    65
    printf ("Running %d threads, semaphore value = %d\n", NUM_THREADS,
slouken@1662
    66
            init_sem);
slouken@1662
    67
    /* Create all the threads */
slouken@1662
    68
    for (i = 0; i < NUM_THREADS; ++i) {
slouken@1662
    69
        threads[i] = SDL_CreateThread (ThreadFunc, (void *) i);
slouken@1662
    70
    }
slouken@0
    71
slouken@1662
    72
    /* Wait 10 seconds */
slouken@1662
    73
    SDL_Delay (10 * 1000);
slouken@1662
    74
slouken@1662
    75
    /* Wait for all threads to finish */
slouken@1662
    76
    printf ("Waiting for threads to finish\n");
slouken@1662
    77
    alive = 0;
slouken@1662
    78
    for (i = 0; i < NUM_THREADS; ++i) {
slouken@1662
    79
        SDL_WaitThread (threads[i], NULL);
slouken@1662
    80
    }
slouken@1662
    81
    printf ("Finished waiting for threads\n");
slouken@1662
    82
slouken@1662
    83
    SDL_DestroySemaphore (sem);
slouken@1662
    84
    SDL_Quit ();
slouken@1662
    85
    return (0);
slouken@0
    86
}