gen_sym.go 380 B

123456789101112131415161718192021
  1. package gls
  2. import (
  3. "sync"
  4. )
  5. var (
  6. keyMtx sync.Mutex
  7. keyCounter uint64
  8. )
  9. // ContextKey is a throwaway value you can use as a key to a ContextManager
  10. type ContextKey struct{ id uint64 }
  11. // GenSym will return a brand new, never-before-used ContextKey
  12. func GenSym() ContextKey {
  13. keyMtx.Lock()
  14. defer keyMtx.Unlock()
  15. keyCounter += 1
  16. return ContextKey{id: keyCounter}
  17. }