Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

[linux-port] Thread-safe increment/decrement - #201

Merged
ehsannas merged 1 commit into
google:linuxfrom
pow2clk:pow2clk-fixup2
May 24, 2018
Merged

ehsannas merged 1 commit into
google:linuxfrom
pow2clk:pow2clk-fixup2

Conversation

@pow2clk

@pow2clk pow2clk commented May 22, 2018 •

Copy link
Copy Markdown

InterlockedIncrement and InterlockedDecrement are atomic operations to
increment or decrement the value pointed to by the pointer parameter and
return the same. Currently they are implemented as simple addition
operations in WinMacros.h for non-win32 platforms which works well
enough, but wasn't really the original intent.

There are already platform abstractions available to LLVM in Atomics.h.
This change leverages them to implement the same functionality in a
platform-agnostic way. Mostly it's just a matter of swapping
InterlockedIncrement for AtomicIncrement.

Fixes half of #198

@pow2clk

pow2clk commented May 22, 2018

Copy link
Copy Markdown
Author

It's worth noting that the critical section changes to LLVM code actually revert changes made as part of the HLSL project for reasons poorly specified in the original change especially since none of their code made use of the functions in question.

@AppVeyorBot

Copy link
Copy Markdown

@AppVeyorBot

Copy link
Copy Markdown

InterlockedIncrement and InterlockedDecrement are atomic operations to
increment or decrement the value pointed to by the pointer parameter and
return the same. Currently they are implemented as simple addition
operations in WinMacros.h for non-win32 platforms which works well
enough, but wasn't really the original intent.

There are already platform abstractions available to LLVM in Atomics.h.
This change leverages them to implement the same functionality in a
platform-agnostic way. Mostly it's just a matter of swapping
InterlockedIncrement for AtomicIncrement.

Fixes half of google#198
@pow2clk

pow2clk commented May 23, 2018

Copy link
Copy Markdown
Author

I backed away from the critical section portion for now. There are some complications on windows and linux regarding how everything is initialized and allocated. It could be fixed by just making everything static, but that's a bit ugly and invasive in the LLVM code. I'm going to poke at the startup mechanism on each a bit more to see if there is a better solution. In the meantime, I can stop tripping over these changes.

@pow2clk pow2clk changed the title [linux-port] Implement thread-safe functionality [linux-port] Thread-safe increment/decrement May 23, 2018
@AppVeyorBot

Copy link
Copy Markdown

@ehsannas ehsannas left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I'm hoping MS is open to using llvm::sys::AtomicIncrement. I think it's reasonable.



// TODO (ehsann): These are not atomic.
#define InterlockedIncrement(x) ++*x

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

happy to see this removed! :)

@pow2clk

pow2clk commented May 24, 2018

Copy link
Copy Markdown
Author

I hope they are too. That's the big unknown. The same potential for turmoil will be present with the followup for critical sections. It looks like they tried to make the common code work there but gave up in the end. So I consider that an encouraging data point.

@antiagainst antiagainst left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool stuff! I think this one is self-contained enough that we can also open a pull request against upstream. llvm::sys::Atomic*() is merely wrapping around Interlocked*() on Windows, so it should be okay.

@ehsannas
ehsannas merged commit 069f18f into google:linux May 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants