test/testatomic.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 12 Aug 2016 19:59:00 -0400
changeset 10266 c09f06c4e8c8
parent 10003 d91a2c45825e
permissions -rw-r--r--
emscripten: send fake mouse events for touches, like other targets do. (This really should be handled at the higher level and not in the individual targets, but this fixes the immediate bug.)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
     1
/*
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9922
diff changeset
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
     3
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
     4
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
     6
  arising from the use of this software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
     7
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
    10
  freely.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5116
diff changeset
    11
*/
3338
9de326b3099c Fixed bug #817
Sam Lantinga <slouken@libsdl.org>
parents: 3327
diff changeset
    12
#include <stdio.h>
5006
8e8876e4aec6 Include windows.h in SDL_atomic.h by default, but don't include the atomic API in SDL.h
Sam Lantinga <slouken@libsdl.org>
parents: 5004
diff changeset
    13
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    14
#include "SDL.h"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    15
3201
c297230efc75 Disabling 64 bit atomics operations until I figure out why they do not link.
Bob Pendleton <bob@pendleton.com>
parents: 3199
diff changeset
    16
/*
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
    17
  Absolutely basic tests just to see if we get the expected value
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
    18
  after calling each function.
3201
c297230efc75 Disabling 64 bit atomics operations until I figure out why they do not link.
Bob Pendleton <bob@pendleton.com>
parents: 3199
diff changeset
    19
*/
c297230efc75 Disabling 64 bit atomics operations until I figure out why they do not link.
Bob Pendleton <bob@pendleton.com>
parents: 3199
diff changeset
    20
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    21
static
3338
9de326b3099c Fixed bug #817
Sam Lantinga <slouken@libsdl.org>
parents: 3327
diff changeset
    22
char *
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
    23
