test/testlock.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
     1.1 --- a/test/testlock.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/test/testlock.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -19,84 +19,92 @@
     1.4   * SDL_Quit() shouldn't be used with atexit() directly because
     1.5   *  calling conventions may differ...
     1.6   */
     1.7 -static void SDL_Quit_Wrapper(void)
     1.8 +static void
     1.9 +SDL_Quit_Wrapper (void)
    1.10  {
    1.11 -	SDL_Quit();
    1.12 +    SDL_Quit ();
    1.13  }
    1.14  
    1.15 -void printid(void)
    1.16 +void
    1.17 +printid (void)
    1.18  {
    1.19 -	printf("Process %u:  exiting\n", SDL_ThreadID());
    1.20 -}
    1.21 -	
    1.22 -void terminate(int sig)
    1.23 -{
    1.24 -	signal(SIGINT, terminate);
    1.25 -	doterminate = 1;
    1.26 -}
    1.27 -void closemutex(int sig)
    1.28 -{
    1.29 -	Uint32 id = SDL_ThreadID();
    1.30 -	int i;
    1.31 -	printf("Process %u:  Cleaning up...\n", id == mainthread ? 0 : id);
    1.32 -	for ( i=0; i<6; ++i )
    1.33 -		SDL_KillThread(threads[i]);
    1.34 -	SDL_DestroyMutex(mutex);
    1.35 -	exit(sig);
    1.36 -}
    1.37 -int SDLCALL Run(void *data)
    1.38 -{
    1.39 -	if ( SDL_ThreadID() == mainthread )
    1.40 -		signal(SIGTERM, closemutex);
    1.41 -	while ( 1 ) {
    1.42 -		printf("Process %u ready to work\n", SDL_ThreadID());
    1.43 -		if ( SDL_mutexP(mutex) < 0 ) {
    1.44 -			fprintf(stderr, "Couldn't lock mutex: %s", SDL_GetError());
    1.45 -			exit(1);
    1.46 -		}
    1.47 -		printf("Process %u, working!\n", SDL_ThreadID());
    1.48 -		SDL_Delay(1*1000);
    1.49 -		printf("Process %u, done!\n", SDL_ThreadID());
    1.50 -		if ( SDL_mutexV(mutex) < 0 ) {
    1.51 -			fprintf(stderr, "Couldn't unlock mutex: %s", SDL_GetError());
    1.52 -			exit(1);
    1.53 -		}
    1.54 -		/* If this sleep isn't done, then threads may starve */
    1.55 -		SDL_Delay(10);
    1.56 -		if (SDL_ThreadID() == mainthread && doterminate) {
    1.57 -			printf("Process %u:  raising SIGTERM\n", SDL_ThreadID());
    1.58 -			raise(SIGTERM);
    1.59 -		}
    1.60 -	}
    1.61 -	return(0);
    1.62 +    printf ("Process %u:  exiting\n", SDL_ThreadID ());
    1.63  }
    1.64  
    1.65 -int main(int argc, char *argv[])
    1.66 +void
    1.67 +terminate (int sig)
    1.68  {
    1.69 -	int i;
    1.70 -	int maxproc = 6;
    1.71 +    signal (SIGINT, terminate);
    1.72 +    doterminate = 1;
    1.73 +}
    1.74  
    1.75 -	/* Load the SDL library */
    1.76 -	if ( SDL_Init(0) < 0 ) {
    1.77 -		fprintf(stderr, "%s\n", SDL_GetError());
    1.78 -		exit(1);
    1.79 -	}
    1.80 -	atexit(SDL_Quit_Wrapper);
    1.81 +void
    1.82 +closemutex (int sig)
    1.83 +{
    1.84 +    Uint32 id = SDL_ThreadID ();
    1.85 +    int i;
    1.86 +    printf ("Process %u:  Cleaning up...\n", id == mainthread ? 0 : id);
    1.87 +    for (i = 0; i < 6; ++i)
    1.88 +        SDL_KillThread (threads[i]);
    1.89 +    SDL_DestroyMutex (mutex);
    1.90 +    exit (sig);
    1.91 +}
    1.92  
    1.93 -	if ( (mutex=SDL_CreateMutex()) == NULL ) {
    1.94 -		fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
    1.95 -		exit(1);
    1.96 -	}
    1.97 +int SDLCALL
    1.98 +Run (void *data)
    1.99 +{
   1.100 +    if (SDL_ThreadID () == mainthread)
   1.101 +        signal (SIGTERM, closemutex);
   1.102 +    while (1) {
   1.103 +        printf ("Process %u ready to work\n", SDL_ThreadID ());
   1.104 +        if (SDL_mutexP (mutex) < 0) {
   1.105 +            fprintf (stderr, "Couldn't lock mutex: %s", SDL_GetError ());
   1.106 +            exit (1);
   1.107 +        }
   1.108 +        printf ("Process %u, working!\n", SDL_ThreadID ());
   1.109 +        SDL_Delay (1 * 1000);
   1.110 +        printf ("Process %u, done!\n", SDL_ThreadID ());
   1.111 +        if (SDL_mutexV (mutex) < 0) {
   1.112 +            fprintf (stderr, "Couldn't unlock mutex: %s", SDL_GetError ());
   1.113 +            exit (1);
   1.114 +        }
   1.115 +        /* If this sleep isn't done, then threads may starve */
   1.116 +        SDL_Delay (10);
   1.117 +        if (SDL_ThreadID () == mainthread && doterminate) {
   1.118 +            printf ("Process %u:  raising SIGTERM\n", SDL_ThreadID ());
   1.119 +            raise (SIGTERM);
   1.120 +        }
   1.121 +    }
   1.122 +    return (0);
   1.123 +}
   1.124  
   1.125 -	mainthread = SDL_ThreadID();
   1.126 -	printf("Main thread: %u\n", mainthread);
   1.127 -	atexit(printid);
   1.128 -	for ( i=0; i<maxproc; ++i ) {
   1.129 -		if ( (threads[i]=SDL_CreateThread(Run, NULL)) == NULL )
   1.130 -			fprintf(stderr, "Couldn't create thread!\n");
   1.131 -	}
   1.132 -	signal(SIGINT, terminate);
   1.133 -	Run(NULL);
   1.134 +int
   1.135 +main (int argc, char *argv[])
   1.136 +{
   1.137 +    int i;
   1.138 +    int maxproc = 6;
   1.139  
   1.140 -	return(0);	/* Never reached */
   1.141 +    /* Load the SDL library */
   1.142 +    if (SDL_Init (0) < 0) {
   1.143 +        fprintf (stderr, "%s\n", SDL_GetError ());
   1.144 +        exit (1);
   1.145 +    }
   1.146 +    atexit (SDL_Quit_Wrapper);
   1.147 +
   1.148 +    if ((mutex = SDL_CreateMutex ()) == NULL) {
   1.149 +        fprintf (stderr, "Couldn't create mutex: %s\n", SDL_GetError ());
   1.150 +        exit (1);
   1.151 +    }
   1.152 +
   1.153 +    mainthread = SDL_ThreadID ();
   1.154 +    printf ("Main thread: %u\n", mainthread);
   1.155 +    atexit (printid);
   1.156 +    for (i = 0; i < maxproc; ++i) {
   1.157 +        if ((threads[i] = SDL_CreateThread (Run, NULL)) == NULL)
   1.158 +            fprintf (stderr, "Couldn't create thread!\n");
   1.159 +    }
   1.160 +    signal (SIGINT, terminate);
   1.161 +    Run (NULL);
   1.162 +
   1.163 +    return (0);                 /* Never reached */
   1.164  }