test/testsem.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 03 Jun 2013 19:24:18 -0700
changeset 7268 b399d0441810
parent 7191 75360622e65f
child 7517 965d57022c01
permissions -rw-r--r--
Add missing VS2012 test projects; update VS2010 and VS2012 solutions; update keybord suite for VS compiler warnings
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Simple test of the SDL semaphore code */
    14 
    15 #include <stdio.h>
    16 #include <stdlib.h>
    17 #include <signal.h>
    18 
    19 #include "SDL.h"
    20 #include "SDL_thread.h"
    21 
    22 #define NUM_THREADS 10
    23 
    24 static SDL_sem *sem;
    25 int alive = 1;
    26 
    27 int SDLCALL
    28 ThreadFunc(void *data)
    29 {
    30     int threadnum = (int) (uintptr_t) data;
    31     while (alive) {
    32         SDL_SemWait(sem);
    33         fprintf(stderr,
    34                 "Thread number %d has got the semaphore (value = %d)!\n",
    35                 threadnum, SDL_SemValue(sem));
    36         SDL_Delay(200);
    37         SDL_SemPost(sem);
    38         fprintf(stderr,
    39                 "Thread number %d has released the semaphore (value = %d)!\n",
    40                 threadnum, SDL_SemValue(sem));
    41         SDL_Delay(1);           /* For the scheduler */
    42     }
    43     printf("Thread number %d exiting.\n", threadnum);
    44     return 0;
    45 }
    46 
    47 static void
    48 killed(int sig)
    49 {
    50     alive = 0;
    51 }
    52 
    53 static void
    54 TestWaitTimeout(void)
    55 {
    56     Uint32 start_ticks;
    57     Uint32 end_ticks;
    58     Uint32 duration;
    59     int retval;
    60 
    61     sem = SDL_CreateSemaphore(0);
    62     printf("Waiting 2 seconds on semaphore\n");
    63 
    64     start_ticks = SDL_GetTicks();
    65     retval = SDL_SemWaitTimeout(sem, 2000);
    66     end_ticks = SDL_GetTicks();
    67 
    68     duration = end_ticks - start_ticks;
    69 
    70     /* Accept a little offset in the effective wait */
    71     if (duration > 1900 && duration < 2050)
    72         printf("Wait done.\n");
    73     else
    74         fprintf(stderr, "Wait took %d milliseconds\n", duration);
    75 
    76     /* Check to make sure the return value indicates timed out */
    77     if (retval != SDL_MUTEX_TIMEDOUT)
    78         fprintf(stderr, "SDL_SemWaitTimeout returned: %d; expected: %d\n", retval, SDL_MUTEX_TIMEDOUT);
    79 }
    80 
    81 int
    82 main(int argc, char **argv)
    83 {
    84     SDL_Thread *threads[NUM_THREADS];
    85     uintptr_t i;
    86     int init_sem;
    87 
    88     if (argc < 2) {
    89         fprintf(stderr, "Usage: %s init_value\n", argv[0]);
    90         return (1);
    91     }
    92 
    93     /* Load the SDL library */
    94     if (SDL_Init(0) < 0) {
    95         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    96         return (1);
    97     }
    98     signal(SIGTERM, killed);
    99     signal(SIGINT, killed);
   100 
   101     init_sem = atoi(argv[1]);
   102     sem = SDL_CreateSemaphore(init_sem);
   103 
   104     printf("Running %d threads, semaphore value = %d\n", NUM_THREADS,
   105            init_sem);
   106     /* Create all the threads */
   107     for (i = 0; i < NUM_THREADS; ++i) {
   108         char name[64];
   109         SDL_snprintf(name, sizeof (name), "Thread%u", (unsigned int) i);
   110         threads[i] = SDL_CreateThread(ThreadFunc, name, (void *) i);
   111     }
   112 
   113     /* Wait 10 seconds */
   114     SDL_Delay(10 * 1000);
   115 
   116     /* Wait for all threads to finish */
   117     printf("Waiting for threads to finish\n");
   118     alive = 0;
   119     for (i = 0; i < NUM_THREADS; ++i) {
   120         SDL_WaitThread(threads[i], NULL);
   121     }
   122     printf("Finished waiting for threads\n");
   123 
   124     SDL_DestroySemaphore(sem);
   125 
   126     TestWaitTimeout();
   127 
   128     SDL_Quit();
   129     return (0);
   130 }