Commit 11921a8b authored by Bob Pendleton's avatar Bob Pendleton

The new, cleaner, version of the atomic operations. The dummy code is what you...

The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
The linux code appears to be complete and *should* be the base of all Unix and GCC based versions. The macosx and win32 versions
are currently just copies of the dummy code. I will begin working on the windows version as soon as this check in is done. I
need someone to work on the Mac OS X version.

I'm afraid that this check in will break QNX (Sorry!)

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403802
parent 5ca4f681
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
Sam Lantinga Sam Lantinga
slouken@libsdl.org slouken@libsdl.org
Contributed by Bob Pendleton, bob@pendleton.com
*/ */
/** /**
...@@ -46,48 +48,50 @@ extern "C" { ...@@ -46,48 +48,50 @@ extern "C" {
* processor specific atomic operations. When possible they are * processor specific atomic operations. When possible they are
* implemented as true processor specific atomic operations. When that * implemented as true processor specific atomic operations. When that
* is not possible the are implemented using locks that *do* use the * is not possible the are implemented using locks that *do* use the
* available atomic operations. In rare cases they may be implemented * available atomic operations.
* using SDL's mutex fuctions. *
* At the very minimum spin locks must be implemented. Without spin
* locks it is not possible (AFAICT) to emulate the rest of the atomic
* operations.
*/ */
/* Function prototypes */ /* Function prototypes */
/* 32 bit atomic operations */
/** /**
* \fn int SDL_AtomicExchange32(volatile Uint32 * ptr, Uint32 value) * SDL AtomicLock.
* *
* \brief Atomically exchange two 32 bit values. * The spin lock functions and type are required and can not be
* * emulated because they are used in the emulation code.
* \return the value point to by ptr. */
typedef volatile Uint32 SDL_SpinLock;
/**
* \fn void SDL_AtomicLock(SDL_SpinLock *lock);
* *
* \param ptr points to the value to be fetched from *ptr. * \brief Lock a spin lock by setting it to a none zero value.
* \param value is value to be stored at *ptr.
* *
* The current value stored at *ptr is returned and it is replaced * \param lock points to the lock.
* with value. This function can be used to implement SDL_TestThenSet.
* *
*/ */
extern DECLSPEC Uint32 SDLCALL SDL_AtomicExchange32(volatile Uint32 * ptr, Uint32 value); extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
/** /**
* \fn int SDL_AtomicCompareThenSet32(volatile Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue) * \fn void SDL_AtomicUnlock(SDL_SpinLock *lock);
*
* \brief If *ptr == oldvalue then replace the contents of *ptr by new value.
* *
* \return true if the newvalue was stored. * \brief Unlock a spin lock by setting it to 0. Always returns immediately
* *
* \param *ptr is the value to be compared and replaced. * \param lock points to the lock.
* \param oldvalue is value to be compared to *ptr.
* \param newvalue is value to be stored at *ptr.
* *
*/ */
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet32(volatile Uint32 * ptr, extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
Uint32 oldvalue, Uint32 newvalue);
/* 32 bit atomic operations */
/** /**
* \fn SDL_bool SDL_AtomicTestThenSet32(volatile Uint32 * ptr); * \fn SDL_bool SDL_AtomicTestThenSet32(volatile Uint32 * ptr);
* *
* \brief Check to see if *ptr == 0 and set it to non-zero. * \brief Check to see if *ptr == 0 and set it to 1.
* *
* \return SDL_True if the value pointed to by ptr was zero and * \return SDL_True if the value pointed to by ptr was zero and
* SDL_False if it was not zero * SDL_False if it was not zero
...@@ -211,9 +215,6 @@ extern DECLSPEC Uint32 SDLCALL SDL_AtomicSubtractThenFetch32(volatile Uint32 * p ...@@ -211,9 +215,6 @@ extern DECLSPEC Uint32 SDLCALL SDL_AtomicSubtractThenFetch32(volatile Uint32 * p
/* 64 bit atomic operations */ /* 64 bit atomic operations */
#ifdef SDL_HAS_64BIT_TYPE #ifdef SDL_HAS_64BIT_TYPE
extern DECLSPEC Uint64 SDLCALL SDL_AtomicExchange64(volatile Uint64 * ptr, Uint64 value);
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet64(volatile Uint64 * ptr,
Uint64 oldvalue, Uint64 newvalue);
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet64(volatile Uint64 * ptr); extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet64(volatile Uint64 * ptr);
extern DECLSPEC void SDLCALL SDL_AtomicClear64(volatile Uint64 * ptr); extern DECLSPEC void SDLCALL SDL_AtomicClear64(volatile Uint64 * ptr);
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenIncrement64(volatile Uint64 * ptr); extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenIncrement64(volatile Uint64 * ptr);
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -29,21 +29,18 @@ main(int argc, char **argv) ...@@ -29,21 +29,18 @@ main(int argc, char **argv)
volatile Uint64 val64 = 0; volatile Uint64 val64 = 0;
Uint64 ret64 = 0; Uint64 ret64 = 0;
SDL_SpinLock lock = 0;
SDL_bool tfret = SDL_FALSE; SDL_bool tfret = SDL_FALSE;
printf("32 bit -----------------------------------------\n\n"); printf("\nspin lock---------------------------------------\n\n");
ret32 = SDL_AtomicExchange32(&val32, 10); SDL_AtomicLock(&lock);
printf("Exchange32 ret=%d val=%d\n", ret32, val32); printf("AtomicLock lock=%d\n", lock);
ret32 = SDL_AtomicExchange32(&val32, 0); SDL_AtomicUnlock(&lock);
printf("Exchange32 ret=%d val=%d\n", ret32, val32); printf("AtomicUnlock lock=%d\n", lock);
val32 = 10; printf("\n32 bit -----------------------------------------\n\n");
tfret = SDL_AtomicCompareThenSet32(&val32, 10, 20);
printf("CompareThenSet32 tfret=%s val=%d\n", tf(tfret), val32);
val32 = 10;
tfret = SDL_AtomicCompareThenSet32(&val32, 0, 20);
printf("CompareThenSet32 tfret=%s val=%d\n", tf(tfret), val32);
val32 = 0; val32 = 0;
tfret = SDL_AtomicTestThenSet32(&val32); tfret = SDL_AtomicTestThenSet32(&val32);
...@@ -79,19 +76,7 @@ main(int argc, char **argv) ...@@ -79,19 +76,7 @@ main(int argc, char **argv)
printf("SubtractThenFetch32 ret=%d val=%d\n", ret32, val32); printf("SubtractThenFetch32 ret=%d val=%d\n", ret32, val32);
#ifdef SDL_HAS_64BIT_TYPE #ifdef SDL_HAS_64BIT_TYPE
printf("64 bit -----------------------------------------\n\n"); printf("\n64 bit -----------------------------------------\n\n");
ret64 = SDL_AtomicExchange64(&val64, 10);
printf("Exchange64 ret=%lld val=%lld\n", ret64, val64);
ret64 = SDL_AtomicExchange64(&val64, 0);
printf("Exchange64 ret=%lld val=%lld\n", ret64, val64);
val64 = 10;
tfret = SDL_AtomicCompareThenSet64(&val64, 10, 20);
printf("CompareThenSet64 tfret=%s val=%lld\n", tf(tfret), val64);
val64 = 10;
tfret = SDL_AtomicCompareThenSet64(&val64, 0, 20);
printf("CompareThenSet64 tfret=%s val=%lld\n", tf(tfret), val64);
val64 = 0; val64 = 0;
tfret = SDL_AtomicTestThenSet64(&val64); tfret = SDL_AtomicTestThenSet64(&val64);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment