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

Deprecate atomics in favor of std/atomics, closes #35 #72

Merged
merged 5 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions threading/atomics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ runnableExamples("--threads:on"):
loc.atomicInc(1)
assert loc.load == 6

{.deprecated: "use `std/atomics` instead".}

when not compileOption("threads"):
{.error: "This module requires --threads:on compilation flag".}

Expand Down
25 changes: 13 additions & 12 deletions threading/channels.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ runnableExamples("--threads:on --gc:orc"):
when not (defined(gcArc) or defined(gcOrc) or defined(gcAtomicArc) or defined(nimdoc)):
{.error: "This module requires one of --mm:arc / --mm:atomicArc / --mm:orc compilation flags".}

import std/[locks, isolation]
import ./atomics
import std/[locks, isolation, atomics]
import system/ansi_c

# Channel
Expand All @@ -120,19 +119,19 @@ type

# ------------------------------------------------------------------------------

proc getTail(chan: ChannelRaw, order: Ordering = Relaxed): int {.inline.} =
proc getTail(chan: ChannelRaw, order: MemoryOrder = moRelaxed): int {.inline.} =
chan.tail.load(order)

proc getHead(chan: ChannelRaw, order: Ordering = Relaxed): int {.inline.} =
proc getHead(chan: ChannelRaw, order: MemoryOrder = moRelaxed): int {.inline.} =
chan.head.load(order)

proc setTail(chan: ChannelRaw, value: int, order: Ordering = Relaxed) {.inline.} =
proc setTail(chan: ChannelRaw, value: int, order: MemoryOrder = moRelaxed) {.inline.} =
chan.tail.store(value, order)

proc setHead(chan: ChannelRaw, value: int, order: Ordering = Relaxed) {.inline.} =
proc setHead(chan: ChannelRaw, value: int, order: MemoryOrder = moRelaxed) {.inline.} =
chan.head.store(value, order)

proc setAtomicCounter(chan: ChannelRaw, value: int, order: Ordering = Relaxed) {.inline.} =
proc setAtomicCounter(chan: ChannelRaw, value: int, order: MemoryOrder = moRelaxed) {.inline.} =
chan.atomicCounter.store(value, order)

proc numItems(chan: ChannelRaw): int {.inline.} =
Expand Down Expand Up @@ -262,9 +261,8 @@ template frees(c) =
if c.d != nil:
# this `fetchSub` returns current val then subs
# so count == 0 means we're the last
if c.d.atomicCounter.fetchSub(1, AcqRel) == 0:
if c.d.buffer != nil:
freeChannel(c.d)
if c.d.atomicCounter.fetchSub(1, moAcquireRelease) == 0:
freeChannel(c.d)

when defined(nimAllowNonVarDestructor):
proc `=destroy`*[T](c: Chan[T]) =
Expand All @@ -273,15 +271,18 @@ else:
proc `=destroy`*[T](c: var Chan[T]) =
frees(c)

proc `=wasMoved`*[T](x: var Chan[T]) =
x.d = nil

proc `=dup`*[T](src: Chan[T]): Chan[T] =
if src.d != nil:
discard fetchAdd(src.d.atomicCounter, 1, Relaxed)
discard fetchAdd(src.d.atomicCounter, 1, moRelaxed)
result.d = src.d

proc `=copy`*[T](dest: var Chan[T], src: Chan[T]) =
## Shares `Channel` by reference counting.
if src.d != nil:
discard fetchAdd(src.d.atomicCounter, 1, Relaxed)
discard fetchAdd(src.d.atomicCounter, 1, moRelaxed)
`=destroy`(dest)
dest.d = src.d

Expand Down
15 changes: 9 additions & 6 deletions threading/smartptrs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# distribution, for details about the copyright.

## C++11 like smart pointers. They always use the shared allocator.
import std/isolation, ./atomics
import std/[isolation, atomics]
from typetraits import supportsCopyMem

proc raiseNilAccess() {.noinline.} =
Expand Down Expand Up @@ -93,7 +93,7 @@ template frees(p) =
if p.val != nil:
# this `fetchSub` returns current val then subs
# so count == 0 means we're the last
if p.val.counter.fetchSub(1, AcqRel) == 0:
if p.val.counter.fetchSub(1, moAcquireRelease) == 0:
`=destroy`(p.val.value)
deallocShared(p.val)

Expand All @@ -104,22 +104,25 @@ else:
proc `=destroy`*[T](p: var SharedPtr[T]) =
frees(p)

proc `=wasMoved`*[T](p: var SharedPtr[T]) =
p.val = nil

proc `=dup`*[T](src: SharedPtr[T]): SharedPtr[T] =
if src.val != nil:
discard fetchAdd(src.val.counter, 1, Relaxed)
discard fetchAdd(src.val.counter, 1, moRelaxed)
result.val = src.val

proc `=copy`*[T](dest: var SharedPtr[T], src: SharedPtr[T]) =
if src.val != nil:
discard fetchAdd(src.val.counter, 1, Relaxed)
discard fetchAdd(src.val.counter, 1, moRelaxed)
`=destroy`(dest)
dest.val = src.val

proc newSharedPtr*[T](val: sink Isolated[T]): SharedPtr[T] {.nodestroy.} =
## Returns a shared pointer which shares
## ownership of the object by reference counting.
result.val = cast[typeof(result.val)](allocShared(sizeof(result.val[])))
result.val.counter.store(0)
result.val.counter.store(0, moRelaxed)
result.val.value = extract val

template newSharedPtr*[T](val: T): SharedPtr[T] =
Expand All @@ -132,7 +135,7 @@ proc newSharedPtr*[T](t: typedesc[T]): SharedPtr[T] =
result.val = cast[typeof(result.val)](allocShared0(sizeof(result.val[])))
else:
result.val = cast[typeof(result.val)](allocShared(sizeof(result.val[])))
int(result.val.counter) = 0
result.val.counter.store(0, moRelaxed)

proc isNil*[T](p: SharedPtr[T]): bool {.inline.} =
p.val == nil
Expand Down
Loading