summary refs log tree commit diff stats
path: root/lib/system/systhread.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/system/systhread.nim')
-rwxr-xr-xlib/system/systhread.nim26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/system/systhread.nim b/lib/system/systhread.nim
index 611191e70..68121661f 100755
--- a/lib/system/systhread.nim
+++ b/lib/system/systhread.nim
@@ -7,9 +7,35 @@
 #    distribution, for details about the copyright.
 #
 
+when defined(gcc) or defined(llvm_gcc):
+  proc sync_add_and_fetch(p: var int, val: int): int {.
+    importc: "__sync_add_and_fetch", nodecl.}
+  proc sync_sub_and_fetch(p: var int, val: int): int {.
+    importc: "__sync_sub_and_fetch", nodecl.}
+elif defined(vcc):
+  proc sync_add_and_fetch(p: var int, val: int): int {.
+    importc: "NimXadd", nodecl.}
+
 const
   isMultiThreaded* = true
   maxThreads = 256
+
+proc atomicInc(memLoc: var int, x: int): int =
+  when isMultiThreaded:
+    result = sync_add_and_fetch(memLoc, x)
+  else:
+    inc(memLoc, x)
+    result = memLoc
+  
+proc atomicDec(memLoc: var int, x: int): int =
+  when isMultiThreaded:
+    when defined(sync_sub_and_fetch):
+      result = sync_sub_and_fetch(memLoc, x)
+    else:
+      result = sync_add_and_fetch(memLoc, -x)
+  else:
+    dec(memLoc, x)
+    result = memLoc  
   
 type
   TThread* {.final, pure.} = object