test/testatomic.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 15 Jan 2011 12:41:59 -0800
changeset 5003 3a95a2b93eb3
parent 3338 9de326b3099c
child 5004 0c72ae7b7cb2
permissions -rw-r--r--
Updated the atomic API for better use cases
slouken@3338
     1
#include <stdio.h>
bob@3180
     2
#include "SDL.h"
bob@3180
     3
bob@3201
     4
/*
bob@3202
     5
  Absolutely basic tests just to see if we get the expected value
bob@3202
     6
  after calling each function.
bob@3201
     7
*/
bob@3201
     8
slouken@3338
     9
char *
bob@3202
    10
tf(SDL_bool tf)
bob@3202
    11
{
slouken@5003
    12
    static char *t = "TRUE";
slouken@5003
    13
    static char *f = "FALSE";
bob@3202
    14
slouken@5003
    15
    if (tf)
slouken@5003
    16
    {
slouken@5003
    17
       return t;
slouken@5003
    18
    }
bob@3202
    19
slouken@5003
    20
    return f;
bob@3202
    21
}
slouken@3338
    22
bob@3180
    23
int
bob@3299
    24
main(int argc, char *argv[])
bob@3180
    25
{
slouken@5003
    26
    int value;
slouken@5003
    27
    SDL_SpinLock lock = 0;
bob@3180
    28
slouken@5003
    29
    SDL_bool tfret = SDL_FALSE;
bob@3180
    30
slouken@5003
    31
    printf("\nspin lock---------------------------------------\n\n");
bob@3180
    32
slouken@5003
    33
    SDL_AtomicLock(&lock);
slouken@5003
    34
    printf("AtomicLock                   lock=%d\n", lock);
slouken@5003
    35
    SDL_AtomicUnlock(&lock);
slouken@5003
    36
    printf("AtomicUnlock                 lock=%d\n", lock);
bob@3261
    37
slouken@5003
    38
    printf("\natomic -----------------------------------------\n\n");
bob@3202
    39
slouken@5003
    40
    SDL_atomic_t v;
slouken@5003
    41
     
slouken@5003
    42
    SDL_AtomicSet(&v, 0);
slouken@5003
    43
    tfret = SDL_AtomicSet(&v, 10) == 0;
slouken@5003
    44
    printf("AtomicSet(10)        tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
slouken@5003
    45
    tfret = SDL_AtomicAdd(&v, 10) == 10;
slouken@5003
    46
    printf("AtomicAdd(10)        tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
slouken@3186
    47
slouken@5003
    48
    SDL_AtomicSet(&v, 0);
slouken@5003
    49
    SDL_AtomicIncRef(&v);
slouken@5003
    50
    tfret = (SDL_AtomicGet(&v) == 1);
slouken@5003
    51
    printf("AtomicIncRef()       tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
slouken@5003
    52
    SDL_AtomicIncRef(&v);
slouken@5003
    53
    tfret = (SDL_AtomicGet(&v) == 2);
slouken@5003
    54
    printf("AtomicIncRef()       tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
slouken@5003
    55
    tfret = (SDL_AtomicDecRef(&v) == SDL_FALSE);
slouken@5003
    56
    printf("AtomicDecRef()       tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
slouken@5003
    57
    tfret = (SDL_AtomicDecRef(&v) == SDL_TRUE);
slouken@5003
    58
    printf("AtomicDecRef()       tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
bob@3202
    59
slouken@5003
    60
    SDL_AtomicSet(&v, 10);
slouken@5003
    61
    tfret = (SDL_AtomicCAS(&v, 0, 20) != 0);
slouken@5003
    62
    printf("AtomicCAS()          tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
slouken@5003
    63
    value = SDL_AtomicGet(&v);
slouken@5003
    64
    tfret = (SDL_AtomicCAS(&v, value, 20) == value);
slouken@5003
    65
    printf("AtomicCAS()          tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
bob@3202
    66
slouken@5003
    67
    return 0;
slouken@5003
    68
}