Atomic int/ptr operations?

A year, maybe two years, ago there was a lot of discussion of adding
support of atomic operations to SDL. And, I’m pretty sure a patch was
submitted. But, I can’t find atomic op code in SDL1.3. Did the atomic
op patch ever get into SDL?

I know one problem is that not every processor supports the same set
of atomic ops. It may be there is no common subset that we can include
in SDL. I would really like to see at least one atomic operation in
SDL. Even if all we got was atomic swap I would be very happy.

Anyone know the status of atomic operations in SDL 1.3?

Bob Pendleton–
±----------------------------------------------------------

It’s still on the TODO list, and I think there’s a patch in bugzilla. Bob,
do you want to check it out?

See ya,
–SamOn Mon, Jun 8, 2009 at 11:02 AM, Bob Pendleton wrote:

A year, maybe two years, ago there was a lot of discussion of adding
support of atomic operations to SDL. And, I’m pretty sure a patch was
submitted. But, I can’t find atomic op code in SDL1.3. Did the atomic
op patch ever get into SDL?

I know one problem is that not every processor supports the same set
of atomic ops. It may be there is no common subset that we can include
in SDL. I would really like to see at least one atomic operation in
SDL. Even if all we got was atomic swap I would be very happy.

Anyone know the status of atomic operations in SDL 1.3?

Bob Pendleton


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Yup, there is a patch…

Here’s the bug report:
http://bugzilla.libsdl.org/show_bug.cgi?id=646

And the linked patch:
http://landofbile.com/software/SDL_atomic.patch________________________________
From: slouken@libsdl.org (slouken)
To: A list for developers using the SDL library. (includes SDL-announce)
Sent: Monday, June 8, 2009 11:25:18 PM
Subject: Re: [SDL] Atomic int/ptr operations?

It’s still on the TODO list, and I think there’s a patch in bugzilla. Bob, do you want to check it out?

See ya,
–Sam

On Mon, Jun 8, 2009 at 11:02 AM, Bob Pendleton wrote:

A year, maybe two years, ago there was a lot of discussion of adding
support of atomic operations to SDL. And, I’m pretty sure a patch was
submitted. But, I can’t find atomic op code in SDL1.3. Did the atomic
op patch ever get into SDL?

I know one problem is that not every processor supports the same set
of atomic ops. It may be there is no common subset that we can include
in SDL. I would really like to see at least one atomic operation in
SDL. Even if all we got was atomic swap I would be very happy.

Anyone know the status of atomic operations in SDL 1.3?

Bob Pendleton


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

xchg xchg xchg xchg xchg xchg xchg xchg …

so what platforms will that patch operate on?On Tue, Jun 9, 2009 at 12:25 AM, Sam Lantinga wrote:

It’s still on the TODO list, and I think there’s a patch in bugzilla.? Bob,
do you want to check it out?

See ya,
–Sam

On Mon, Jun 8, 2009 at 11:02 AM, Bob Pendleton wrote:

A year, maybe two years, ago there was a lot of discussion of adding
support of atomic operations to SDL. And, I’m pretty sure a patch was
submitted. But, I can’t find atomic op code in SDL1.3. Did the atomic
op patch ever get into SDL?

I know one problem is that not every processor supports the same set
of atomic ops. It may be there is no common subset that we can include
in SDL. I would really like to see at least one atomic operation in
SDL. Even if all we got was atomic swap I would be very happy.

Anyone know the status of atomic operations in SDL 1.3?

Bob Pendleton


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


http://codebad.com/

Sure, I’ll take a look.

Bob PendletonOn Mon, Jun 8, 2009 at 11:25 PM, Sam Lantinga wrote:

It’s still on the TODO list, and I think there’s a patch in bugzilla.? Bob,
do you want to check it out?

See ya,
–Sam

On Mon, Jun 8, 2009 at 11:02 AM, Bob Pendleton <@Bob_Pendleton> wrote:

A year, maybe two years, ago there was a lot of discussion of adding
support of atomic operations to SDL. And, I’m pretty sure a patch was
submitted. But, I can’t find atomic op code in SDL1.3. Did the atomic
op patch ever get into SDL?

I know one problem is that not every processor supports the same set
of atomic ops. It may be there is no common subset that we can include
in SDL. I would really like to see at least one atomic operation in
SDL. Even if all we got was atomic swap I would be very happy.

Anyone know the status of atomic operations in SDL 1.3?

Bob Pendleton


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------

xchg xchg xchg xchg xchg xchg xchg xchg …

so what platforms will that patch operate on?

Don’t know yet! What platforms are you interested in? Why don’t you
take a look too.

