test/testsem.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
     1.1 --- a/test/testsem.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/test/testsem.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -13,66 +13,74 @@
     1.4  static SDL_sem *sem;
     1.5  int alive = 1;
     1.6  
     1.7 -int SDLCALL ThreadFunc(void *data)
     1.8 +int SDLCALL
     1.9 +ThreadFunc (void *data)
    1.10  {
    1.11 -	int threadnum = (int)(uintptr_t)data;
    1.12 -	while ( alive ) {
    1.13 -		SDL_SemWait(sem);
    1.14 -		fprintf(stderr, "Thread number %d has got the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
    1.15 -		SDL_Delay(200);
    1.16 -		SDL_SemPost(sem);
    1.17 -		fprintf(stderr, "Thread number %d has released the semaphore (value = %d)!\n", threadnum, SDL_SemValue(sem));
    1.18 -		SDL_Delay(1); /* For the scheduler */
    1.19 -	}
    1.20 -	printf("Thread number %d exiting.\n", threadnum);
    1.21 -	return 0;
    1.22 +    int threadnum = (int) (uintptr_t) data;
    1.23 +    while (alive) {
    1.24 +        SDL_SemWait (sem);
    1.25 +        fprintf (stderr,
    1.26 +                 "Thread number %d has got the semaphore (value = %d)!\n",
    1.27 +                 threadnum, SDL_SemValue (sem));
    1.28 +        SDL_Delay (200);
    1.29 +        SDL_SemPost (sem);
    1.30 +        fprintf (stderr,
    1.31 +                 "Thread number %d has released the semaphore (value = %d)!\n",
    1.32 +                 threadnum, SDL_SemValue (sem));
    1.33 +        SDL_Delay (1);          /* For the scheduler */
    1.34 +    }
    1.35 +    printf ("Thread number %d exiting.\n", threadnum);
    1.36 +    return 0;
    1.37  }
    1.38  
    1.39 -static void killed(int sig)
    1.40 +static void
    1.41 +killed (int sig)
    1.42  {
    1.43 -	alive = 0;
    1.44 +    alive = 0;
    1.45  }
    1.46  
    1.47 -int main(int argc, char **argv)
    1.48 +int
    1.49 +main (int argc, char **argv)
    1.50  {
    1.51 -	SDL_Thread *threads[NUM_THREADS];
    1.52 -	uintptr_t i;
    1.53 -	int init_sem;
    1.54 +    SDL_Thread *threads[NUM_THREADS];
    1.55 +    uintptr_t i;
    1.56 +    int init_sem;
    1.57  
    1.58 -	if(argc < 2) {
    1.59 -		fprintf(stderr,"Usage: %s init_value\n", argv[0]);
    1.60 -		return(1);
    1.61 -	}
    1.62 +    if (argc < 2) {
    1.63 +        fprintf (stderr, "Usage: %s init_value\n", argv[0]);
    1.64 +        return (1);
    1.65 +    }
    1.66  
    1.67 -	/* Load the SDL library */
    1.68 -	if ( SDL_Init(0) < 0 ) {
    1.69 -		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    1.70 -		return(1);
    1.71 -	}
    1.72 -	signal(SIGTERM, killed);
    1.73 -	signal(SIGINT, killed);
    1.74 -	
    1.75 -	init_sem = atoi(argv[1]);
    1.76 -	sem = SDL_CreateSemaphore(init_sem);
    1.77 -	
    1.78 -	printf("Running %d threads, semaphore value = %d\n", NUM_THREADS, init_sem);
    1.79 -	/* Create all the threads */
    1.80 -	for( i = 0; i < NUM_THREADS; ++i ) {
    1.81 -		threads[i] = SDL_CreateThread(ThreadFunc, (void*)i);
    1.82 -	}
    1.83 +    /* Load the SDL library */
    1.84 +    if (SDL_Init (0) < 0) {
    1.85 +        fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
    1.86 +        return (1);
    1.87 +    }
    1.88 +    signal (SIGTERM, killed);
    1.89 +    signal (SIGINT, killed);
    1.90  
    1.91 -	/* Wait 10 seconds */
    1.92 -	SDL_Delay(10 * 1000);
    1.93 +    init_sem = atoi (argv[1]);
    1.94 +    sem = SDL_CreateSemaphore (init_sem);
    1.95  
    1.96 -	/* Wait for all threads to finish */
    1.97 -	printf("Waiting for threads to finish\n");
    1.98 -	alive = 0;
    1.99 -	for( i = 0; i < NUM_THREADS; ++i ) {
   1.100 -		SDL_WaitThread(threads[i], NULL);
   1.101 -	}
   1.102 -	printf("Finished waiting for threads\n");
   1.103 +    printf ("Running %d threads, semaphore value = %d\n", NUM_THREADS,
   1.104 +            init_sem);
   1.105 +    /* Create all the threads */
   1.106 +    for (i = 0; i < NUM_THREADS; ++i) {
   1.107 +        threads[i] = SDL_CreateThread (ThreadFunc, (void *) i);
   1.108 +    }
   1.109  
   1.110 -	SDL_DestroySemaphore(sem);
   1.111 -	SDL_Quit();
   1.112 -	return(0);
   1.113 +    /* Wait 10 seconds */
   1.114 +    SDL_Delay (10 * 1000);
   1.115 +
   1.116 +    /* Wait for all threads to finish */
   1.117 +    printf ("Waiting for threads to finish\n");
   1.118 +    alive = 0;
   1.119 +    for (i = 0; i < NUM_THREADS; ++i) {
   1.120 +        SDL_WaitThread (threads[i], NULL);
   1.121 +    }
   1.122 +    printf ("Finished waiting for threads\n");
   1.123 +
   1.124 +    SDL_DestroySemaphore (sem);
   1.125 +    SDL_Quit ();
   1.126 +    return (0);
   1.127  }