Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Make Arc decrefs and increfs atomic #88

Closed
wants to merge 4 commits into from
Closed

Conversation

shayanhabibi
Copy link
Collaborator

@shayanhabibi shayanhabibi commented Dec 1, 2021

As per the discussion on shared refs and arc, this fix will prevent any undefined behavior occuring.

The idea is to:

  • Have all atomic operations on ref count changes when threads:on

  • Make atomic operations opt-out using a Owned[T] type

  • Include a one file test that demonstrates the behavior before/after

Copy link
Contributor

@alaviss alaviss left a comment

Choose a reason for hiding this comment

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

I think we should not go with relaxed atomics until we can verify that everything works as expected.

@shayanhabibi
Copy link
Collaborator Author

The dec and incs must be ordered correctly; the load can be relaxed as it will not be able to pass any dec/incs due to their stricter ordering. The subsequent atomicdec being ordered prevents the load moving forward. Since the load is used as the condition for entering the block, should be aiight. Else I'll change it to acqrel

@alaviss
Copy link
Contributor

alaviss commented Dec 3, 2021

The subsequent atomicdec being ordered prevents the load moving forward.

I guess you haven't noticed this:

proc atomicDec*(memLoc: var int, x: int = 1): int =
when someGcc and hasThreadSupport:
when declared(atomicSubFetch):
result = atomicSubFetch(memLoc.addr, x, ATOMIC_RELAXED)
else:
result = atomicAddFetch(memLoc.addr, -x, ATOMIC_RELAXED)

@shayanhabibi
Copy link
Collaborator Author

F*** 🤣 I assumed seqcst is the default!

Hahaha I made an ass out of myself. Thank you bb ❤️ I'll fix it up

Copy link
Contributor

@disruptek disruptek left a comment

Choose a reason for hiding this comment

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

If you're going to touch every line in atomics why not improve it -- do we need to be exporting a distinct cint for AtomMemModel or can we make the API idiomatic, with an enum? These variables and constants are code smell; you can hide them in here with a converter if you're lazy. Also not a fan of the barrier template. Yikes.

Comment on lines +1809 to +1815
# proc atomicInc*(memLoc: var int, x: int = 1; order: AtomMemModel = ATOMIC_RELAXED): int {.inline,
# discardable, benign.}
# ## Atomic increment of `memLoc`. Returns the value after the operation.

# proc atomicDec*(memLoc: var int, x: int = 1; order: AtomMemModel = ATOMIC_RELAXED): int {.inline,
# discardable, benign.}
# ## Atomic decrement of `memLoc`. Returns the value after the operation.
Copy link
Contributor

Choose a reason for hiding this comment

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

Prefer when false: and document why these are removed.

@@ -116,15 +116,30 @@ proc nimNewObjUninit(size, alignment: int): pointer {.compilerRtl.} =
cprintf("[Allocated] %p result: %p\n", result -! sizeof(RefHeader), result)

proc nimDecWeakRef(p: pointer) {.compilerRtl, inl.} =
dec head(p).rc, rcIncrement
# We want to use atomic operations when threading is on
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a period.

Comment on lines +122 to +126
template decOpr(x,y: untyped): untyped =
when hasThreadSupport:
atomicDec(x, y, ATOMIC_ACQ_REL)
else:
dec(x, y)
Copy link
Contributor

Choose a reason for hiding this comment

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

Move it out and reuse it below. Add a comment or more descriptive name. Why not use typed?

Comment on lines +205 to +215
template loadOpr(x: untyped): untyped =
when hasThreadSupport:
atomicLoadN(x.addr, ATOMIC_ACQUIRE)
else:
x
template decOpr(x, y: untyped): untyped =
when hasThreadSupport:
atomicDec(x, y, ATOMIC_RELEASE)
else:
dec(x, y)

Copy link
Contributor

Choose a reason for hiding this comment

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

Comment or rename to be more descriptive.

@disruptek
Copy link
Contributor

Also, I would favor a single when hasThreadSupport: where you define two sets of templates; you can document the differences there and explain why they exist instead of scattering the conditionals in templates throughout the code.

@shayanhabibi
Copy link
Collaborator Author

If you're going to touch every line in atomics why not improve it -- do we need to be exporting a distinct cint for AtomMemModel or can we make the API idiomatic, with an enum? These variables and constants are code smell; you can hide them in here with a converter if you're lazy. Also not a fan of the barrier template. Yikes.

Bloody CLRF.

@disruptek disruptek closed this Jul 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants