ic/sharedcounters

Search:
Group by:
Source   Edit  

std/macrocache.CacheCounter state shared by the nim m processes of a nim ic build. Sibling modules are compiled by separate (possibly parallel) processes, so an in-memory counter lets both allocate from the same initial state and embed duplicate values (#26201). Instead the counters live in one file in the nimcache, guarded by an OS file lock that a process takes on its first counter operation and keeps until it is done with its module: ids.inc; ids.value must observe its own increment, so locking every operation separately would not suffice. No process waits on another one while holding the lock, so this cannot deadlock.

The file survives across builds and records, per counter, the high-water mark and the numbers every process (keyed by its module) was handed. A re-semmed module is handed the same numbers again, so a no-op rebuild reproduces its artifacts; only a module that needs more numbers than before is given fresh ones above the high-water mark. The numbers of one module therefore need not be contiguous. They are unique, but unlike under nim c neither dense nor ordered by import order.

Procs

proc releaseSharedCounters() {....raises: [IOError], tags: [], forbids: [].}
The OS would release the lock at process exit too; this merely does it as early as possible. Source   Edit  
proc sharedCounterInc(conf: ConfigRef; key: string; by: BiggestInt) {.
    ...raises: [OSError, IOError, ValueError, KeyError, Exception], tags: [
    ReadEnvEffect, ReadIOEffect, WriteDirEffect, ReadDirEffect, WriteIOEffect],
    forbids: [].}
Source   Edit  
proc sharedCounterValue(conf: ConfigRef; key: string): BiggestInt {.
    ...raises: [OSError, IOError, ValueError, KeyError],
    tags: [ReadEnvEffect, ReadIOEffect, WriteDirEffect, ReadDirEffect],
    forbids: [].}
Source   Edit  
proc usesSharedCounters(conf: ConfigRef): bool {.inline, ...raises: [], tags: [],
    forbids: [].}
Under nim m each module is compiled by its own process, so the counters must be shared through the nimcache. Source   Edit