summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2011-06-04 02:56:55 +0200
committerAraq <rumpf_a@web.de>2011-06-04 02:56:55 +0200
commit5008b44467bf545287289087a13f7e53c3d242ff (patch)
tree856090834d05969fc2722c5f3f3bd5df61f449f6 /lib
parent3260702a6044cdae89cf673ad1983aa3342127de (diff)
downloadNim-5008b44467bf545287289087a13f7e53c3d242ff.tar.gz
bugfixes for semantic checking; thread implementation pushed the compiler
Diffstat (limited to 'lib')
-rwxr-xr-xlib/pure/strutils.nim6
-rwxr-xr-xlib/system/alloc.nim4
-rwxr-xr-xlib/system/gc.nim52
-rwxr-xr-xlib/system/repr.nim6
-rwxr-xr-xlib/system/threads.nim75
5 files changed, 79 insertions, 64 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim
index 7ed224b67..8659c7d29 100755
--- a/lib/pure/strutils.nim
+++ b/lib/pure/strutils.nim
@@ -461,6 +461,12 @@ proc repeatChar*(count: int, c: Char = ' '): string {.noSideEffect,
   ## the character `c`.

   result = newString(count)

   for i in 0..count-1: result[i] = c

+
+proc repeatStr*(count: int, s: string): string {.noSideEffect,

+  rtl, extern: "nsuRepeatStr".} =

+  ## Returns `s` concatenated `count` times.

+  result = newStringOfCap(count*s.len)

+  for i in 0..count-1: result.add(s)

 

 proc align*(s: string, count: int): string {.

   noSideEffect, rtl, extern: "nsuAlignString".} =

diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim
index c12dc253a..0218e3baa 100755
--- a/lib/system/alloc.nim
+++ b/lib/system/alloc.nim
@@ -532,7 +532,7 @@ when not defined(useNimRtl):
     assert(not isAllocatedPtr(allocator, x))
 
   proc alloc(size: int): pointer =
-    when hasThreadSupport and hasSharedHeap: AquireSys(HeapLock)
+    when hasThreadSupport and hasSharedHeap: AcquireSys(HeapLock)
     result = unlockedAlloc(size)
     when hasThreadSupport and hasSharedHeap: ReleaseSys(HeapLock)
 
@@ -541,7 +541,7 @@ when not defined(useNimRtl):
     zeroMem(result, size)
 
   proc dealloc(p: pointer) =
-    when hasThreadSupport and hasSharedHeap: AquireSys(HeapLock)
+    when hasThreadSupport and hasSharedHeap: AcquireSys(HeapLock)
     unlockedDealloc(p)
     when hasThreadSupport and hasSharedHeap: ReleaseSys(HeapLock)
 
diff --git a/lib/system/gc.nim b/lib/system/gc.nim
index d276d6cec..d2a6b4b94 100755
--- a/lib/system/gc.nim
+++ b/lib/system/gc.nim
@@ -57,26 +57,20 @@ type
     decStack: TCellSeq       # cells in the stack that are to decref again
     cycleRoots: TCellSet
     tempStack: TCellSeq      # temporary stack for recursion elimination
+    recGcLock: int           # prevent recursion via finalizers; no thread lock
     stat: TGcStat
 
 var
   stackBottom {.rtlThreadVar.}: pointer
   gch {.rtlThreadVar.}: TGcHeap
   cycleThreshold {.rtlThreadVar.}: int = InitialCycleThreshold
-  recGcLock {.rtlThreadVar.}: int = 0
-    # we use a lock to prevent the garbage collector to be triggered in a
-    # finalizer; the collector should not call itself this way! Thus every
-    # object allocated by a finalizer will not trigger a garbage collection.
-    # This is wasteful but safe and won't ever be a problem for sane
-    # finalizers. This is a lock against recursive garbage collection, not a
-    # lock for threads!
-
-proc aquire(gch: var TGcHeap) {.inline.} = 
-  when hasThreadSupport:
-    AquireSys(HeapLock)
+
+proc acquire(gch: var TGcHeap) {.inline.} = 
+  when hasThreadSupport and hasSharedHeap:
+    AcquireSys(HeapLock)
 
 proc release(gch: var TGcHeap) {.inline.} = 
-  when hasThreadSupport:
+  when hasThreadSupport and hasSharedHeap:
     releaseSys(HeapLock)
 
 proc addZCT(s: var TCellSeq, c: PCell) {.noinline.} =
@@ -198,14 +192,14 @@ proc prepareDealloc(cell: PCell) =
     # collection. Since we are already collecting we
     # prevend recursive entering here by a lock.
     # XXX: we should set the cell's children to nil!
-    inc(recGcLock)
+    inc(gch.recGcLock)
     (cast[TFinalizer](cell.typ.finalizer))(cellToUsr(cell))
-    dec(recGcLock)
+    dec(gch.recGcLock)
 
 proc rtlAddCycleRoot(c: PCell) {.rtl, inl.} = 
   # we MUST access gch as a global here, because this crosses DLL boundaries!
   when hasThreadSupport:
-    AquireSys(HeapLock)
+    AcquireSys(HeapLock)
   incl(gch.cycleRoots, c)
   when hasThreadSupport:  
     ReleaseSys(HeapLock)
@@ -213,7 +207,7 @@ proc rtlAddCycleRoot(c: PCell) {.rtl, inl.} =
 proc rtlAddZCT(c: PCell) {.rtl, inl.} =
   # we MUST access gch as a global here, because this crosses DLL boundaries!
   when hasThreadSupport:
-    AquireSys(HeapLock)
+    AcquireSys(HeapLock)
   addZCT(gch.zct, c)
   when hasThreadSupport:
     ReleaseSys(HeapLock)
@@ -329,7 +323,7 @@ proc forAllChildren(cell: PCell, op: TWalkOp) =
 
 proc checkCollection {.inline.} =
   # checks if a collection should be done
-  if recGcLock == 0:
+  if gch.recGcLock == 0:
     collectCT(gch)
 
 proc addNewObjToZCT(res: PCell) {.inline.} =
@@ -378,7 +372,7 @@ proc addNewObjToZCT(res: PCell) {.inline.} =
 
 proc newObj(typ: PNimType, size: int): pointer {.compilerRtl.} =
   # generates a new object and sets its reference counter to 0
-  aquire(gch)
+  acquire(gch)
   assert(typ.kind in {tyRef, tyString, tySequence})
   checkCollection()
   var res = cast[PCell](rawAlloc(allocator, size + sizeof(TCell)))
@@ -406,7 +400,7 @@ proc newSeq(typ: PNimType, len: int): pointer {.compilerRtl.} =
   cast[PGenericSeq](result).space = len
 
 proc growObj(old: pointer, newsize: int): pointer {.rtl.} =
-  aquire(gch)
+  acquire(gch)
   checkCollection()
   var ol = usrToCell(old)
   assert(ol.typ != nil)
@@ -521,13 +515,15 @@ proc gcMark(p: pointer) {.inline.} =
       add(gch.decStack, cell)
 
 proc markThreadStacks(gch: var TGcHeap) = 
-  when hasThreadSupport:
+  when hasThreadSupport and hasSharedHeap:
+    {.error: "not fully implemented".}
     var it = threadList
     while it != nil:
       # mark registers: 
       for i in 0 .. high(it.registers): gcMark(it.registers[i])
       var sp = cast[TAddress](it.stackBottom)
       var max = cast[TAddress](it.stackTop)
+      # XXX stack direction?
       # XXX unroll this loop:
       while sp <=% max:
         gcMark(cast[ppointer](sp)[])
@@ -696,7 +692,7 @@ proc unmarkStackAndRegisters(gch: var TGcHeap) =
   var d = gch.decStack.d
   for i in 0..gch.decStack.len-1:
     assert isAllocatedPtr(allocator, d[i])
-    # decRef(d[i]) inlined: cannot create a cycle and must not aquire lock
+    # decRef(d[i]) inlined: cannot create a cycle and must not acquire lock
     var c = d[i]
     # XXX no need for an atomic dec here:
     if --c.refcount:
@@ -725,9 +721,17 @@ proc collectCT(gch: var TGcHeap) =
     unmarkStackAndRegisters(gch)
 
 when not defined(useNimRtl):
-  proc GC_disable() = discard atomicInc(recGcLock, 1)
+  proc GC_disable() = 
+    when hasThreadSupport and hasSharedHeap:
+      discard atomicInc(gch.recGcLock, 1)
+    else:
+      inc(gch.recGcLock)
   proc GC_enable() =
-    if recGcLock > 0: discard atomicDec(recGcLock, 1)
+    if gch.recGcLock > 0: 
+      when hasThreadSupport and hasSharedHeap:
+        discard atomicDec(gch.recGcLock, 1)
+      else:
+        dec(gch.recGcLock)
 
   proc GC_setStrategy(strategy: TGC_Strategy) =
     case strategy
@@ -744,7 +748,7 @@ when not defined(useNimRtl):
     # set to the max value to suppress the cycle detector
 
   proc GC_fullCollect() =
-    aquire(gch)
+    acquire(gch)
     var oldThreshold = cycleThreshold
     cycleThreshold = 0 # forces cycle collection
     collectCT(gch)
diff --git a/lib/system/repr.nim b/lib/system/repr.nim
index d9997001d..add0af10c 100755
--- a/lib/system/repr.nim
+++ b/lib/system/repr.nim
@@ -118,14 +118,16 @@ when not defined(useNimRtl):
   proc initReprClosure(cl: var TReprClosure) =
     # Important: cellsets does not lock the heap when doing allocations! We
     # have to do it here ...
-    when hasThreadSupport and defined(heapLock): AquireSys(HeapLock)
+    when hasThreadSupport and hasSharedHeap and defined(heapLock):
+      AcquireSys(HeapLock)
     Init(cl.marked)
     cl.recdepth = -1      # default is to display everything!
     cl.indent = 0
 
   proc deinitReprClosure(cl: var TReprClosure) =
     Deinit(cl.marked)
-    when hasThreadSupport and defined(heapLock): ReleaseSys(HeapLock)
+    when hasThreadSupport and hasSharedHeap and defined(heapLock): 
+      ReleaseSys(HeapLock)
 
   proc reprBreak(result: var string, cl: TReprClosure) =
     add result, "\n"
diff --git a/lib/system/threads.nim b/lib/system/threads.nim
index 052335baa..c5264e4e2 100755
--- a/lib/system/threads.nim
+++ b/lib/system/threads.nim
@@ -27,7 +27,7 @@
 ##  
 ##  proc threadFunc(interval: tuple[a,b: int]) {.procvar.} = 
 ##    for i in interval.a..interval.b: 
-##      Aquire(L) # lock stdout
+##      Acquire(L) # lock stdout
 ##      echo i
 ##      Release(L)
 ##
@@ -56,16 +56,16 @@ when defined(Windows):
     dynlib: "kernel32", importc: "InitializeCriticalSection".}
     ## Initializes the lock `L`.
 
