test/testlock.c
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 18 Jul 2008 19:23:13 +0000
branchgsoc2008_iphone
changeset 2377 e9a0bad0ea07
parent 1895 c121d94672cb
child 2779 4436464c4f51
permissions -rw-r--r--
Just renamed it...
     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 }