Advertisement
Cool_Dalek

Scala3 VarHandle type safe zero-cost wrapper

Oct 5th, 2022
1,703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.77 KB | None | 0 0
  1. package flames.concurrent.execution.handles
  2.  
  3. import java.lang.invoke.{MethodHandles, VarHandle as JVarHandle}
  4.  
  5. import scala.reflect.{ClassTag, classTag}
  6.  
  7. object VarHandle:
  8.   opaque type VarGet[+T] = JVarHandle
  9.   opaque type VarSet[-T] = JVarHandle
  10.   opaque type VarHandle[T] <: VarGet[T] & VarSet[T] = JVarHandle
  11.   opaque type AccessModeTag[T] = T
  12.   sealed trait AccessMode {
  13.     type Tag[x] <: AccessModeTag[x]
  14.   }
  15.   opaque type Plain[T] <: AccessModeTag[T] = T
  16.   object Plain extends AccessMode {
  17.     type Tag[x] = Plain[x]
  18.   }
  19.   opaque type Opaque[T] <: Plain[T] = T
  20.   object Opaque extends AccessMode {
  21.     type Tag[x] = Opaque[x]
  22.   }
  23.   opaque type Acquire[T] <: Opaque[T] = T
  24.   object Acquire {
  25.     type Tag[x] = Acquire[x]
  26.   }
  27.   opaque type Release[T] <: Opaque[T] = T
  28.   object Release {
  29.     type Tag[x] = Release[x]
  30.   }
  31.   opaque type AcquireRelease[T] <: Acquire[T] & Release[T] = T
  32.   object AcquireRelease {
  33.     type Tag[x] = AcquireRelease[x]
  34.   }
  35.   opaque type Volatile[T] <: AcquireRelease[T] = T
  36.   object Volatile {
  37.     type Tag[x] = Volatile[x]
  38.   }
  39.  
  40.   extension [T](self: Plain[VarGet[T]])
  41.     inline def getPlain: T = self.get().asInstanceOf[T]
  42.  
  43.   extension[T](self: Opaque[VarGet[T]])
  44.     inline def getOpaque: T = self.getOpaque().asInstanceOf[T]
  45.  
  46.   extension[T](self: Acquire[VarGet[T]])
  47.     inline def getAcquire: T = self.getAcquire().asInstanceOf[T]
  48.  
  49.   extension[T](self: Volatile[VarGet[T]])
  50.     inline def getVolatile: T = self.getVolatile().asInstanceOf[T]
  51.  
  52.   extension [T](self: Plain[VarSet[T]])
  53.     inline def setPlain(value: T): Unit = self.set(value)
  54.  
  55.   extension [T](self: Opaque[VarSet[T]])
  56.     inline def setOpaque(value: T): Unit = self.setOpaque(value)
  57.  
  58.   extension [T](self: Release[VarSet[T]])
  59.     inline def setRelease(value: T): Unit = self.setRelease(value)
  60.  
  61.   extension [T](self: Volatile[VarSet[T]])
  62.     inline def setVolatile(value: T): Unit = self.setVolatile(value)
  63.  
  64.   extension [T](self: Plain[VarHandle[T]])
  65.  
  66.     inline def weakCompareAndSetPlain(expected: T, exchanged: T): Boolean =
  67.       self.weakCompareAndSetPlain(expected, exchanged)
  68.  
  69.   extension [T](self: Acquire[VarHandle[T]]) {
  70.  
  71.     inline def getAndSetAcquire(value: T): T = self.getAndSetAcquire(value).asInstanceOf[T]
  72.  
  73.     inline def compareAndExchangeAcquire(expected: T, exchanged: T): T =
  74.       self.compareAndExchangeAcquire(expected, exchanged).asInstanceOf[T]
  75.  
  76.     inline def weakCompareAndSetAcquire(expected: T, exchanged: T): Boolean =
  77.       self.weakCompareAndSetAcquire(expected, exchanged)
  78.  
  79.   }
  80.  
  81.   extension [T](self: Release[VarHandle[T]]) {
  82.  
  83.     inline def getAndSetRelease(value: T): T = self.getAndSetRelease(value).asInstanceOf[T]
  84.  
  85.     inline def compareAndExchangeRelease(expected: T, exchanged: T): T =
  86.       self.compareAndExchangeRelease(expected, exchanged).asInstanceOf[T]
  87.  
  88.     inline def weakCompareAndSetRelease(expected: T, exchanged: T): Boolean =
  89.       self.weakCompareAndSetRelease(expected, exchanged)
  90.  
  91.   }
  92.  
  93.   extension [T](self: Volatile[VarHandle[T]]) {
  94.  
  95.     inline def getAndSetVolatile(value: T): T = self.getAndSet(value).asInstanceOf[T]
  96.  
  97.     inline def compareAndSetVolatile(expected: T, exchanged: T): Boolean = self.compareAndSet(expected, exchanged)
  98.  
  99.     inline def compareAndExchangeVolatile(expected: T, exchanged: T): T =
  100.       self.compareAndExchange(expected, exchanged).asInstanceOf[T]
  101.  
  102.     inline def weakCompareAndSetVolatile(expected: T, exchanged: T): Boolean =
  103.       self.weakCompareAndSet(expected, exchanged)
  104.  
  105.   }
  106.  
  107.   inline def apply[From: ClassTag, Type: ClassTag](x: Type, mode: AccessMode): mode.Tag[VarHandle[Type]] =
  108.     ???
  109.   end apply
  110.  
  111. end VarHandle
  112. export VarHandle.{
  113.   VarGet, VarSet, VarHandle,
  114.   AccessModeTag, Plain, Opaque,
  115.   Acquire, Release,
  116.   AcquireRelease, Volatile,
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement