-
-
Save cheatfate/ec8770bc33d2cf37b2907758b92204d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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