Bob PendletonOn Tue, Jun 9, 2009 at 1:21 AM, Donny Viszneki<donny.viszneki at gmail.com> wrote:

On Tue, Jun 9, 2009 at 12:25 AM, Sam Lantinga wrote:

It’s still on the TODO list, and I think there’s a patch in bugzilla.? Bob,
do you want to check it out?

See ya,
–Sam

On Mon, Jun 8, 2009 at 11:02 AM, Bob Pendleton <@Bob_Pendleton> wrote:

A year, maybe two years, ago there was a lot of discussion of adding
support of atomic operations to SDL. And, I’m pretty sure a patch was
submitted. But, I can’t find atomic op code in SDL1.3. Did the atomic
op patch ever get into SDL?

I know one problem is that not every processor supports the same set
of atomic ops. It may be there is no common subset that we can include
in SDL. I would really like to see at least one atomic operation in
SDL. Even if all we got was atomic swap I would be very happy.

Anyone know the status of atomic operations in SDL 1.3?

Bob Pendleton


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


http://codebad.com/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------

I did take a look! That’s what “xchg xchg xchg xchg” was all about!On Tue, Jun 9, 2009 at 9:04 AM, Bob Pendleton wrote:

On Tue, Jun 9, 2009 at 1:21 AM, Donny Viszneki<@Donny_Viszneki> wrote:

xchg xchg xchg xchg xchg xchg xchg xchg …

so what platforms will that patch operate on?

Don’t know yet! What platforms are you interested in? Why don’t you
take a look too.


http://codebad.com/

Ok, I applied the patch. Got everything to compile. Got it all to
install correctly. And ran testatomic. It all works on my Linux box so
I checked it in. No guarantees about any other platform. So, update
from svn and test it on everything. If you find a problem post patches
and we’ll get them in.

xchg xchg xchg xchg xchg xchg xchg xchg …

so what platforms will that patch operate on?

Don’t know yet! What platforms are you interested in? Why don’t you
take a look too.

I did take a look! That’s what “xchg xchg xchg xchg” was all about!

Ya’know… I had not clue what that was about…

Bob PendletonOn Tue, Jun 9, 2009 at 11:16 AM, Donny Viszneki<donny.viszneki at gmail.com> wrote:

On Tue, Jun 9, 2009 at 9:04 AM, Bob Pendleton<@Bob_Pendleton> wrote:

On Tue, Jun 9, 2009 at 1:21 AM, Donny Viszneki<donny.viszneki at gmail.com> wrote:


http://codebad.com/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------

Bob, this broke building on Windows. There are a couple problems.

First, including windows.h indirectly in SDL.h breaks SDL_sysvideo.h, which
can be fixed, and potentially breaks application code that doesn’t compile
with windows.h included.

Second, at least on Visual C++ there are warnings about using the wrong
parameter types with the interlocked functions. These are correct, and even
though some care has been taken to make sure the parameters are the same
size, it makes me nervous.

See ya,
–SamOn Tue, Jun 9, 2009 at 11:03 AM, Bob Pendleton wrote:

Ok, I applied the patch. Got everything to compile. Got it all to
install correctly. And ran testatomic. It all works on my Linux box so
I checked it in. No guarantees about any other platform. So, update
from svn and test it on everything. If you find a problem post patches
and we’ll get them in.

On Tue, Jun 9, 2009 at 11:16 AM, Donny Viszneki<donny.viszneki at gmail.com> wrote:

On Tue, Jun 9, 2009 at 9:04 AM, Bob Pendleton wrote:

On Tue, Jun 9, 2009 at 1:21 AM, Donny Viszneki<donny.viszneki at gmail.com> wrote:

xchg xchg xchg xchg xchg xchg xchg xchg …

so what platforms will that patch operate on?

Don’t know yet! What platforms are you interested in? Why don’t you
take a look too.

I did take a look! That’s what “xchg xchg xchg xchg” was all about!

Ya’know… I had not clue what that was about…

Bob Pendleton


http://codebad.com/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Bob, this broke building on Windows. There are a couple problems.

First, including windows.h indirectly in SDL.h breaks SDL_sysvideo.h, which
can be fixed, and potentially breaks application code that doesn’t compile
with windows.h included.

Second, at least on Visual C++ there are warnings about using the wrong
parameter types with the interlocked functions. These are correct, and even
though some care has been taken to make sure the parameters are the same
size, it makes me nervous.