tf(SDL_bool tf)
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
    24
{
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    25
    static char *t = "TRUE";
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    26
    static char *f = "FALSE";
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
    27
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    28
    if (tf)
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    29
    {
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    30
       return t;
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    31
    }
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
    32
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    33
    return f;
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
    34
}
3338
9de326b3099c Fixed bug #817
Sam Lantinga <slouken@libsdl.org>
parents: 3327
diff changeset
    35
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    36
static
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    37
void RunBasicTest()
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    38
{
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    39
    int value;
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    40
    SDL_SpinLock lock = 0;
3261
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
    41
5013
9d6b527e0912 Fixed compiler errors
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
    42
    SDL_atomic_t v;
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    43
    SDL_bool tfret = SDL_FALSE;
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
    44
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    45
    SDL_Log("\nspin lock---------------------------------------\n\n");
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
    46
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    47
    SDL_AtomicLock(&lock);
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    48
    SDL_Log("AtomicLock                   lock=%d\n", lock);
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    49
    SDL_AtomicUnlock(&lock);
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    50
    SDL_Log("AtomicUnlock                 lock=%d\n", lock);
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
    51
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    52
    SDL_Log("\natomic -----------------------------------------\n\n");
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
    53
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    54
    SDL_AtomicSet(&v, 0);
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    55
    tfret = SDL_AtomicSet(&v, 10) == 0 ? SDL_TRUE : SDL_FALSE;
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    56
    SDL_Log("AtomicSet(10)        tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    57
    tfret = SDL_AtomicAdd(&v, 10) == 10 ? SDL_TRUE : SDL_FALSE;
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    58
    SDL_Log("AtomicAdd(10)        tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
    59
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    60
    SDL_AtomicSet(&v, 0);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    61
    SDL_AtomicIncRef(&v);
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    62
    tfret = (SDL_AtomicGet(&v) == 1) ? SDL_TRUE : SDL_FALSE;
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    63
    SDL_Log("AtomicIncRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    64
    SDL_AtomicIncRef(&v);
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    65
    tfret = (SDL_AtomicGet(&v) == 2) ? SDL_TRUE : SDL_FALSE;
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    66
    SDL_Log("AtomicIncRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    67
    tfret = (SDL_AtomicDecRef(&v) == SDL_FALSE) ? SDL_TRUE : SDL_FALSE;
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    68
    SDL_Log("AtomicDecRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    69
    tfret = (SDL_AtomicDecRef(&v) == SDL_TRUE) ? SDL_TRUE : SDL_FALSE;
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    70
    SDL_Log("AtomicDecRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
    71
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    72
    SDL_AtomicSet(&v, 10);
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    73
    tfret = (SDL_AtomicCAS(&v, 0, 20) == SDL_FALSE) ? SDL_TRUE : SDL_FALSE;
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    74
    SDL_Log("AtomicCAS()          tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
    75
    value = SDL_AtomicGet(&v);
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    76
    tfret = (SDL_AtomicCAS(&v, value, 20) == SDL_TRUE) ? SDL_TRUE : SDL_FALSE;
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
    77
    SDL_Log("AtomicCAS()          tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    78
}
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3201
diff changeset
    79
5018
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
    80
/**************************************************************************/
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
    81
/* Atomic operation test
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
    82
 * Adapted with permission from code by Michael Davidsaver at:
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
    83
 *  http://bazaar.launchpad.net/~mdavidsaver/epics-base/atomic/revision/12105#src/libCom/test/epicsAtomicTest.c
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
    84
 * Original copyright 2010 Brookhaven Science Associates as operator of Brookhaven National Lab
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
    85
 * http://www.aps.anl.gov/epics/license/open.php
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
    86
 */
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    87
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    88
/* Tests semantics of atomic operations.  Also a stress test
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    89
 * to see if they are really atomic.
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    90
 *
7538
1d187795e103 Corrected words in comments of test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7517
diff changeset
    91
 * Several threads adding to the same variable.
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    92
 * at the end the value is compared with the expected
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    93
 * and with a non-atomic counter.
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    94
 */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
    95
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    96
/* Number of concurrent incrementers */
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    97
#define NThreads 2
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    98
#define CountInc 100
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    99
#define VALBITS (sizeof(atomicValue)*8)
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   100
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   101
#define atomicValue int
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   102
#define CountTo ((atomicValue)((unsigned int)(1<<(VALBITS-1))-1))
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   103
#define NInter (CountTo/CountInc/NThreads)
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   104
#define Expect (CountTo-NInter*CountInc*NThreads)
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   105
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   106
SDL_COMPILE_TIME_ASSERT(size, CountTo>0); /* check for rollover */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   107
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   108
static SDL_atomic_t good = { 42 };
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   109
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   110
static atomicValue bad = 42;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   111
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   112
static SDL_atomic_t threadsRunning;
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   113
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   114
static SDL_sem *threadDone;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   115
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   116
static
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   117
int adder(void* junk)
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   118
{
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   119
    unsigned long N=NInter;
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   120
    SDL_Log("Thread subtracting %d %lu times\n",CountInc,N);
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   121
    while (N--) {
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   122
        SDL_AtomicAdd(&good, -CountInc);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   123
        bad-=CountInc;
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   124
    }
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   125
    SDL_AtomicAdd(&threadsRunning, -1);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   126
    SDL_SemPost(threadDone);
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
   127
    return 0;
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 3338
diff changeset
   128
}
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   129
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   130
static
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   131
void runAdder(void)
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   132
{
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   133
    Uint32 start, end;
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   134
    int T=NThreads;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   135
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   136
    start = SDL_GetTicks();
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   137
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   138
    threadDone = SDL_CreateSemaphore(0);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   139
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   140
    SDL_AtomicSet(&threadsRunning, NThreads);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   141
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   142
    while (T--)
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   143
        SDL_CreateThread(adder, "Adder", NULL);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   144
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   145
    while (SDL_AtomicGet(&threadsRunning) > 0)
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   146
        SDL_SemWait(threadDone);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   147
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   148
    SDL_DestroySemaphore(threadDone);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   149
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   150
    end = SDL_GetTicks();
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   151
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   152
    SDL_Log("Finished in %f sec\n", (end - start) / 1000.f);
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   153
}
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   154
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   155
static
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   156
void RunEpicTest()
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   157
{
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   158
    int b;
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   159
    atomicValue v;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   160
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   161
    SDL_Log("\nepic test---------------------------------------\n\n");
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   162
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   163
    SDL_Log("Size asserted to be >= 32-bit\n");
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   164
    SDL_assert(sizeof(atomicValue)>=4);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   165
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   166
    SDL_Log("Check static initializer\n");
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   167
    v=SDL_AtomicGet(&good);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   168
    SDL_assert(v==42);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   169
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   170
    SDL_assert(bad==42);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   171
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   172
    SDL_Log("Test negative values\n");
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   173
    SDL_AtomicSet(&good, -5);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   174
    v=SDL_AtomicGet(&good);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   175
    SDL_assert(v==-5);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   176
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   177
    SDL_Log("Verify maximum value\n");
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   178
    SDL_AtomicSet(&good, CountTo);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   179
    v=SDL_AtomicGet(&good);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   180
    SDL_assert(v==CountTo);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   181
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   182
    SDL_Log("Test compare and exchange\n");
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   183
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   184
    b=SDL_AtomicCAS(&good, 500, 43);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   185
    SDL_assert(!b); /* no swap since CountTo!=500 */
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   186
    v=SDL_AtomicGet(&good);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   187
    SDL_assert(v==CountTo); /* ensure no swap */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   188
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   189
    b=SDL_AtomicCAS(&good, CountTo, 44);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   190
    SDL_assert(!!b); /* will swap */
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   191
    v=SDL_AtomicGet(&good);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   192
    SDL_assert(v==44);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   193
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   194
    SDL_Log("Test Add\n");
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   195
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   196
    v=SDL_AtomicAdd(&good, 1);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   197
    SDL_assert(v==44);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   198
    v=SDL_AtomicGet(&good);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   199
    SDL_assert(v==45);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   200
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   201
    v=SDL_AtomicAdd(&good, 10);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   202
    SDL_assert(v==45);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   203
    v=SDL_AtomicGet(&good);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   204
    SDL_assert(v==55);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   205
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   206
    SDL_Log("Test Add (Negative values)\n");
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   207
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   208
    v=SDL_AtomicAdd(&good, -20);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   209
    SDL_assert(v==55);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   210
    v=SDL_AtomicGet(&good);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   211
    SDL_assert(v==35);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   212
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   213
    v=SDL_AtomicAdd(&good, -50); /* crossing zero down */
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   214
    SDL_assert(v==35);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   215
    v=SDL_AtomicGet(&good);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   216
    SDL_assert(v==-15);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   217
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   218
    v=SDL_AtomicAdd(&good, 30); /* crossing zero up */
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   219
    SDL_assert(v==-15);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   220
    v=SDL_AtomicGet(&good);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   221
    SDL_assert(v==15);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   222
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   223
    SDL_Log("Reset before count down test\n");
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   224
    SDL_AtomicSet(&good, CountTo);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   225
    v=SDL_AtomicGet(&good);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   226
    SDL_assert(v==CountTo);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   227
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   228
    bad=CountTo;
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   229
    SDL_assert(bad==CountTo);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   230
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   231
    SDL_Log("Counting down from %d, Expect %d remaining\n",CountTo,Expect);
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   232
    runAdder();
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   233
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   234
    v=SDL_AtomicGet(&good);
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   235
    SDL_Log("Atomic %d Non-Atomic %d\n",v,bad);
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   236
    SDL_assert(v==Expect);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   237
    SDL_assert(bad!=Expect);
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   238
}
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   239
5018
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
   240
/* End atomic operation test */
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
   241
/**************************************************************************/
342b158efbbe Michael gave permission to use his test code
Sam Lantinga <slouken@libsdl.org>
parents: 5013
diff changeset
   242
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   243
/**************************************************************************/
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   244
/* Lock-free FIFO test */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   245
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   246
/* This is useful to test the impact of another thread locking the queue
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   247
   entirely for heavy-weight manipulation.
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   248
 */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   249
#define TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   250
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   251
#define NUM_READERS 4
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   252
#define NUM_WRITERS 4
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   253
#define EVENTS_PER_WRITER   1000000
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   254
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   255
/* The number of entries must be a power of 2 */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   256
#define MAX_ENTRIES 256
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   257
#define WRAP_MASK   (MAX_ENTRIES-1)
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   258
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   259
typedef struct
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   260
{
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   261
    SDL_atomic_t sequence;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   262
    SDL_Event event;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   263
} SDL_EventQueueEntry;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   264
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   265
typedef struct
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   266
{
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   267
    SDL_EventQueueEntry entries[MAX_ENTRIES];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   268
5115
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5114
diff changeset
   269
    char cache_pad1[SDL_CACHELINE_SIZE-((sizeof(SDL_EventQueueEntry)*MAX_ENTRIES)%SDL_CACHELINE_SIZE)];
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   270
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   271
    SDL_atomic_t enqueue_pos;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   272
5115
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5114
diff changeset
   273
    char cache_pad2[SDL_CACHELINE_SIZE-sizeof(SDL_atomic_t)];
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   274
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   275
    SDL_atomic_t dequeue_pos;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   276
5115
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5114
diff changeset
   277
    char cache_pad3[SDL_CACHELINE_SIZE-sizeof(SDL_atomic_t)];
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   278
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   279
#ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   280
    SDL_SpinLock lock;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   281
    SDL_atomic_t rwcount;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   282
    SDL_atomic_t watcher;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   283
5115
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5114
diff changeset
   284
    char cache_pad4[SDL_CACHELINE_SIZE-sizeof(SDL_SpinLock)-2*sizeof(SDL_atomic_t)];
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   285
#endif
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   286
10003
d91a2c45825e Remove almost all instances of "volatile" keyword.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   287
    SDL_atomic_t active;
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   288
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   289
    /* Only needed for the mutex test */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   290
    SDL_mutex *mutex;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   291
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   292
} SDL_EventQueue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   293
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   294
static void InitEventQueue(SDL_EventQueue *queue)
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   295
{
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   296
    int i;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   297
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   298
    for (i = 0; i < MAX_ENTRIES; ++i) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   299
        SDL_AtomicSet(&queue->entries[i].sequence, i);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   300
    }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   301
    SDL_AtomicSet(&queue->enqueue_pos, 0);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   302
    SDL_AtomicSet(&queue->dequeue_pos, 0);
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   303
#ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   304
    queue->lock = 0;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   305
    SDL_AtomicSet(&queue->rwcount, 0);
8838
b50a84131dc9 Fixed initializing test event queue
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   306
    SDL_AtomicSet(&queue->watcher, 0);
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   307
#endif
10003
d91a2c45825e Remove almost all instances of "volatile" keyword.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   308
    SDL_AtomicSet(&queue->active, 1);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   309
}
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   310
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   311
static SDL_bool EnqueueEvent_LockFree(SDL_EventQueue *queue, const SDL_Event *event)
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   312
{
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   313
    SDL_EventQueueEntry *entry;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   314
    unsigned queue_pos;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   315
    unsigned entry_seq;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   316
    int delta;
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   317
    SDL_bool status;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   318
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   319
#ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   320
    /* This is a gate so an external thread can lock the queue */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   321
    SDL_AtomicLock(&queue->lock);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   322
    SDL_assert(SDL_AtomicGet(&queue->watcher) == 0);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   323
    SDL_AtomicIncRef(&queue->rwcount);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   324
    SDL_AtomicUnlock(&queue->lock);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   325
#endif
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   326
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   327
    queue_pos = (unsigned)SDL_AtomicGet(&queue->enqueue_pos);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   328
    for ( ; ; ) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   329
        entry = &queue->entries[queue_pos & WRAP_MASK];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   330
        entry_seq = (unsigned)SDL_AtomicGet(&entry->sequence);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   331
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   332
        delta = (int)(entry_seq - queue_pos);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   333
        if (delta == 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   334
            /* The entry and the queue position match, try to increment the queue position */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   335
            if (SDL_AtomicCAS(&queue->enqueue_pos, (int)queue_pos, (int)(queue_pos+1))) {
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   336
                /* We own the object, fill it! */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   337
                entry->event = *event;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   338
                SDL_AtomicSet(&entry->sequence, (int)(queue_pos + 1));
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   339
                status = SDL_TRUE;
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   340
                break;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   341
            }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   342
        } else if (delta < 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   343
            /* We ran into an old queue entry, which means it still needs to be dequeued */
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   344
            status = SDL_FALSE;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   345
            break;
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   346
        } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   347
            /* We ran into a new queue entry, get the new queue position */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   348
            queue_pos = (unsigned)SDL_AtomicGet(&queue->enqueue_pos);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   349
        }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   350
    }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   351
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   352
#ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   353
    SDL_AtomicDecRef(&queue->rwcount);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   354
#endif
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   355
    return status;
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   356
}
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   357
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   358
static SDL_bool DequeueEvent_LockFree(SDL_EventQueue *queue, SDL_Event *event)
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   359
{
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   360
    SDL_EventQueueEntry *entry;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   361
    unsigned queue_pos;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   362
    unsigned entry_seq;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   363
    int delta;
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   364
    SDL_bool status;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   365
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   366
#ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   367
    /* This is a gate so an external thread can lock the queue */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   368
    SDL_AtomicLock(&queue->lock);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   369
    SDL_assert(SDL_AtomicGet(&queue->watcher) == 0);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   370
    SDL_AtomicIncRef(&queue->rwcount);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   371
    SDL_AtomicUnlock(&queue->lock);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   372
#endif
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   373
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   374
    queue_pos = (unsigned)SDL_AtomicGet(&queue->dequeue_pos);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   375
    for ( ; ; ) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   376
        entry = &queue->entries[queue_pos & WRAP_MASK];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   377
        entry_seq = (unsigned)SDL_AtomicGet(&entry->sequence);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   378
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   379
        delta = (int)(entry_seq - (queue_pos + 1));
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   380
        if (delta == 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   381
            /* The entry and the queue position match, try to increment the queue position */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   382
            if (SDL_AtomicCAS(&queue->dequeue_pos, (int)queue_pos, (int)(queue_pos+1))) {
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   383
                /* We own the object, fill it! */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   384
                *event = entry->event;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   385
                SDL_AtomicSet(&entry->sequence, (int)(queue_pos+MAX_ENTRIES));
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   386
                status = SDL_TRUE;
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   387
                break;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   388
            }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   389
        } else if (delta < 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   390
            /* We ran into an old queue entry, which means we've hit empty */
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   391
            status = SDL_FALSE;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   392
            break;
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   393
        } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   394
            /* We ran into a new queue entry, get the new queue position */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   395
            queue_pos = (unsigned)SDL_AtomicGet(&queue->dequeue_pos);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   396
        }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   397
    }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   398
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   399
#ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   400
    SDL_AtomicDecRef(&queue->rwcount);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   401
#endif
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   402
    return status;
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   403
}
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   404
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   405
static SDL_bool EnqueueEvent_Mutex(SDL_EventQueue *queue, const SDL_Event *event)
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   406
{
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   407
    SDL_EventQueueEntry *entry;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   408
    unsigned queue_pos;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   409
    unsigned entry_seq;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   410
    int delta;
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   411
    SDL_bool status = SDL_FALSE;
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   412
6977
b73d51026c68 Changed the name of SDL_mutexP() SDL_mutexV()
Sam Lantinga <slouken@libsdl.org>
parents: 5969
diff changeset
   413
    SDL_LockMutex(queue->mutex);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   414
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   415
    queue_pos = (unsigned)queue->enqueue_pos.value;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   416
    entry = &queue->entries[queue_pos & WRAP_MASK];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   417
    entry_seq = (unsigned)entry->sequence.value;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   418
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   419
    delta = (int)(entry_seq - queue_pos);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   420
    if (delta == 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   421
        ++queue->enqueue_pos.value;
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   422
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   423
        /* We own the object, fill it! */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   424
        entry->event = *event;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   425
        entry->sequence.value = (int)(queue_pos + 1);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   426
        status = SDL_TRUE;
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   427
    } else if (delta < 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   428
        /* We ran into an old queue entry, which means it still needs to be dequeued */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   429
    } else {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   430
        SDL_Log("ERROR: mutex failed!\n");
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   431
    }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   432
6977
b73d51026c68 Changed the name of SDL_mutexP() SDL_mutexV()
Sam Lantinga <slouken@libsdl.org>
parents: 5969
diff changeset
   433
    SDL_UnlockMutex(queue->mutex);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   434
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   435
    return status;
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   436
}
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   437
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   438
static SDL_bool DequeueEvent_Mutex(SDL_EventQueue *queue, SDL_Event *event)
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   439
{
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   440
    SDL_EventQueueEntry *entry;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   441
    unsigned queue_pos;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   442
    unsigned entry_seq;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   443
    int delta;
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   444
    SDL_bool status = SDL_FALSE;
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   445
6977
b73d51026c68 Changed the name of SDL_mutexP() SDL_mutexV()
Sam Lantinga <slouken@libsdl.org>
parents: 5969
diff changeset
   446
    SDL_LockMutex(queue->mutex);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   447
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   448
    queue_pos = (unsigned)queue->dequeue_pos.value;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   449
    entry = &queue->entries[queue_pos & WRAP_MASK];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   450
    entry_seq = (unsigned)entry->sequence.value;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   451
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   452
    delta = (int)(entry_seq - (queue_pos + 1));
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   453
    if (delta == 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   454
        ++queue->dequeue_pos.value;
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   455
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   456
        /* We own the object, fill it! */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   457
        *event = entry->event;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   458
        entry->sequence.value = (int)(queue_pos + MAX_ENTRIES);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   459
        status = SDL_TRUE;
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   460
    } else if (delta < 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   461
        /* We ran into an old queue entry, which means we've hit empty */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   462
    } else {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   463
        SDL_Log("ERROR: mutex failed!\n");
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   464
    }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   465
6977
b73d51026c68 Changed the name of SDL_mutexP() SDL_mutexV()
Sam Lantinga <slouken@libsdl.org>
parents: 5969
diff changeset
   466
    SDL_UnlockMutex(queue->mutex);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   467
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   468
    return status;
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   469
}
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   470
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   471
static SDL_sem *writersDone;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   472
static SDL_sem *readersDone;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   473
static SDL_atomic_t writersRunning;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   474
static SDL_atomic_t readersRunning;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   475
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   476
typedef struct
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   477
{
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   478
    SDL_EventQueue *queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   479
    int index;
5115
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5114
diff changeset
   480
    char padding1[SDL_CACHELINE_SIZE-(sizeof(SDL_EventQueue*)+sizeof(int))%SDL_CACHELINE_SIZE];
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   481
    int waits;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   482
    SDL_bool lock_free;
5115
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5114
diff changeset
   483
    char padding2[SDL_CACHELINE_SIZE-sizeof(int)-sizeof(SDL_bool)];
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   484
} WriterData;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   485
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   486
typedef struct
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   487
{
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   488
    SDL_EventQueue *queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   489
    int counters[NUM_WRITERS];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   490
    int waits;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   491
    SDL_bool lock_free;
5115
427998ff3bcf Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents: 5114
diff changeset
   492
    char padding[SDL_CACHELINE_SIZE-(sizeof(SDL_EventQueue*)+sizeof(int)*NUM_WRITERS+sizeof(int)+sizeof(SDL_bool))%SDL_CACHELINE_SIZE];
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   493
} ReaderData;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   494
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   495
static int FIFO_Writer(void* _data)
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   496
{
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   497
    WriterData *data = (WriterData *)_data;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   498
    SDL_EventQueue *queue = data->queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   499
    int i;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   500
    SDL_Event event;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   501
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   502
    event.type = SDL_USEREVENT;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   503
    event.user.windowID = 0;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   504
    event.user.code = 0;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   505
    event.user.data1 = data;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   506
    event.user.data2 = NULL;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   507
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   508
    if (data->lock_free) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   509
        for (i = 0; i < EVENTS_PER_WRITER; ++i) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   510
            event.user.code = i;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   511
            while (!EnqueueEvent_LockFree(queue, &event)) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   512
                ++data->waits;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   513
                SDL_Delay(0);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   514
            }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   515
        }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   516
    } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   517
        for (i = 0; i < EVENTS_PER_WRITER; ++i) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   518
            event.user.code = i;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   519
            while (!EnqueueEvent_Mutex(queue, &event)) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   520
                ++data->waits;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   521
                SDL_Delay(0);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   522
            }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   523
        }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   524
    }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   525
    SDL_AtomicAdd(&writersRunning, -1);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   526
    SDL_SemPost(writersDone);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   527
    return 0;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   528
}
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   529
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   530
static int FIFO_Reader(void* _data)
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   531
{
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   532
    ReaderData *data = (ReaderData *)_data;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   533
    SDL_EventQueue *queue = data->queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   534
    SDL_Event event;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   535
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   536
    if (data->lock_free) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   537
        for ( ; ; ) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   538
            if (DequeueEvent_LockFree(queue, &event)) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   539
                WriterData *writer = (WriterData*)event.user.data1;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   540
                ++data->counters[writer->index];
10003
d91a2c45825e Remove almost all instances of "volatile" keyword.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   541
            } else if (SDL_AtomicGet(&queue->active)) {
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   542
                ++data->waits;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   543
                SDL_Delay(0);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   544
            } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   545
                /* We drained the queue, we're done! */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   546
                break;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   547
            }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   548
        }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   549
    } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   550
        for ( ; ; ) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   551
            if (DequeueEvent_Mutex(queue, &event)) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   552
                WriterData *writer = (WriterData*)event.user.data1;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   553
                ++data->counters[writer->index];
10003
d91a2c45825e Remove almost all instances of "volatile" keyword.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   554
            } else if (SDL_AtomicGet(&queue->active)) {
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   555
                ++data->waits;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   556
                SDL_Delay(0);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   557
            } else {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   558
                /* We drained the queue, we're done! */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   559
                break;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   560
            }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   561
        }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   562
    }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   563
    SDL_AtomicAdd(&readersRunning, -1);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   564
    SDL_SemPost(readersDone);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   565
    return 0;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   566
}
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   567
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   568
#ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   569
/* This thread periodically locks the queue for no particular reason */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   570
static int FIFO_Watcher(void* _data)
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   571
{
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   572
    SDL_EventQueue *queue = (SDL_EventQueue *)_data;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   573
10003
d91a2c45825e Remove almost all instances of "volatile" keyword.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   574
    while (SDL_AtomicGet(&queue->active)) {
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   575
        SDL_AtomicLock(&queue->lock);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   576
        SDL_AtomicIncRef(&queue->watcher);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   577
        while (SDL_AtomicGet(&queue->rwcount) > 0) {
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   578
            SDL_Delay(0);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   579
        }
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   580
        /* Do queue manipulation here... */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   581
        SDL_AtomicDecRef(&queue->watcher);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   582
        SDL_AtomicUnlock(&queue->lock);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   583
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   584
        /* Wait a bit... */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   585
        SDL_Delay(1);
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   586
    }
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   587
    return 0;
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   588
}
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   589
#endif /* TEST_SPINLOCK_FIFO */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   590
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   591
static void RunFIFOTest(SDL_bool lock_free)
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   592
{
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   593
    SDL_EventQueue queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   594
    WriterData writerData[NUM_WRITERS];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   595
    ReaderData readerData[NUM_READERS];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   596
    Uint32 start, end;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   597
    int i, j;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   598
    int grand_total;
9922
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   599
    char textBuffer[1024];
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   600
    int len;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   601
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   602
    SDL_Log("\nFIFO test---------------------------------------\n\n");
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   603
    SDL_Log("Mode: %s\n", lock_free ? "LockFree" : "Mutex");
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   604
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   605
    readersDone = SDL_CreateSemaphore(0);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   606
    writersDone = SDL_CreateSemaphore(0);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   607
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   608
    SDL_memset(&queue, 0xff, sizeof(queue));
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   609
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   610
    InitEventQueue(&queue);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   611
    if (!lock_free) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   612
        queue.mutex = SDL_CreateMutex();
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   613
    }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   614
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   615
    start = SDL_GetTicks();
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   616
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   617
#ifdef TEST_SPINLOCK_FIFO
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   618
    /* Start a monitoring thread */
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   619
    if (lock_free) {
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   620
        SDL_CreateThread(FIFO_Watcher, "FIFOWatcher", &queue);
5099
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   621
    }
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   622
#endif
1b3678ac9804 Added a test to measure the impact of a separate thread periodically locking the queue entirely.
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   623
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   624
    /* Start the readers first */
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   625
    SDL_Log("Starting %d readers\n", NUM_READERS);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   626
    SDL_zero(readerData);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   627
    SDL_AtomicSet(&readersRunning, NUM_READERS);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   628
    for (i = 0; i < NUM_READERS; ++i) {
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   629
        char name[64];
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   630
        SDL_snprintf(name, sizeof (name), "FIFOReader%d", i);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   631
        readerData[i].queue = &queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   632
        readerData[i].lock_free = lock_free;
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   633
        SDL_CreateThread(FIFO_Reader, name, &readerData[i]);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   634
    }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   635
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   636
    /* Start up the writers */
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   637
    SDL_Log("Starting %d writers\n", NUM_WRITERS);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   638
    SDL_zero(writerData);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   639
    SDL_AtomicSet(&writersRunning, NUM_WRITERS);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   640
    for (i = 0; i < NUM_WRITERS; ++i) {
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   641
        char name[64];
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   642
        SDL_snprintf(name, sizeof (name), "FIFOWriter%d", i);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   643
        writerData[i].queue = &queue;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   644
        writerData[i].index = i;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   645
        writerData[i].lock_free = lock_free;
5969
3a041d215edc 1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   646
        SDL_CreateThread(FIFO_Writer, name, &writerData[i]);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   647
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   648
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   649
    /* Wait for the writers */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   650
    while (SDL_AtomicGet(&writersRunning) > 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   651
        SDL_SemWait(writersDone);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   652
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   653
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   654
    /* Shut down the queue so readers exit */
10003
d91a2c45825e Remove almost all instances of "volatile" keyword.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   655
    SDL_AtomicSet(&queue.active, 0);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   656
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   657
    /* Wait for the readers */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   658
    while (SDL_AtomicGet(&readersRunning) > 0) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   659
        SDL_SemWait(readersDone);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   660
    }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   661
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   662
    end = SDL_GetTicks();
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   663
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   664
    SDL_DestroySemaphore(readersDone);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   665
    SDL_DestroySemaphore(writersDone);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   666
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   667
    if (!lock_free) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   668
        SDL_DestroyMutex(queue.mutex);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   669
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6977
diff changeset
   670
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   671
    SDL_Log("Finished in %f sec\n", (end - start) / 1000.f);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   672
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   673
    SDL_Log("\n");
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   674
    for (i = 0; i < NUM_WRITERS; ++i) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   675
        SDL_Log("Writer %d wrote %d events, had %d waits\n", i, EVENTS_PER_WRITER, writerData[i].waits);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   676
    }
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   677
    SDL_Log("Writers wrote %d total events\n", NUM_WRITERS*EVENTS_PER_WRITER);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   678
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   679
    /* Print a breakdown of which readers read messages from which writer */
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   680
    SDL_Log("\n");
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   681
    grand_total = 0;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   682
    for (i = 0; i < NUM_READERS; ++i) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   683
        int total = 0;
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   684
        for (j = 0; j < NUM_WRITERS; ++j) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   685
            total += readerData[i].counters[j];
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   686
        }
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   687
        grand_total += total;
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   688
        SDL_Log("Reader %d read %d events, had %d waits\n", i, total, readerData[i].waits);
