test/testsem.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 27 Jan 2011 00:34:12 -0800
changeset 5106 d547877e355e
parent 1895 c121d94672cb
child 5535 96594ac5fd1a
permissions -rw-r--r--
Colin Leroy 2011-01-26 04:24:20 PST

the pthread implementation of SDL_SemWaitTimeout() uses busy waiting, while
pthread's sem_timedwait() does work. Attached are patches that make use of it
slouken@0
     1
slouken@0
     2
/* Simple test of the SDL semaphore code */
slouken@0
     3
slouken@0
     4
#include <stdio.h>
slouken@0
     5
#include <stdlib.h>
slouken@0
     6
#include <signal.h>
slouken@0
     7
slouken@0
     8
#include "SDL.h"
slouken@0
     9
#include "SDL_thread.h"
slouken@0
    10
slouken@0
    11
#define NUM_THREADS 10
slouken@0
    12
slouken@0
    13
static SDL_sem *sem;
slouken@0
    14
int alive = 1;
slouken@0
    15
slouken@1895
    16
int SDLCALL
slouken@1895
    17
ThreadFunc(void *data)
slouken@0
    18
{
slouken@1895
    19
    int threadnum = (int) (uintptr_t) data;
slouken@1895
    20
    while (alive) {
slouken@1895
    21
        SDL_SemWait(sem);
slouken@1895
    22
        fprintf(stderr,
slouken@1895
    23
                "Thread number %d has got the semaphore (value = %d)!\n",
slouken@1895
    24
                threadnum, SDL_SemValue(sem));
slouken@1895
    25
        SDL_Delay(200);
slouken@1895
    26
        SDL_SemPost(sem);
slouken@1895
    27
        fprintf(stderr,
slouken@1895
    28
                "Thread number %d has released the semaphore (value = %d)!\n",
slouken@1895
    29
                threadnum, SDL_SemValue(sem));
slouken@1895
    30
        SDL_Delay(1);           /* For the scheduler */
slouken@1895
    31
    }
slouken@1895
    32
    printf("Thread number %d exiting.\n", threadnum);
slouken@1895
    33
    return 0;
slouken@0
    34
}
slouken@0
    35
slouken@1895
    36
static void
slouken@1895
    37
killed(int sig)
slouken@0
    38
{
slouken@1895
    39
    alive = 0;
slouken@0
    40
}
slouken@0
    41
slouken@5106
    42
static void
slouken@5106
    43
TestWaitTimeout(void)
slouken@5106
    44
{
slouken@5106
    45
    Uint32 start_ticks;
slouken@5106
    46
    Uint32 end_ticks;
slouken@5106
    47
    Uint32 duration;
slouken@5106
    48
slouken@5106
    49
    sem = SDL_CreateSemaphore(0);
slouken@5106
    50
    printf("Waiting 2 seconds on semaphore\n");
slouken@5106
    51
slouken@5106
    52
    start_ticks = SDL_GetTicks();
slouken@5106
    53
    SDL_SemWaitTimeout(sem, 2000);
slouken@5106
    54
    end_ticks = SDL_GetTicks();
slouken@5106
    55
slouken@5106
    56
    duration = end_ticks - start_ticks;
slouken@5106
    57
slouken@5106
    58
    /* Accept a little offset in the effective wait */
slouken@5106
    59
    if (duration > 1900 && duration < 2050)
slouken@5106
    60
        printf("Wait done.\n");
slouken@5106
    61
    else
slouken@5106
    62
        fprintf(stderr, "Wait took %d milliseconds\n", duration);
slouken@5106
    63
}
slouken@5106
    64
slouken@1895
    65
int
slouken@1895
    66
main(int argc, char **argv)
slouken@0
    67
{
slouken@1895
    68
    SDL_Thread *threads[NUM_THREADS];
slouken@1895
    69
    uintptr_t i;
slouken@1895
    70
    int init_sem;
slouken@0
    71
slouken@1895
    72
    if (argc < 2) {
slouken@1895
    73
        fprintf(stderr, "Usage: %s init_value\n", argv[0]);
slouken@1895
    74
        return (1);
slouken@1895
    75
    }
slouken@0
    76
slouken@1895
    77
    /* Load the SDL library */
slouken@1895
    78
    if (SDL_Init(0) < 0) {
slouken@1895
    79
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
    80
        return (1);
slouken@1895
    81
    }
slouken@1895
    82
    signal(SIGTERM, killed);
slouken@1895
    83
    signal(SIGINT, killed);
slouken@0
    84
slouken@1895
    85
    init_sem = atoi(argv[1]);
slouken@1895
    86
    sem = SDL_CreateSemaphore(init_sem);
slouken@0
    87
slouken@1895
    88
    printf("Running %d threads, semaphore value = %d\n", NUM_THREADS,
slouken@1895
    89
           init_sem);
slouken@1895
    90
    /* Create all the threads */
slouken@1895
    91
    for (i = 0; i < NUM_THREADS; ++i) {
slouken@1895
    92
        threads[i] = SDL_CreateThread(ThreadFunc, (void *) i);
slouken@1895
    93
    }
slouken@0
    94
slouken@1895
    95
    /* Wait 10 seconds */
slouken@1895
    96
    SDL_Delay(10 * 1000);
slouken@1895
    97
slouken@1895
    98
    /* Wait for all threads to finish */
slouken@1895
    99
    printf("Waiting for threads to finish\n");
slouken@1895
   100
    alive = 0;
slouken@1895
   101
    for (i = 0; i < NUM_THREADS; ++i) {
slouken@1895
   102
        SDL_WaitThread(threads[i], NULL);
slouken@1895
   103
    }
slouken@1895
   104
    printf("Finished waiting for threads\n");
slouken@1895
   105
slouken@1895
   106
    SDL_DestroySemaphore(sem);
slouken@5106
   107
slouken@5106
   108
    TestWaitTimeout();
slouken@5106
   109
slouken@1895
   110
    SDL_Quit();
slouken@1895
   111
    return (0);
slouken@0
   112
}