Sorry to take so long to reply, my wife and I took a long weekend to
celebrate our 32nd wedding anniversary. I agree with your nervousness.
Trouble is that I don’t currently have a computer with Windows on it. I’ll
see what I can do. It’s just that working on windows is so much like work…
:slight_smile:

Bob PendletonOn Fri, Jun 12, 2009 at 3:46 AM, Sam Lantinga wrote:

See ya,
–Sam

On Tue, Jun 9, 2009 at 11:03 AM, Bob Pendleton <@Bob_Pendleton> wrote:

Ok, I applied the patch. Got everything to compile. Got it all to
install correctly. And ran testatomic. It all works on my Linux box so
I checked it in. No guarantees about any other platform. So, update
from svn and test it on everything. If you find a problem post patches
and we’ll get them in.

On Tue, Jun 9, 2009 at 11:16 AM, Donny Viszneki<donny.viszneki at gmail.com> wrote:

On Tue, Jun 9, 2009 at 9:04 AM, Bob Pendleton<@Bob_Pendleton> wrote:

On Tue, Jun 9, 2009 at 1:21 AM, Donny Viszneki< donny.viszneki at gmail.com> wrote:

xchg xchg xchg xchg xchg xchg xchg xchg …

so what platforms will that patch operate on?

Don’t know yet! What platforms are you interested in? Why don’t you
take a look too.

I did take a look! That’s what “xchg xchg xchg xchg” was all about!

Ya’know… I had not clue what that was about…

Bob Pendleton


http://codebad.com/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------

It is off topic, but congratulations!–
Paulo

On Wed, Jun 17, 2009 at 10:22 PM, Bob Pendleton wrote:

On Fri, Jun 12, 2009 at 3:46 AM, Sam Lantinga wrote:

Bob, this broke building on Windows. There are a couple problems.

First, including windows.h indirectly in SDL.h breaks SDL_sysvideo.h,
which can be fixed, and potentially breaks application code that doesn’t
compile with windows.h included.

Second, at least on Visual C++ there are warnings about using the wrong
parameter types with the interlocked functions. These are correct, and even
though some care has been taken to make sure the parameters are the same
size, it makes me nervous.

Sorry to take so long to reply, my wife and I took a long weekend to
celebrate our 32nd wedding anniversary. I agree with your nervousness.
Trouble is that I don’t currently have a computer with Windows on it. I’ll
see what I can do. It’s just that working on windows is so much like work…
:slight_smile:

Bob Pendleton

See ya,
–Sam

On Tue, Jun 9, 2009 at 11:03 AM, Bob Pendleton wrote:

Ok, I applied the patch. Got everything to compile. Got it all to
install correctly. And ran testatomic. It all works on my Linux box so
I checked it in. No guarantees about any other platform. So, update
from svn and test it on everything. If you find a problem post patches
and we’ll get them in.

On Tue, Jun 9, 2009 at 11:16 AM, Donny Viszneki<donny.viszneki at gmail.com> wrote:

On Tue, Jun 9, 2009 at 9:04 AM, Bob Pendleton wrote:

On Tue, Jun 9, 2009 at 1:21 AM, Donny Viszneki< donny.viszneki at gmail.com> wrote:

xchg xchg xchg xchg xchg xchg xchg xchg …

so what platforms will that patch operate on?

Don’t know yet! What platforms are you interested in? Why don’t you
take a look too.

I did take a look! That’s what “xchg xchg xchg xchg” was all about!

Ya’know… I had not clue what that was about…

Bob Pendleton


http://codebad.com/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

It is off topic, but congratulations!

Thank you!

I didn’t realize I was posting to the whole list.

Bob PendletonOn Wed, Jun 17, 2009 at 3:47 PM, Paulo Pinto wrote:


Paulo

On Wed, Jun 17, 2009 at 10:22 PM, Bob Pendleton <@Bob_Pendleton> wrote:

On Fri, Jun 12, 2009 at 3:46 AM, Sam Lantinga wrote:

Bob, this broke building on Windows. There are a couple problems.

First, including windows.h indirectly in SDL.h breaks SDL_sysvideo.h,
which can be fixed, and potentially breaks application code that doesn’t
compile with windows.h included.

Second, at least on Visual C++ there are warnings about using the wrong
parameter types with the interlocked functions. These are correct, and even
though some care has been taken to make sure the parameters are the same
size, it makes me nervous.

Sorry to take so long to reply, my wife and I took a long weekend to
celebrate our 32nd wedding anniversary. I agree with your nervousness.
Trouble is that I don’t currently have a computer with Windows on it. I’ll
see what I can do. It’s just that working on windows is so much like work…
:slight_smile:

Bob Pendleton

See ya,
–Sam

