From 09b7f90475006c58139da5b959b6ea3f47c0e5c1 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 23 Nov 2022 03:39:30 +0800 Subject: [PATCH] move `system/atomics` out of system; `std/atomics` should be preferred (#20875) * move `system/atomics` out of system; `std/atomics` should be preferred * add deprecation message * fixes * fixes * fixes * fixes more tests --- changelog.md | 1 + lib/pure/concurrency/threadpool.nim | 2 +- lib/pure/nimprof.nim | 2 +- lib/pure/oids.nim | 3 +++ .../atomics.nim => std/sysatomics.nim} | 10 +++++++-- lib/system.nim | 21 +++++++------------ lib/system/alloc.nim | 1 + 7 files changed, 23 insertions(+), 17 deletions(-) rename lib/{system/atomics.nim => std/sysatomics.nim} (97%) diff --git a/changelog.md b/changelog.md index ba52eff8af..80af02512b 100644 --- a/changelog.md +++ b/changelog.md @@ -16,6 +16,7 @@ - `std/objectdollar` - `std/widestrs` - `std/typedthreads` + - `std/sysatomics` In the future, these definitions will be removed from the `system` module, and their respective modules will have to be imported to use them. diff --git a/lib/pure/concurrency/threadpool.nim b/lib/pure/concurrency/threadpool.nim index dcc917225e..0eb20dc9a7 100644 --- a/lib/pure/concurrency/threadpool.nim +++ b/lib/pure/concurrency/threadpool.nim @@ -23,7 +23,7 @@ when not compileOption("threads"): import cpuinfo, cpuload, locks, os when defined(nimPreviewSlimSystem): - import std/[assertions, typedthreads] + import std/[assertions, typedthreads, sysatomics] {.push stackTrace:off.} diff --git a/lib/pure/nimprof.nim b/lib/pure/nimprof.nim index fe497c6450..7f493418e9 100644 --- a/lib/pure/nimprof.nim +++ b/lib/pure/nimprof.nim @@ -24,7 +24,7 @@ when defined(nimHasUsed): import hashes, algorithm, strutils, tables, sets when defined(nimPreviewSlimSystem): - import std/syncio + import std/[syncio, sysatomics] when not defined(memProfiler): include "system/timers" diff --git a/lib/pure/oids.nim b/lib/pure/oids.nim index 486e01ea31..e6e5e6e564 100644 --- a/lib/pure/oids.nim +++ b/lib/pure/oids.nim @@ -17,6 +17,9 @@ import hashes, times, endians, random from std/private/decode_helpers import handleHexChar +when defined(nimPreviewSlimSystem): + import std/sysatomics + type Oid* = object ## An OID. time: int64 diff --git a/lib/system/atomics.nim b/lib/std/sysatomics.nim similarity index 97% rename from lib/system/atomics.nim rename to lib/std/sysatomics.nim index 61a60fa530..ddd8746c68 100644 --- a/lib/system/atomics.nim +++ b/lib/std/sysatomics.nim @@ -7,9 +7,14 @@ # distribution, for details about the copyright. # +when defined(nimPreviewSlimSystem): + {.deprecated: "use `std/atomics` instead".} + # Atomic operations for Nim. {.push stackTrace:off, profiler:off.} +const + hasThreadSupport = compileOption("threads") and not defined(nimscript) const someGcc = defined(gcc) or defined(llvm_gcc) or defined(clang) const someVcc = defined(vcc) or defined(clang_cl) @@ -215,7 +220,8 @@ else: inc(p[], val) result = p[] -proc atomicInc*(memLoc: var int, x: int = 1): int = + +proc atomicInc*(memLoc: var int, x: int = 1): int {.inline, discardable, raises: [], tags: [].} = when someGcc and hasThreadSupport: result = atomicAddFetch(memLoc.addr, x, ATOMIC_SEQ_CST) elif someVcc and hasThreadSupport: @@ -225,7 +231,7 @@ proc atomicInc*(memLoc: var int, x: int = 1): int = inc(memLoc, x) result = memLoc -proc atomicDec*(memLoc: var int, x: int = 1): int = +proc atomicDec*(memLoc: var int, x: int = 1): int {.inline, discardable, raises: [], tags: [].} = when someGcc and hasThreadSupport: when declared(atomicSubFetch): result = atomicSubFetch(memLoc.addr, x, ATOMIC_SEQ_CST) diff --git a/lib/system.nim b/lib/system.nim index ae07e58f3c..c41b3cb9d3 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -1394,13 +1394,15 @@ when not defined(js) and not defined(booting) and defined(nimTrMacros): swap(cast[ptr pointer](addr arr[a])[], cast[ptr pointer](addr arr[b])[]) when not defined(nimscript): - proc atomicInc*(memLoc: var int, x: int = 1): int {.inline, - discardable, raises: [], tags: [], benign.} - ## Atomic increment of `memLoc`. Returns the value after the operation. + {.push stackTrace: off, profiler: off.} - proc atomicDec*(memLoc: var int, x: int = 1): int {.inline, - discardable, raises: [], tags: [], benign.} - ## Atomic decrement of `memLoc`. Returns the value after the operation. + when not defined(nimPreviewSlimSystem): + import std/sysatomics + export sysatomics + else: + import std/sysatomics + + {.pop.} include "system/memalloc" @@ -1617,13 +1619,6 @@ when not defined(nimscript): when not declared(sysFatal): include "system/fatal" -when not defined(nimscript): - {.push stackTrace: off, profiler: off.} - - include "system/atomics" - - {.pop.} - when defined(nimV2): include system/arc diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim index a99769725d..69bab5a85e 100644 --- a/lib/system/alloc.nim +++ b/lib/system/alloc.nim @@ -12,6 +12,7 @@ include osalloc import std/private/syslocks +import std/sysatomics template track(op, address, size) = when defined(memTracker):