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