On Tue, Jun 9, 2009 at 11:03 AM, Bob Pendleton <@Bob_Pendleton>wrote:

Ok, I applied the patch. Got everything to compile. Got it all to
install correctly. And ran testatomic. It all works on my Linux box so
I checked it in. No guarantees about any other platform. So, update
from svn and test it on everything. If you find a problem post patches
and we’ll get them in.

On Tue, Jun 9, 2009 at 11:16 AM, Donny Viszneki< donny.viszneki at gmail.com> wrote:

On Tue, Jun 9, 2009 at 9:04 AM, Bob Pendleton<@Bob_Pendleton> wrote:

On Tue, Jun 9, 2009 at 1:21 AM, Donny Viszneki< donny.viszneki at gmail.com> wrote:

xchg xchg xchg xchg xchg xchg xchg xchg …

so what platforms will that patch operate on?

Don’t know yet! What platforms are you interested in? Why don’t you
take a look too.

I did take a look! That’s what “xchg xchg xchg xchg” was all about!

Ya’know… I had not clue what that was about…

Bob Pendleton


http://codebad.com/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------

Bob, this broke building on Windows. There are a couple problems.

First, including windows.h indirectly in SDL.h breaks SDL_sysvideo.h, which
can be fixed, and potentially breaks application code that doesn’t compile
with windows.h included.

Second, at least on Visual C++ there are warnings about using the wrong
parameter types with the interlocked functions. These are correct, and even
though some care has been taken to make sure the parameters are the same
size, it makes me nervous.

Spent most of this afternoon reading the MSDN and GCC sections on atomic
operations. No, not even close to reading all the stuff on MSDN. GCC docs
were much more concise.

One with the code we now have is that it is intended to be inlined whenever
possible. This makes good sense for performance, not such good sense for
portability. That is where we get the problem with #including “windows.h” in
a header file. That is pretty much a no no. Another problem is that the
Windows functions are very picky about their argument sizes. As a fan of
strong typing I can hardly complain about that. Yet another problem is all
that assembly code. It made me nervous from the get go. AFAICT it isn’t
needed on any platfrom supported by Windows or by GCC.

GCC: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html

MSDN: http://msdn.microsoft.com/en-us/library/ms686353(VS.85).aspx

I think we need to go back to basics on this part of 1.3.

It looks like the size is going to have to be part of the function names, so
if we want to support atomic ops on 8,16,32, and 64 bit fields we’re going
to have 4 versions of each function.

We need to look at inlining. Looks to me like we can maybe get away with
#define-ing the functions to map to the equivalent fuctions for each
platform. This does assume that things like windows.h will get included
"somewhere" before the #define-ed code is used. This may not be possible and
the functions may have to be defined completely outside of the header files.
I need input on this topic. The easiest way to implement this is out of
line, but that does cost some performance.

It looks like there is no one atomic operation you can count on having on
every processor type. At the least you get either an atomic test-and-set
used to acquire a lock along with an atomic store used to release a lock or
an atomic exchange that can be used to implement the test-and-set/relelase
operations. After that you get into the whole set of
fetch-and-op/op-and-fetch operations where op is a subset of (increment,
add, decrement, subtract, and, xor, nand…).

Given either exchange or test-and-set/release you can implement all the
others with a small loss of performance.

It looks to me like a reasonable set would be 8,16,32, and 64 bit operand
versions of:

test-and-set/release
exchange
fetch-and-increment
fetch-and-add
fetch-and-decrement
fetch-and-subtract
increment-and-fetch
add-and-fetch
decrement-and-fetch
subtract-and-fetch

The difference between fetch-and-add and add-and-fetch is like the
difference between i++ and ++i.

test-and-set returns SDL_bool,
release returns void
all the rest return one of Uint8, Uint16, Uint32, Uint64

hmm, should probably add busy-wait that works with exchange or test-and-set.

Double hmm, don’t worry, the final names will not have “-” in them, they’ll
be SDL_TestAndSet() or something like that.

Ok, that’s my take on it, let me know what’s wrong with it.On Fri, Jun 12, 2009 at 3:46 AM, Sam Lantinga wrote:

See ya,
–Sam

On Tue, Jun 9, 2009 at 11:03 AM, Bob Pendleton <@Bob_Pendleton> wrote:

Ok, I applied the patch. Got everything to compile. Got it all to
install correctly. And ran testatomic. It all works on my Linux box so
I checked it in. No guarantees about any other platform. So, update
from svn and test it on everything. If you find a problem post patches
and we’ll get them in.

