Skip to content

Instantly share code, notes, and snippets.

@cheatfate
Created October 11, 2019 05:40
Show Gist options
  • Save cheatfate/ec8770bc33d2cf37b2907758b92204d9 to your computer and use it in GitHub Desktop.
Save cheatfate/ec8770bc33d2cf37b2907758b92204d9 to your computer and use it in GitHub Desktop.
import sets, hashes
type
PDispatcher* = ref object
handles: HashSet[AsyncFD]
AsyncFD* = distinct int
proc hash(x: AsyncFD): Hash {.borrow.}
proc `==`*(x: AsyncFD, y: AsyncFD): bool {.borrow.}
var gDisp{.threadvar.}: PDispatcher ## Global dispatcher
proc newDispatcher*(): PDispatcher =
## Creates a new Dispatcher instance.
new result
result.handles = initSet[AsyncFD]()
proc setGlobalDispatcher*(disp: PDispatcher) =
gDisp = disp
proc getGlobalDispatcher*(): PDispatcher =
if gDisp.isNil:
setGlobalDispatcher(newDispatcher())
result = gDisp
proc register*(fd: AsyncFD) =
getGlobalDispatcher().handles.incl(fd)
proc unregister*(fd: AsyncFD) =
getGlobalDispatcher().handles.excl(fd)
when isMainModule:
for i in 0 ..< 1000:
echo "Iteration ", i
for i in 0 ..< 100_000:
register(AsyncFD(i))
if i mod 10_000 == 0:
echo "registering ", i
for i in 0 ..< 100_000:
unregister(AsyncFD(i))
if i mod 10_000 == 0:
echo "unregistering ", i
echo "Iteration ", i, " finished, press Enter to continue"
var c = readLine(stdin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment