test/testlock.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Jan 2009 04:05:28 +0000
changeset 3017 3272431eeee2
parent 2779 4436464c4f51
child 3578 0d1b16ee0bca
permissions -rw-r--r--
Added testresample.c
     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     doterminate = 1;
    48     for (i = 0; i < 6; ++i)
    49         SDL_WaitThread(threads[i], NULL);
    50     SDL_DestroyMutex(mutex);
    51     exit(sig);
    52 }
    53 
    54 int SDLCALL
    55 Run(void *data)
    56 {
    57     if (SDL_ThreadID() == mainthread)
    58         signal(SIGTERM, closemutex);
    59     while (!doterminate) {
    60         printf("Process %u ready to work\n", SDL_ThreadID());
    61         if (SDL_mutexP(mutex) < 0) {
    62             fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
    63             exit(1);
    64         }
    65         printf("Process %u, working!\n", SDL_ThreadID());
    66         SDL_Delay(1 * 1000);
    67         printf("Process %u, done!\n", SDL_ThreadID());
    68         if (SDL_mutexV(mutex) < 0) {
    69             fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
    70             exit(1);
    71         }
    72         /* If this sleep isn't done, then threads may starve */
    73         SDL_Delay(10);
    74     }
    75     if (SDL_ThreadID() == mainthread && doterminate) {
    76         printf("Process %u:  raising SIGTERM\n", SDL_ThreadID());
    77         raise(SIGTERM);
    78     }
    79     return (0);
    80 }
    81 
    82 int
    83 main(int argc, char *argv[])
    84 {
    85     int i;
    86     int maxproc = 6;
    87 
    88     /* Load the SDL library */
    89     if (SDL_Init(0) < 0) {
    90         fprintf(stderr, "%s\n", SDL_GetError());
    91         exit(1);
    92     }
    93     atexit(SDL_Quit_Wrapper);
    94 
    95     if ((mutex = SDL_CreateMutex()) == NULL) {
    96         fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
    97         exit(1);
    98     }
    99 
   100     mainthread = SDL_ThreadID();
   101     printf("Main thread: %u\n", mainthread);
   102     atexit(printid);
   103     for (i = 0; i < maxproc; ++i) {
   104         if ((threads[i] = SDL_CreateThread(Run, NULL)) == NULL)
   105             fprintf(stderr, "Couldn't create thread!\n");
   106     }
   107     signal(SIGINT, terminate);
   108     Run(NULL);
   109 
   110     return (0);                 /* Never reached */
   111 }