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