-  proc TryAquireSysAux(L: var TSysLock): int32 {.stdcall,
+  proc TryAcquireSysAux(L: var TSysLock): int32 {.stdcall,
     dynlib: "kernel32", importc: "TryEnterCriticalSection".}
-    ## Tries to aquire the lock `L`.
+    ## Tries to acquire the lock `L`.
     
-  proc TryAquireSys(L: var TSysLock): bool {.inline.} = 
-    result = TryAquireSysAux(L) != 0'i32
+  proc TryAcquireSys(L: var TSysLock): bool {.inline.} = 
+    result = TryAcquireSysAux(L) != 0'i32
 
-  proc AquireSys(L: var TSysLock) {.stdcall,
+  proc AcquireSys(L: var TSysLock) {.stdcall,
     dynlib: "kernel32", importc: "EnterCriticalSection".}
-    ## Aquires the lock `L`.
+    ## Acquires the lock `L`.
     
   proc ReleaseSys(L: var TSysLock) {.stdcall,
     dynlib: "kernel32", importc: "LeaveCriticalSection".}
@@ -131,13 +131,13 @@ else:
   proc InitSysLock(L: var TSysLock, attr: pointer = nil) {.
     importc: "pthread_mutex_init", header: "<pthread.h>".}
 
-  proc AquireSys(L: var TSysLock) {.
+  proc AcquireSys(L: var TSysLock) {.
     importc: "pthread_mutex_lock", header: "<pthread.h>".}
-  proc TryAquireSysAux(L: var TSysLock): cint {.
+  proc TryAcquireSysAux(L: var TSysLock): cint {.
     importc: "pthread_mutex_trylock", header: "<pthread.h>".}
 
-  proc TryAquireSys(L: var TSysLock): bool {.inline.} = 
-    result = TryAquireSysAux(L) == 0'i32
+  proc TryAcquireSys(L: var TSysLock): bool {.inline.} = 
+    result = TryAcquireSysAux(L) == 0'i32
 
   proc ReleaseSys(L: var TSysLock) {.
     importc: "pthread_mutex_unlock", header: "<pthread.h>".}
@@ -168,14 +168,14 @@ else:
   proc pthread_cancel(a1: TSysThread): cint {.
     importc: "pthread_cancel", header: "<pthread.h>".}
 
-  proc AquireSysTimeoutAux(L: var TSysLock, timeout: var Ttimespec): cint {.
+  proc AcquireSysTimeoutAux(L: var TSysLock, timeout: var Ttimespec): cint {.
     importc: "pthread_mutex_timedlock", header: "<time.h>".}
 
-  proc AquireSysTimeout(L: var TSysLock, msTimeout: int) {.inline.} =
+  proc AcquireSysTimeout(L: var TSysLock, msTimeout: int) {.inline.} =
     var a: Ttimespec
     a.tv_sec = msTimeout div 1000
     a.tv_nsec = (msTimeout mod 1000) * 1000
-    var res = AquireSysTimeoutAux(L, a)
+    var res = AcquireSysTimeoutAux(L, a)
     if res != 0'i32: raise newException(EResourceExhausted, $strerror(res))
 
   type
@@ -257,7 +257,7 @@ when not defined(useNimRtl):
     
   proc registerThread(t: PGcThread) = 
     # we need to use the GC global lock here!
-    AquireSys(HeapLock)
+    AcquireSys(HeapLock)
     t.prev = nil
     t.next = threadList
     if threadList != nil: 
@@ -268,7 +268,7 @@ when not defined(useNimRtl):
         
   proc unregisterThread(t: PGcThread) =
     # we need to use the GC global lock here!
-    AquireSys(HeapLock)
+    AcquireSys(HeapLock)
     if t == threadList: threadList = t.next
     if t.next != nil: t.next.prev = t.prev
     if t.prev != nil: t.prev.next = t.next
@@ -297,10 +297,12 @@ type
     data: TParam
   
 template ThreadProcWrapperBody(closure: expr) =
-  when not hasSharedHeap: initGC() # init the GC for this thread
   ThreadVarSetValue(globalsSlot, closure)
   var t = cast[ptr TThread[TParam]](closure)
-  when not hasSharedHeap: stackBottom = addr(t)
+  when not hasSharedHeap:
+    # init the GC for this thread:
+    setStackBottom(addr(t))
+    initGC()
   t.stackBottom = addr(t)
   registerThread(t)
   try:
@@ -337,7 +339,7 @@ proc joinThreads*[TParam](t: openArray[TThread[TParam]]) =
 
 proc destroyThread*[TParam](t: var TThread[TParam]) {.inline.} =
   ## forces the thread `t` to terminate. This is potentially dangerous if
-  ## you don't have full control over `t` and its aquired resources.
+  ## you don't have full control over `t` and its acquired resources.
   when hostOS == "windows":
     discard TerminateThread(t.sys, 1'i32)
   else:
@@ -357,12 +359,14 @@ proc createThread*[TParam](t: var TThread[TParam],
     var dummyThreadId: int32
     t.sys = CreateThread(nil, stackSize, threadProcWrapper[TParam],
                          addr(t), 0'i32, dummyThreadId)
+    if t.sys <= 0:
+      raise newException(EResourceExhausted, "cannot create thread")
   else:
     var a: Tpthread_attr
     pthread_attr_init(a)
     pthread_attr_setstacksize(a, stackSize)
     if pthread_create(t.sys, a, threadProcWrapper[TParam], addr(t)) != 0:
-      raise newException(EIO, "cannot create thread")
+      raise newException(EResourceExhausted, "cannot create thread")
 
 # --------------------------- lock handling ----------------------------------
 
@@ -386,17 +390,17 @@ proc OrderedLocks(g: PGcThread): bool =
     if g.locks[i] >= g.locks[i+1]: return false
   result = true
 
-proc TryAquire*(lock: var TLock): bool {.inline.} = 
-  ## Try to aquires the lock `lock`. Returns `true` on success.
+proc TryAcquire*(lock: var TLock): bool {.inline.} = 
+  ## Try to acquires the lock `lock`. Returns `true` on success.
+  result = TryAcquireSys(lock)
   when noDeadlocks:
-    result = TryAquireSys(lock)
     if not result: return
     # we have to add it to the ordered list. Oh, and we might fail if
     # there is no space in the array left ...
     var g = ThisThread()
     if g.locksLen >= len(g.locks):
       ReleaseSys(lock)
-      raise newException(EResourceExhausted, "cannot aquire additional lock")
+      raise newException(EResourceExhausted, "cannot acquire additional lock")
     # find the position to add:
     var p = addr(lock)
     var L = g.locksLen-1
@@ -418,11 +422,9 @@ proc TryAquire*(lock: var TLock): bool {.inline.} =
     g.locks[g.locksLen] = p
     inc(g.locksLen)
     assert OrderedLocks(g)
-  else:
-    result = TryAquireSys(lock)
 
-proc Aquire*(lock: var TLock) =
-  ## Aquires the lock `lock`.
+proc Acquire*(lock: var TLock) =
+  ## Acquires the lock `lock`.
   when nodeadlocks:
     var g = ThisThread()
     var p = addr(lock)
@@ -435,20 +437,21 @@ proc Aquire*(lock: var TLock) =
       else:
         # do the crazy stuff here:
         if g.locksLen >= len(g.locks):
-          raise newException(EResourceExhausted, "cannot aquire additional lock")
+          raise newException(EResourceExhausted, 
+              "cannot acquire additional lock")
         while L >= i:
           ReleaseSys(cast[ptr TSysLock](g.locks[L])[])
           g.locks[L+1] = g.locks[L]
           dec L
-        # aquire the current lock:
-        AquireSys(lock)
+        # acquire the current lock:
+        AcquireSys(lock)
         g.locks[i] = p
         inc(g.locksLen)
-        # aquire old locks in proper order again:
+        # acquire old locks in proper order again:
         L = g.locksLen-1
         inc i
         while i <= L:
-          AquireSys(cast[ptr TSysLock](g.locks[i])[])
+          AcquireSys(cast[ptr TSysLock](g.locks[i])[])
           inc(i)
         # DANGER: We can only modify this global var if we gained every lock!
         # NO! We need an atomic increment. Crap.
@@ -458,13 +461,13 @@ proc Aquire*(lock: var TLock) =
         
     # simply add to the end:
     if g.locksLen >= len(g.locks):
-      raise newException(EResourceExhausted, "cannot aquire additional lock")
-    AquireSys(lock)
+      raise newException(EResourceExhausted, "cannot acquire additional lock")
+    AcquireSys(lock)
     g.locks[g.locksLen] = p
     inc(g.locksLen)
     assert OrderedLocks(g)
   else:
-    AquireSys(lock)
+    AcquireSys(lock)
   
 proc Release*(lock: var TLock) =
   ## Releases the lock `lock`.