On Tue, Jun 9, 2009 at 11:16 AM, Donny Viszneki<donny.viszneki at gmail.com> wrote:

On Tue, Jun 9, 2009 at 9:04 AM, Bob Pendleton<@Bob_Pendleton> wrote:

On Tue, Jun 9, 2009 at 1:21 AM, Donny Viszneki< donny.viszneki at gmail.com> wrote:

xchg xchg xchg xchg xchg xchg xchg xchg …

so what platforms will that patch operate on?

Don’t know yet! What platforms are you interested in? Why don’t you
take a look too.

I did take a look! That’s what “xchg xchg xchg xchg” was all about!

Ya’know… I had not clue what that was about…

Bob Pendleton


http://codebad.com/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------

Here is info for MacOS. Interesting that they only support (AFAICT) 32 bit
and 64 bit operands. That means, that if we want to work across platforms we
can’t support 8 and 16 bit operands.

Mac OS X:
http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-SW14

I’m starting to think that these operations should be implemented as out of
line functions in SDL. Doing it that way avoids problems with forced
inclusion of os specific header files in user code and it appears to get rid
of memory barrier problems (I said appears…). All at the cost of a very
small reduction in performance.

Opinions? Flames? what do y’all think about this?

Bob PendletonOn Wed, Jun 17, 2009 at 7:50 PM, Bob Pendleton <@Bob_Pendleton> wrote:

On Fri, Jun 12, 2009 at 3:46 AM, Sam Lantinga wrote:

Bob, this broke building on Windows. There are a couple problems.

First, including windows.h indirectly in SDL.h breaks SDL_sysvideo.h,
which can be fixed, and potentially breaks application code that doesn’t
compile with windows.h included.

Second, at least on Visual C++ there are warnings about using the wrong
parameter types with the interlocked functions. These are correct, and even
though some care has been taken to make sure the parameters are the same
size, it makes me nervous.

Spent most of this afternoon reading the MSDN and GCC sections on atomic
operations. No, not even close to reading all the stuff on MSDN. GCC docs
were much more concise.

One with the code we now have is that it is intended to be inlined whenever
possible. This makes good sense for performance, not such good sense for
portability. That is where we get the problem with #including “windows.h” in
a header file. That is pretty much a no no. Another problem is that the
Windows functions are very picky about their argument sizes. As a fan of
strong typing I can hardly complain about that. Yet another problem is all
that assembly code. It made me nervous from the get go. AFAICT it isn’t
needed on any platfrom supported by Windows or by GCC.

GCC: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html

MSDN: http://msdn.microsoft.com/en-us/library/ms686353(VS.85).aspxhttp://msdn.microsoft.com/en-us/library/ms686353(VS.85).aspx

I think we need to go back to basics on this part of 1.3.

It looks like the size is going to have to be part of the function names,
so if we want to support atomic ops on 8,16,32, and 64 bit fields we’re
going to have 4 versions of each function.

We need to look at inlining. Looks to me like we can maybe get away with
#define-ing the functions to map to the equivalent fuctions for each
platform. This does assume that things like windows.h will get included
"somewhere" before the #define-ed code is used. This may not be possible and
the functions may have to be defined completely outside of the header files.
I need input on this topic. The easiest way to implement this is out of
line, but that does cost some performance.

It looks like there is no one atomic operation you can count on having on
every processor type. At the least you get either an atomic test-and-set
used to acquire a lock along with an atomic store used to release a lock or
an atomic exchange that can be used to implement the test-and-set/relelase
operations. After that you get into the whole set of
fetch-and-op/op-and-fetch operations where op is a subset of (increment,
add, decrement, subtract, and, xor, nand…).

Given either exchange or test-and-set/release you can implement all the
others with a small loss of performance.

It looks to me like a reasonable set would be 8,16,32, and 64 bit operand
versions of:

test-and-set/release
exchange
fetch-and-increment
fetch-and-add
fetch-and-decrement
fetch-and-subtract
increment-and-fetch
add-and-fetch
decrement-and-fetch
subtract-and-fetch

The difference between fetch-and-add and add-and-fetch is like the
difference between i++ and ++i.

test-and-set returns SDL_bool,
release returns void
all the rest return one of Uint8, Uint16, Uint32, Uint64

hmm, should probably add busy-wait that works with exchange or
test-and-set.

Double hmm, don’t worry, the final names will not have “-” in them, they’ll
be SDL_TestAndSet() or something like that.

Ok, that’s my take on it, let me know what’s wrong with it.

See ya,
–Sam

On Tue, Jun 9, 2009 at 11:03 AM, Bob Pendleton <@Bob_Pendleton> wrote:

