nim-lang / threading

New atomics, thread primitives, atomic refcounting for --gc:arc/orc.
MIT License
73 stars 12 forks source link

Atomics could accept SharedPtr type #17

Open planetis-m opened 2 years ago

planetis-m commented 2 years ago

Recently someone in the channel has tried to do Atomic[SharedPtr], but it's not currently possible. Of course you don't need the encapsulation, could also use an Atomic[bool] for this. But an idea would be to turn SharedPtr = object to distinct pointer and somehow relax the AtomType type class to accept distinct pointers. I have no idea how that would work.

beef331 commented 2 years ago

On planetis' request I'll document a possible solution to this problem:

import std/typetraits
type
  AtomicBases = concept a, type A
    distinctBase(A) is AtomType
  AllAtomTypes = AtomType or AtomicBases
  Atomic*[T: AllAtomTyps] = distinct T

This should allow all types that are actually capable of being atomic to be as such, though i guess one might want to ensure that the T itself is not atomic, but I do not know anything about threading.