diff options
Diffstat (limited to 'lib/system')
-rwxr-xr-x | lib/system/gc.nim | 8 | ||||
-rwxr-xr-x | lib/system/repr.nim | 3 | ||||
-rwxr-xr-x | lib/system/sysio.nim | 10 | ||||
-rwxr-xr-x | lib/system/sysstr.nim | 38 | ||||
-rwxr-xr-x | lib/system/threads.nim | 18 |
5 files changed, 36 insertions, 41 deletions
diff --git a/lib/system/gc.nim b/lib/system/gc.nim index 033a7bdbe..619b1c9b1 100755 --- a/lib/system/gc.nim +++ b/lib/system/gc.nim @@ -198,18 +198,18 @@ proc prepareDealloc(cell: PCell) = proc rtlAddCycleRoot(c: PCell) {.rtl, inl.} = # we MUST access gch as a global here, because this crosses DLL boundaries! - when hasThreadSupport: + when hasThreadSupport and hasSharedHeap: AcquireSys(HeapLock) incl(gch.cycleRoots, c) - when hasThreadSupport: + when hasThreadSupport and hasSharedHeap: ReleaseSys(HeapLock) proc rtlAddZCT(c: PCell) {.rtl, inl.} = # we MUST access gch as a global here, because this crosses DLL boundaries! - when hasThreadSupport: + when hasThreadSupport and hasSharedHeap: AcquireSys(HeapLock) addZCT(gch.zct, c) - when hasThreadSupport: + when hasThreadSupport and hasSharedHeap: ReleaseSys(HeapLock) proc decRef(c: PCell) {.inline.} = diff --git a/lib/system/repr.nim b/lib/system/repr.nim index add0af10c..256313ebd 100755 --- a/lib/system/repr.nim +++ b/lib/system/repr.nim @@ -9,9 +9,6 @@ # The generic ``repr`` procedure. It is an invaluable debugging tool. -#proc cstrToNimStrDummy(s: cstring): string {.inline.} = -# result = cast[string](cstrToNimStr(s)) - proc reprInt(x: int64): string {.compilerproc.} = return $x proc reprFloat(x: float): string {.compilerproc.} = return $x diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index af6ba49b2..cb4aaae54 100755 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -36,13 +36,16 @@ var IOFBF {.importc: "_IOFBF", nodecl.}: cint IONBF {.importc: "_IONBF", nodecl.}: cint +proc raiseEIO(msg: string) {.noinline, noreturn.} = + raise newException(EIO, msg) + proc rawReadLine(f: TFile, result: var string) = # of course this could be optimized a bit; but IO is slow anyway... # and it was difficult to get this CORRECT with Ansi C's methods setLen(result, 0) # reuse the buffer! while True: var c = fgetc(f) - if c < 0'i32: break # EOF + if c < 0'i32: raiseEIO("EOF reached") if c == 10'i32: break # LF if c == 13'i32: # CR c = fgetc(f) # is the next char LF? @@ -76,11 +79,6 @@ proc write(f: TFile, c: Char) = putc(c, f) proc write(f: TFile, a: openArray[string]) = for x in items(a): write(f, x) -#{.error: "for debugging.".} - -proc raiseEIO(msg: string) {.noinline, noreturn.} = - raise newException(EIO, msg) - proc readFile(filename: string): string = var f = open(filename) try: diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim index 2afb91d7b..e8d351899 100755 --- a/lib/system/sysstr.nim +++ b/lib/system/sysstr.nim @@ -15,12 +15,10 @@ # we don't use refcounts because that's a behaviour # the programmer may not want -# implementation: - proc resize(old: int): int {.inline.} = - if old <= 0: return 4 - elif old < 65536: return old * 2 - else: return old * 3 div 2 # for large arrays * 3/2 is better + if old <= 0: result = 4 + elif old < 65536: result = old * 2 + else: result = old * 3 div 2 # for large arrays * 3/2 is better proc cmpStrings(a, b: NimString): int {.inline, compilerProc.} = if a == b: return 0 @@ -53,13 +51,13 @@ proc toNimStr(str: CString, len: int): NimString {.compilerProc.} = result.data[len] = '\0' # readline relies on this! proc cstrToNimstr(str: CString): NimString {.compilerProc.} = - return toNimstr(str, c_strlen(str)) + result = toNimstr(str, c_strlen(str)) proc copyString(src: NimString): NimString {.compilerProc.} = - if src == nil: return nil - result = rawNewString(src.space) - result.len = src.len - c_memcpy(result.data, src.data, (src.len + 1) * sizeof(Char)) + if src != nil: + result = rawNewString(src.space) + result.len = src.len + c_memcpy(result.data, src.data, (src.len + 1) * sizeof(Char)) proc hashString(s: string): int {.compilerproc.} = # the compiler needs exactly the same hash function! @@ -86,7 +84,7 @@ proc copyStrLast(s: NimString, start, last: int): NimString {.exportc.} = result = mnewString(0) proc copyStr(s: NimString, start: int): NimString {.exportc.} = - return copyStrLast(s, start, s.len-1) + result = copyStrLast(s, start, s.len-1) proc addChar(s: NimString, c: char): NimString = # is compilerproc! @@ -135,7 +133,7 @@ proc addChar(s: NimString, c: char): NimString = # s = rawNewString(0); proc resizeString(dest: NimString, addlen: int): NimString {.compilerproc.} = - if dest.len + addLen + 1 <= dest.space: # BUGFIX: this is horrible! + if dest.len + addLen + 1 <= dest.space: result = dest else: # slow path: var sp = max(resize(dest.space), dest.len + addLen + 1) @@ -279,12 +277,12 @@ proc binaryStrSearch(x: openarray[string], y: string): int {.compilerproc.} = a = 0 b = len(x) while a < b: - var mid = (a + b) div 2 - if x[mid] < y: - a = mid + 1 - else: - b = mid - if (a < len(x)) and (x[a] == y): - return a + var mid = (a + b) div 2 + if x[mid] < y: + a = mid + 1 + else: + b = mid + if a < len(x) and x[a] == y: + result = a else: - return -1 + result = -1 diff --git a/lib/system/threads.nim b/lib/system/threads.nim index db16502ff..7478b89d0 100755 --- a/lib/system/threads.nim +++ b/lib/system/threads.nim @@ -339,14 +339,16 @@ proc joinThreads*[TParam](t: openArray[TThread[TParam]]) = else: for i in 0..t.high: joinThread(t[i]) -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 acquired resources. - when hostOS == "windows": - discard TerminateThread(t.sys, 1'i32) - else: - discard pthread_cancel(t.sys) - unregisterThread(addr(t.gcInfo)) +when false: + # XXX a thread should really release its heap here somehow: + 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 acquired resources. + when hostOS == "windows": + discard TerminateThread(t.sys, 1'i32) + else: + discard pthread_cancel(t.sys) + unregisterThread(addr(t)) proc createThread*[TParam](t: var TThread[TParam], tp: proc (param: TParam), |