9922
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   689
        SDL_snprintf(textBuffer, sizeof(textBuffer), "  { ");
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   690
        for (j = 0; j < NUM_WRITERS; ++j) {
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   691
            if (j > 0) {
9922
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   692
                len = SDL_strlen(textBuffer);
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   693
                SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, ", ");
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   694
            }
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   695
            len = SDL_strlen(textBuffer);
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   696
            SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, "%d", readerData[i].counters[j]);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   697
        }
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   698
        len = SDL_strlen(textBuffer);
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   699
        SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, " }\n");
9922
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   700
        SDL_Log("%s", textBuffer);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   701
    }
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   702
    SDL_Log("Readers read %d total events\n", grand_total);
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   703
}
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   704
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   705
/* End FIFO test */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   706
/**************************************************************************/
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   707
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   708
int
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   709
main(int argc, char *argv[])
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   710
{
9922
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   711
    /* Enable standard application logging */
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   712
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7538
diff changeset
   713
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   714
    RunBasicTest();
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   715
    RunEpicTest();
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   716
/* This test is really slow, so don't run it by default */
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   717
#if 0
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   718
    RunFIFOTest(SDL_FALSE);
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   719
#endif
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   720
    RunFIFOTest(SDL_TRUE);
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   721
    return 0;
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   722
}
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   723
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5018
diff changeset
   724
/* vi: set ts=4 sw=4 expandtab: */