test/testlock.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 /* Test the thread and mutex locking functions 
     3    Also exercises the system's signal/thread interaction
     4 */
     5 
     6 #include <signal.h>
     7 #include <stdio.h>
     8 
     9 #include "SDL.h"
    10 #include "SDL_mutex.h"
    11 #include "SDL_thread.h"
    12 
    13 static SDL_mutex *mutex = NULL;
    14 static Uint32 mainthread;
    15 static SDL_Thread *threads[6];
    16 static volatile int doterminate = 0;
    17 
    18 /*
    19  * SDL_Quit() shouldn't be used with atexit() directly because
    20  *  calling conventions may differ...
    21  */
    22 static void
    23 SDL_Quit_Wrapper (void)
    24 {
    25     SDL_Quit ();
    26 }
    27 
    28 void
    29 printid (void)
    30 {
    31     printf ("Process %u:  exiting\n", SDL_ThreadID ());
    32 }
    33 
    34 void
    35 terminate (int sig)
    36 {
    37     signal (SIGINT, terminate);
    38     doterminate = 1;
    39 }
    40 
    41 void
    42 closemutex (int sig)
    43 {
    44     Uint32 id = SDL_ThreadID ();
    45     int i;
    46     printf ("Process %u:  Cleaning up...\n", id == mainthread ? 0 : id);
    47     for (i = 0; i < 6; ++i)
    48         SDL_KillThread (threads[i]);
    49     SDL_DestroyMutex (mutex);
    50     exit (sig);
    51 }
    52 
    53 int SDLCALL
    54 Run (void *data)
    55 {
    56     if (SDL_ThreadID () == mainthread)
    57         signal (SIGTERM, closemutex);
    58     while (1) {
    59         printf ("Process %u ready to work\n", SDL_ThreadID ());
    60         if (SDL_mutexP (mutex) < 0) {
    61             fprintf (stderr, "Couldn't lock mutex: %s", SDL_GetError ());
    62             exit (1);
    63         }
    64         printf ("Process %u, working!\n", SDL_ThreadID ());
    65         SDL_Delay (1 * 1000);
    66         printf ("Process %u, done!\n", SDL_ThreadID ());
    67         if (SDL_mutexV (mutex) < 0) {
    68             fprintf (stderr, "Couldn't unlock mutex: %s", SDL_GetError ());
    69             exit (1);
    70         }
    71         /* If this sleep isn't done, then threads may starve */
    72         SDL_Delay (10);
    73         if (SDL_ThreadID () == mainthread && doterminate) {
    74             printf ("Process %u:  raising SIGTERM\n", SDL_ThreadID ());
    75             raise (SIGTERM);
    76         }
    77     }
    78     return (0);
    79 }
    80 
    81 int
    82 main (int argc, char *argv[])
    83 {
    84     int i;
    85     int maxproc = 6;
    86 
    87     /* Load the SDL library */
    88     if (SDL_Init (0) < 0) {
    89         fprintf (stderr, "%s\n", SDL_GetError ());
    90         exit (1);
    91     }
    92     atexit (SDL_Quit_Wrapper);
    93 
    94     if ((mutex = SDL_CreateMutex ()) == NULL) {
    95         fprintf (stderr, "Couldn't create mutex: %s\n", SDL_GetError ());
    96         exit (1);
    97     }
    98 
    99     mainthread = SDL_ThreadID ();
   100     printf ("Main thread: %u\n", mainthread);
   101     atexit (printid);
   102     for (i = 0; i < maxproc; ++i) {
   103         if ((threads[i] = SDL_CreateThread (Run, NULL)) == NULL)
   104             fprintf (stderr, "Couldn't create thread!\n");
   105     }
   106     signal (SIGINT, terminate);
   107     Run (NULL);
   108 
   109     return (0);                 /* Never reached */
   110 }