Ok, I applied the patch. Got everything to compile. Got it all to
install correctly. And ran testatomic. It all works on my Linux box so
I checked it in. No guarantees about any other platform. So, update
from svn and test it on everything. If you find a problem post patches
and we’ll get them in.

On Tue, Jun 9, 2009 at 11:16 AM, Donny Viszneki<donny.viszneki at gmail.com> wrote:

On Tue, Jun 9, 2009 at 9:04 AM, Bob Pendleton<@Bob_Pendleton> wrote:

On Tue, Jun 9, 2009 at 1:21 AM, Donny Viszneki< donny.viszneki at gmail.com> wrote:

xchg xchg xchg xchg xchg xchg xchg xchg …

so what platforms will that patch operate on?

Don’t know yet! What platforms are you interested in? Why don’t you
take a look too.

I did take a look! That’s what “xchg xchg xchg xchg” was all about!

Ya’know… I had not clue what that was about…

Bob Pendleton


http://codebad.com/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------


±----------------------------------------------------------

Mac OS X:
http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-SW14

You should use those on PowerPC builds, because it’s hard to get it
right in assembly (and possibly impossible across all PowerPC chips).

But on Intel chips, you might as well just use the same "lock ; xchg"
opcodes everyone else does, instead of relying on the OS.

–ryan.

There are easy ways to design around this limitation.On Thu, Jun 18, 2009 at 10:24 AM, Bob Pendleton wrote:

Here is info for MacOS. Interesting that they only support (AFAICT) 32 bit
and 64 bit operands. That means, that if we want to work across platforms we
can’t support 8 and 16 bit operands.


http://codebad.com/

Yeah, this all seems reasonable. Patch away! :)On Thu, Jun 18, 2009 at 7:24 AM, Bob Pendleton wrote:

Here is info for MacOS. Interesting that they only support (AFAICT) 32 bit
and 64 bit operands. That means, that if we want to work across platforms we
can’t support 8 and 16 bit operands.

Mac OS X:
http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-SW14

I’m starting to think that these operations should be implemented as out of
line functions in SDL. Doing it that way avoids problems with forced
inclusion of os specific header files in user code and it appears to get rid
of memory barrier problems (I said appears…). All at the cost of a very
small reduction in performance.

Opinions? Flames? what do y’all think about this?

Bob Pendleton

On Wed, Jun 17, 2009 at 7:50 PM, Bob Pendleton wrote:

On Fri, Jun 12, 2009 at 3:46 AM, Sam Lantinga <@slouken> wrote:

Bob, this broke building on Windows. There are a couple problems.

First, including windows.h indirectly in SDL.h breaks SDL_sysvideo.h,
which can be fixed, and potentially breaks application code that doesn’t
compile with windows.h included.

Second, at least on Visual C++ there are warnings about using the wrong
parameter types with the interlocked functions. These are correct, and even
though some care has been taken to make sure the parameters are the same
size, it makes me nervous.

Spent most of this afternoon reading the MSDN and GCC sections on atomic
operations. No, not even close to reading all the stuff on MSDN. GCC docs
were much more concise.

One with the code we now have is that it is intended to be inlined
whenever possible. This makes good sense for performance, not such good
sense for portability. That is where we get the problem with #including
"windows.h" in a header file. That is pretty much a no no. Another problem
is that the Windows functions are very picky about their argument sizes. As
a fan of strong typing I can hardly complain about that. Yet another problem
is all that assembly code. It made me nervous from the get go. AFAICT it
isn’t needed on any platfrom supported by Windows or by GCC.

GCC: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html

MSDN: http://msdn.microsoft.com/en-us/library/ms686353(VS.85).aspxhttp://msdn.microsoft.com/en-us/library/ms686353(VS.85).aspx

I think we need to go back to basics on this part of 1.3.

It looks like the size is going to have to be part of the function names,
so if we want to support atomic ops on 8,16,32, and 64 bit fields we’re
going to have 4 versions of each function.

We need to look at inlining. Looks to me like we can maybe get away with
#define-ing the functions to map to the equivalent fuctions for each
platform. This does assume that things like windows.h will get included
"somewhere" before the #define-ed code is used. This may not be possible and
the functions may have to be defined completely outside of the header files.
I need input on this topic. The easiest way to implement this is out of
line, but that does cost some performance.

It looks like there is no one atomic operation you can count on having on
every processor type. At the least you get either an atomic test-and-set
used to acquire a lock along with an atomic store used to release a lock or
an atomic exchange that can be used to implement the test-and-set/relelase
operations. After that you get into the whole set of
fetch-and-op/op-and-fetch operations where op is a subset of (increment,
add, decrement, subtract, and, xor, nand…).

