summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2019-10-30 16:15:17 +0100
committerGitHub <noreply@github.com>2019-10-30 16:15:17 +0100
commit1746da2d9e3d802d990d9636d2f6887a4aeb5dc9 (patch)
tree687727e8d4b858eb74db2e07a1a778cff58a7bdd /lib
parentb5bb58164270a819cfb37655139251955541a964 (diff)
downloadNim-1746da2d9e3d802d990d9636d2f6887a4aeb5dc9.tar.gz
--gc:destructors now means Nim uses pure refcounting (#12557)
Diffstat (limited to 'lib')
-rw-r--r--lib/core/runtime_v2.nim22
-rw-r--r--lib/system.nim18
-rw-r--r--lib/system/hti.nim4
-rw-r--r--lib/system/mmdisp.nim4
-rw-r--r--lib/system/repr.nim2
-rw-r--r--lib/system/threads.nim2
6 files changed, 34 insertions, 18 deletions
diff --git a/lib/core/runtime_v2.nim b/lib/core/runtime_v2.nim
index 5c9554ad8..fe4bf37bf 100644
--- a/lib/core/runtime_v2.nim
+++ b/lib/core/runtime_v2.nim
@@ -57,13 +57,13 @@ proc nimNewObj(size: int): pointer {.compilerRtl.} =
   else:
     inc allocs
 
-proc nimDecWeakRef(p: pointer) {.compilerRtl.} =
+proc nimDecWeakRef(p: pointer) {.compilerRtl, inl.} =
   when hasThreadSupport:
     atomicDec head(p).rc
   else:
     dec head(p).rc
 
-proc nimIncWeakRef(p: pointer) {.compilerRtl.} =
+proc nimIncRef(p: pointer) {.compilerRtl, inl.} =
   when hasThreadSupport:
     atomicInc head(p).rc
   else:
@@ -106,11 +106,25 @@ proc nimDestroyAndDispose(p: pointer) {.compilerRtl.} =
       cstderr.rawWrite "has destructor!\n"
   nimRawDispose(p)
 
-proc isObj(obj: PNimType, subclass: cstring): bool {.compilerproc.} =
+proc nimDecRefIsLast(p: pointer): bool {.compilerRtl, inl.} =
+  if p != nil:
+    when hasThreadSupport:
+      if atomicLoadN(addr head(p).rc, ATOMIC_RELAXED) == 0:
+        result = true
+      else:
+        if atomicDec(head(p).rc) <= 0:
+          result = true
+    else:
+      if head(p).rc == 0:
+        result = true
+      else:
+        dec head(p).rc
+
+proc isObj(obj: PNimType, subclass: cstring): bool {.compilerRtl, inl.} =
   proc strstr(s, sub: cstring): cstring {.header: "<string.h>", importc.}
 
   result = strstr(obj.name, subclass) != nil
 
-proc chckObj(obj: PNimType, subclass: cstring) {.compilerproc.} =
+proc chckObj(obj: PNimType, subclass: cstring) {.compilerRtl.} =
   # checks if obj is of type subclass:
   if not isObj(obj, subclass): sysFatal(ObjectConversionError, "invalid object conversion")
diff --git a/lib/system.nim b/lib/system.nim
index 54ced6c7c..1485e4f6a 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -1722,12 +1722,12 @@ template `isnot`*(x, y: untyped): untyped = not (x is y)
   ##   assert 42 isnot float
   ##   assert @[1, 2] isnot enum
 
-when (defined(nimV2) and not defined(nimscript)) or defined(nimFixedOwned):
+when (defined(nimOwnedEnabled) and not defined(nimscript)) or defined(nimFixedOwned):
   type owned*[T]{.magic: "BuiltinType".} ## type constructor to mark a ref/ptr or a closure as `owned`.
 else:
   template owned*(t: typedesc): typedesc = t
 
-when defined(nimV2) and not defined(nimscript):
+when defined(nimOwnedEnabled) and not defined(nimscript):
   proc new*[T](a: var owned(ref T)) {.magic: "New", noSideEffect.}
     ## Creates a new object of type ``T`` and returns a safe (traced)
     ## reference to it in ``a``.
@@ -3237,6 +3237,8 @@ proc `<`*[T: tuple](x, y: T): bool =
 
 
 # ----------------- GC interface ---------------------------------------------
+const
+  usesDestructors = defined(gcDestructors) or defined(gcHooks)
 
 when not defined(nimscript) and hasAlloc:
   type
@@ -3246,7 +3248,7 @@ when not defined(nimscript) and hasAlloc:
       gcOptimizeTime,    ## optimize for speed
       gcOptimizeSpace    ## optimize for memory footprint
 
-  when not defined(JS) and not defined(gcDestructors):
+  when not defined(JS) and not usesDestructors:
     proc GC_disable*() {.rtl, inl, benign.}
       ## Disables the GC. If called `n` times, `n` calls to `GC_enable`
       ## are needed to reactivate the GC.
@@ -3602,7 +3604,7 @@ when not defined(JS): #and not defined(nimscript):
   {.push stack_trace: off, profiler:off.}
 
   when hasAlloc:
-    when not defined(gcRegions) and not defined(gcDestructors):
+    when not defined(gcRegions) and not usesDestructors:
       proc initGC() {.gcsafe.}
 
     proc initStackBottom() {.inline, compilerproc.} =
@@ -3620,7 +3622,7 @@ when not defined(JS): #and not defined(nimscript):
       when declared(nimGC_setStackBottom):
         nimGC_setStackBottom(locals)
 
-    when not defined(gcDestructors):
+    when not usesDestructors:
       {.push profiler: off.}
       var
         strDesc = TNimType(size: sizeof(string), kind: tyString, flags: {ntfAcyclic})
@@ -3792,10 +3794,10 @@ when not defined(JS): #and not defined(nimscript):
     when hasAlloc: include "system/strmantle"
 
     when hasThreadSupport:
-      when hostOS != "standalone" and not defined(gcDestructors): include "system/channels"
+      when hostOS != "standalone" and not usesDestructors: include "system/channels"
 
   when not defined(nimscript) and hasAlloc:
-    when not defined(gcDestructors):
+    when not usesDestructors:
       include "system/assign"
     when not defined(nimV2):
       include "system/repr"
@@ -4396,7 +4398,7 @@ proc locals*(): RootObj {.magic: "Plugin", noSideEffect.} =
   discard
 
 when hasAlloc and not defined(nimscript) and not defined(JS) and
-    not defined(gcDestructors):
+    not usesDestructors:
   # XXX how to implement 'deepCopy' is an open problem.
   proc deepCopy*[T](x: var T, y: T) {.noSideEffect, magic: "DeepCopy".} =
     ## Performs a deep copy of `y` and copies it into `x`.
diff --git a/lib/system/hti.nim b/lib/system/hti.nim
index b2e90211d..c20f132fb 100644
--- a/lib/system/hti.nim
+++ b/lib/system/hti.nim
@@ -87,7 +87,7 @@ type
     ntfEnumHole = 2    # enum has holes and thus `$` for them needs the slow
                        # version
   TNimType {.compilerproc.} = object
-    when defined(gcDestructors):
+    when defined(gcHooks):
       head*: pointer
     size*: int
     kind: TNimKind
@@ -103,7 +103,7 @@ type
       instances: int # count the number of instances
       sizes: int # sizes of all instances in bytes
 
-when defined(gcDestructors):
+when defined(gcHooks):
   type
     PNimType* = ptr TNimType
 else:
diff --git a/lib/system/mmdisp.nim b/lib/system/mmdisp.nim
index de89acd33..330c551c5 100644
--- a/lib/system/mmdisp.nim
+++ b/lib/system/mmdisp.nim
@@ -507,10 +507,10 @@ else:
   elif defined(gcRegions):
     # XXX due to bootstrapping reasons, we cannot use  compileOption("gc", "stack") here
     include "system/gc_regions"
-  elif defined(nimV2) or defined(gcDestructors):
+  elif defined(nimV2) or usesDestructors:
     var allocator {.rtlThreadVar.}: MemRegion
     instantiateForRegion(allocator)
-    when defined(gcDestructors):
+    when defined(gcHooks):
       include "system/gc_hooks"
   elif defined(gcMarkAndSweep):
     # XXX use 'compileOption' here
diff --git a/lib/system/repr.nim b/lib/system/repr.nim
index 0c7848c75..43bbc8bb6 100644
--- a/lib/system/repr.nim
+++ b/lib/system/repr.nim
@@ -226,7 +226,7 @@ when not defined(useNimRtl):
                cl: var ReprClosure) =
     # we know that p is not nil here:
     when declared(CellSet):
-      when defined(boehmGC) or defined(gogc) or defined(nogc) or defined(gcDestructors):
+      when defined(boehmGC) or defined(gogc) or defined(nogc) or usesDestructors:
         var cell = cast[PCell](p)
       else:
         var cell = usrToCell(p)
diff --git a/lib/system/threads.nim b/lib/system/threads.nim
index ad1d82be2..b09ed0c6f 100644
--- a/lib/system/threads.nim
+++ b/lib/system/threads.nim
@@ -148,7 +148,7 @@ else:
 proc threadProcWrapStackFrame[TArg](thrd: ptr Thread[TArg]) =
   when defined(boehmgc):
     boehmGC_call_with_stack_base(threadProcWrapDispatch[TArg], thrd)
-  elif not defined(nogc) and not defined(gogc) and not defined(gcRegions) and not defined(gcDestructors):
+  elif not defined(nogc) and not defined(gogc) and not defined(gcRegions) and not usesDestructors:
     var p {.volatile.}: proc(a: ptr Thread[TArg]) {.nimcall, gcsafe.} =
       threadProcWrapDispatch[TArg]
     # init the GC for refc/markandsweep