logos-messaging-nim/waku/common/broker/broker_context.nim
NagyZoltanPeter 94e00e7e25
Apply suggestion from @Ivansete-status
Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com>
2025-12-18 12:04:41 +01:00

19 lines
518 B
Nim

import std/[strutils, concurrency/atomics]
type BrokerContext* = distinct uint32
func `==`*(a, b: BrokerContext): bool {.borrow.}
func `$`*(bc: BrokerContext): string =
toHex(uint32(bc), 8)
const DefaultBrokerContext* = BrokerContext(0xCAFFE14E'u32)
var gContextCounter: Atomic[uint32]
proc newBrokerContext*(): BrokerContext =
var nextId = gContextCounter.fetchAdd(1, moRelaxed)
if nextId == uint32(DefaultBrokerContext):
nextId = gContextCounter.fetchAdd(1, moRelaxed)
return BrokerContext(nextId)