Given either exchange or test-and-set/release you can implement all the
others with a small loss of performance.

It looks to me like a reasonable set would be 8,16,32, and 64 bit operand
versions of:

test-and-set/release
exchange
fetch-and-increment
fetch-and-add
fetch-and-decrement
fetch-and-subtract
increment-and-fetch
add-and-fetch
decrement-and-fetch
subtract-and-fetch

The difference between fetch-and-add and add-and-fetch is like the
difference between i++ and ++i.

test-and-set returns SDL_bool,
release returns void
all the rest return one of Uint8, Uint16, Uint32, Uint64

hmm, should probably add busy-wait that works with exchange or
test-and-set.

Double hmm, don’t worry, the final names will not have “-” in them,
they’ll be SDL_TestAndSet() or something like that.

Ok, that’s my take on it, let me know what’s wrong with it.

See ya,
–Sam

On Tue, Jun 9, 2009 at 11:03 AM, Bob Pendleton wrote:

Ok, I applied the patch. Got everything to compile. Got it all to
install correctly. And ran testatomic. It all works on my Linux box so
I checked it in. No guarantees about any other platform. So, update
from svn and test it on everything. If you find a problem post patches
and we’ll get them in.

On Tue, Jun 9, 2009 at 11:16 AM, Donny Viszneki< donny.viszneki at gmail.com> wrote:

On Tue, Jun 9, 2009 at 9:04 AM, Bob Pendleton wrote:

On Tue, Jun 9, 2009 at 1:21 AM, Donny Viszneki< donny.viszneki at gmail.com> wrote:

xchg xchg xchg xchg xchg xchg xchg xchg …

so what platforms will that patch operate on?

Don’t know yet! What platforms are you interested in? Why don’t you
take a look too.

I did take a look! That’s what “xchg xchg xchg xchg” was all about!

Ya’know… I had not clue what that was about…

Bob Pendleton


http://codebad.com/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


-Sam Lantinga, Founder and President, Galaxy Gameworks LLC

Here is info for MacOS. Interesting that they only support (AFAICT) 32
bit
and 64 bit operands. That means, that if we want to work across platforms
we
can’t support 8 and 16 bit operands.

There are easy ways to design around this limitation.

Donny, I tell us what you are thinking? I do not want to get into this and
find out you, like Fermat, have a simple solution that will not fit in a one
line email post. :slight_smile:

Bob PendletonOn Thu, Jun 18, 2009 at 8:34 PM, Donny Viszneki <donny.viszneki at gmail.com>wrote:

On Thu, Jun 18, 2009 at 10:24 AM, Bob Pendleton<@Bob_Pendleton> wrote:


http://codebad.com/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------

Yeah, this all seems reasonable. Patch away! :slight_smile:

OK… you asked for it :slight_smile:

Seriously, I can build a new .h and add a .c file with function definitions.
I’ll even redo the test program. I’ll sketch out the Windows and Mac code,
but, I will not be able to compile the Mac code, and maybe not the Windows
code. So, I’ll check in the GCC code and circulate the rest on the list for
other people to compile and test. When I have that code nailed down, I’ll
check it in.

Bob PendletonOn Thu, Jun 18, 2009 at 10:35 PM, Sam Lantinga wrote:

On Thu, Jun 18, 2009 at 7:24 AM, Bob Pendleton <@Bob_Pendleton> wrote:

Here is info for MacOS. Interesting that they only support (AFAICT) 32 bit
and 64 bit operands. That means, that if we want to work across platforms we
can’t support 8 and 16 bit operands.

Mac OS X:
http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-SW14

I’m starting to think that these operations should be implemented as out
of line functions in SDL. Doing it that way avoids problems with forced
inclusion of os specific header files in user code and it appears to get rid
of memory barrier problems (I said appears…). All at the cost of a very
small reduction in performance.

Opinions? Flames? what do y’all think about this?

Bob Pendleton

On Wed, Jun 17, 2009 at 7:50 PM, Bob Pendleton <@Bob_Pendleton> wrote:

On Fri, Jun 12, 2009 at 3:46 AM, Sam Lantinga wrote:

Bob, this broke building on Windows. There are a couple problems.

First, including windows.h indirectly in SDL.h breaks SDL_sysvideo.h,
which can be fixed, and potentially breaks application code that doesn’t
compile with windows.h included.

Second, at least on Visual C++ there are warnings about using the wrong
parameter types with the interlocked functions. These are correct, and even
though some care has been taken to make sure the parameters are the same
size, it makes me nervous.

