test/testsem.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5106 d547877e355e
child 5969 3a041d215edc
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
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@5106
    59
slouken@5106
    60
    sem = SDL_CreateSemaphore(0);
slouken@5106
    61
    printf("Waiting 2 seconds on semaphore\n");
slouken@5106
    62
slouken@5106
    63
    start_ticks = SDL_GetTicks();
slouken@5106
    64
    SDL_SemWaitTimeout(sem, 2000);
slouken@5106
    65
    end_ticks = SDL_GetTicks();
slouken@5106
    66
slouken@5106
    67
    duration = end_ticks - start_ticks;
slouken@5106
    68
slouken@5106
    69
    /* Accept a little offset in the effective wait */
slouken@5106
    70
    if (duration > 1900 && duration < 2050)
slouken@5106
    71
        printf("Wait done.\n");
slouken@5106
    72
    else
slouken@5106
    73
        fprintf(stderr, "Wait took %d milliseconds\n", duration);
slouken@5106
    74
}
slouken@5106
    75
slouken@1895
    76
int
slouken@1895
    77
main(int argc, char **argv)
slouken@0
    78
{
slouken@1895
    79
    SDL_Thread *threads[NUM_THREADS];
slouken@1895
    80
    uintptr_t i;
slouken@1895
    81
    int init_sem;
slouken@0
    82
slouken@1895
    83
    if (argc < 2) {
slouken@1895
    84
        fprintf(stderr, "Usage: %s init_value\n", argv[0]);
slouken@1895
    85
        return (1);
slouken@1895
    86
    }
slouken@0
    87
slouken@1895
    88
    /* Load the SDL library */
slouken@1895
    89
    if (SDL_Init(0) < 0) {
slouken@1895
    90
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    91
        return (1);
slouken@1895
    92
    }
slouken@1895
    93
    signal(SIGTERM, killed);
slouken@1895
    94
    signal(SIGINT, killed);
slouken@0
    95
slouken@1895
    96
    init_sem = atoi(argv[1]);
slouken@1895
    97
    sem = SDL_CreateSemaphore(init_sem);
slouken@0
    98
slouken@1895
    99
    printf("Running %d threads, semaphore value = %d\n", NUM_THREADS,
slouken@1895
   100
           init_sem);
slouken@1895
   101
    /* Create all the threads */
slouken@1895
   102
    for (i = 0; i < NUM_THREADS; ++i) {
slouken@1895
   103
        threads[i] = SDL_CreateThread(ThreadFunc, (void *) i);
slouken@1895
   104
    }
slouken@0
   105
slouken@1895
   106
    /* Wait 10 seconds */
slouken@1895
   107
    SDL_Delay(10 * 1000);
slouken@1895
   108
slouken@1895
   109
    /* Wait for all threads to finish */
slouken@1895
   110
    printf("Waiting for threads to finish\n");
slouken@1895
   111
    alive = 0;
slouken@1895
   112
    for (i = 0; i < NUM_THREADS; ++i) {
slouken@1895
   113
        SDL_WaitThread(threads[i], NULL);
slouken@1895
   114
    }
slouken@1895
   115
    printf("Finished waiting for threads\n");
slouken@1895
   116
slouken@1895
   117
    SDL_DestroySemaphore(sem);
slouken@5106
   118
slouken@5106
   119
    TestWaitTimeout();
slouken@5106
   120
slouken@1895
   121
    SDL_Quit();
slouken@1895
   122
    return (0);
slouken@0
   123
}