Spent most of this afternoon reading the MSDN and GCC sections on atomic
operations. No, not even close to reading all the stuff on MSDN. GCC docs
were much more concise.

One with the code we now have is that it is intended to be inlined
whenever possible. This makes good sense for performance, not such good
sense for portability. That is where we get the problem with #including
"windows.h" in a header file. That is pretty much a no no. Another problem
is that the Windows functions are very picky about their argument sizes. As
a fan of strong typing I can hardly complain about that. Yet another problem
is all that assembly code. It made me nervous from the get go. AFAICT it
isn’t needed on any platfrom supported by Windows or by GCC.

GCC: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html

MSDN: http://msdn.microsoft.com/en-us/library/ms686353(VS.85).aspxhttp://msdn.microsoft.com/en-us/library/ms686353(VS.85).aspx

I think we need to go back to basics on this part of 1.3.

It looks like the size is going to have to be part of the function names,
so if we want to support atomic ops on 8,16,32, and 64 bit fields we’re
going to have 4 versions of each function.

We need to look at inlining. Looks to me like we can maybe get away with
#define-ing the functions to map to the equivalent fuctions for each
platform. This does assume that things like windows.h will get included
"somewhere" before the #define-ed code is used. This may not be possible and
the functions may have to be defined completely outside of the header files.
I need input on this topic. The easiest way to implement this is out of
line, but that does cost some performance.

It looks like there is no one atomic operation you can count on having on
every processor type. At the least you get either an atomic test-and-set
used to acquire a lock along with an atomic store used to release a lock or
an atomic exchange that can be used to implement the test-and-set/relelase
operations. After that you get into the whole set of
fetch-and-op/op-and-fetch operations where op is a subset of (increment,
add, decrement, subtract, and, xor, nand…).

Given either exchange or test-and-set/release you can implement all the
others with a small loss of performance.

It looks to me like a reasonable set would be 8,16,32, and 64 bit operand
versions of:

test-and-set/release
exchange
fetch-and-increment
fetch-and-add
fetch-and-decrement
fetch-and-subtract
increment-and-fetch
add-and-fetch
decrement-and-fetch
subtract-and-fetch

The difference between fetch-and-add and add-and-fetch is like the
difference between i++ and ++i.

test-and-set returns SDL_bool,
release returns void
all the rest return one of Uint8, Uint16, Uint32, Uint64

hmm, should probably add busy-wait that works with exchange or
test-and-set.

Double hmm, don’t worry, the final names will not have “-” in them,
they’ll be SDL_TestAndSet() or something like that.

Ok, that’s my take on it, let me know what’s wrong with it.

See ya,
–Sam

On Tue, Jun 9, 2009 at 11:03 AM, Bob Pendleton <@Bob_Pendleton>wrote:

Ok, I applied the patch. Got everything to compile. Got it all to
install correctly. And ran testatomic. It all works on my Linux box so
I checked it in. No guarantees about any other platform. So, update
from svn and test it on everything. If you find a problem post patches
and we’ll get them in.

On Tue, Jun 9, 2009 at 11:16 AM, Donny Viszneki< donny.viszneki at gmail.com> wrote:

On Tue, Jun 9, 2009 at 9:04 AM, Bob Pendleton<@Bob_Pendleton> wrote:

On Tue, Jun 9, 2009 at 1:21 AM, Donny Viszneki< donny.viszneki at gmail.com> wrote:

xchg xchg xchg xchg xchg xchg xchg xchg …

so what platforms will that patch operate on?

Don’t know yet! What platforms are you interested in? Why don’t you
take a look too.

I did take a look! That’s what “xchg xchg xchg xchg” was all about!

Ya’know… I had not clue what that was about…

Bob Pendleton


http://codebad.com/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------


±----------------------------------------------------------


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


-Sam Lantinga, Founder and President, Galaxy Gameworks LLC


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


±----------------------------------------------------------

Here is info for MacOS. Interesting that they only support (AFAICT) 32
bit
and 64 bit operands. That means, that if we want to work across
platforms we
can’t support 8 and 16 bit operands.

There are easy ways to design around this limitation.

Donny, I tell us what you are thinking? I do not want to get into this and
find out you, like Fermat, have a simple solution that will not fit in a one
line email post. :slight_smile:

Yeah, I’m especially curious to find out how multiple threads can
manipulate an array of uint8_t safely with only 32 bit hardware atomic
operands…On Fri, Jun 19, 2009 at 1:35 PM, Bob Pendleton wrote:


http://pphaneuf.livejournal.com/