Skip to content

Commit

Permalink
moving all APIs to the kyo package
Browse files Browse the repository at this point in the history
  • Loading branch information
fwbrasil committed Jan 15, 2024
1 parent 73da13b commit ef7e259
Show file tree
Hide file tree
Showing 52 changed files with 403 additions and 485 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class EnqueueDequeueBench extends Bench.ForkOnly[Unit] {
import kyo._
import kyo.ios._
import kyo.fibers._
import kyo.channels._
import kyo.Access

def loop(c: Channel[Unit], i: Int): Unit < Fibers =
Expand Down
1 change: 0 additions & 1 deletion kyo-bench/src/main/scala/kyo/bench/ForkManyBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class ForkManyBench extends Bench.ForkOnly[Int] {
override def kyoBenchFiber() = {
import kyo._
import kyo.ios._
import kyo.atomics._
import kyo.fibers._

def repeat[A](n: Int)(io: A < IOs): A < IOs =
Expand Down
2 changes: 0 additions & 2 deletions kyo-bench/src/main/scala/kyo/bench/PingPongBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ class PingPongBench extends Bench.ForkOnly[Unit] {
import kyo.ios._
import kyo.queues._
import kyo.fibers._
import kyo.atomics._
import kyo.channels._

def repeat[A](n: Int)(io: A < Fibers): A < Fibers =
if (n <= 1) io
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class ProducerConsumerBench extends Bench.ForkOnly[Unit] {
import kyo._
import kyo.ios._
import kyo.fibers._
import kyo.channels._
import kyo.Access

def repeat[A](n: Int)(io: A < Fibers): A < Fibers =
Expand Down
1 change: 0 additions & 1 deletion kyo-bench/src/main/scala/kyo/bench/RendezvousBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class RendezvousBench extends Bench.ForkOnly[Int] {
import kyo._
import kyo._
import kyo.ios._
import kyo.atomics._
import kyo.fibers._

def produce(waiting: AtomicRef[Any], n: Int = 0): Unit < Fibers =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object FlatImplicits {
}

def code(str: String) =
s"${Console.YELLOW}'$str'${Console.RESET}"
s"${scala.Console.YELLOW}'$str'${scala.Console.RESET}"

def print(t: Type): String = {
t match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object FlatImplicits {
}

def code(str: String) =
s"${Console.YELLOW}'$str'${Console.RESET}"
s"${scala.Console.YELLOW}'$str'${scala.Console.RESET}"

def print(t: TypeRepr): String = {
t match {
Expand Down
3 changes: 0 additions & 3 deletions kyo-core/shared/src/main/scala/kyo/KyoApp.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package kyo

import ios._
import clocks._
import consoles._
import resources._
import tries._
import aspects._
import randoms._
import fibers._
import timers._
Expand Down
33 changes: 15 additions & 18 deletions kyo-core/shared/src/main/scala/kyo/adders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,23 @@ import kyo.ios._
import java.util.concurrent.atomic.{DoubleAdder => JDoubleAdder}
import java.util.concurrent.atomic.{LongAdder => JLongAdder}

object adders {

object Adders {
val initLong: LongAdder < IOs = IOs(new LongAdder(new JLongAdder()))
val initDouble: DoubleAdder < IOs = IOs(new DoubleAdder(new JDoubleAdder()))
}
object Adders {
val initLong: LongAdder < IOs = IOs(new LongAdder(new JLongAdder()))
val initDouble: DoubleAdder < IOs = IOs(new DoubleAdder(new JDoubleAdder()))
}

class LongAdder private[adders] (private val ref: JLongAdder) extends AnyVal {
class LongAdder private[kyo] (private val ref: JLongAdder) extends AnyVal {

def add(v: Long): Unit < IOs = IOs(ref.add(v))
def decrement: Unit < IOs = IOs(ref.decrement())
def increment: Unit < IOs = IOs(ref.increment())
def get: Long < IOs = IOs(ref.sum())
def reset: Unit < IOs = IOs(ref.reset())
}
def add(v: Long): Unit < IOs = IOs(ref.add(v))
def decrement: Unit < IOs = IOs(ref.decrement())
def increment: Unit < IOs = IOs(ref.increment())
def get: Long < IOs = IOs(ref.sum())
def reset: Unit < IOs = IOs(ref.reset())
}

class DoubleAdder private[adders] (private val ref: JDoubleAdder) extends AnyVal {
class DoubleAdder private[kyo] (private val ref: JDoubleAdder) extends AnyVal {

def add(v: Double): Unit < IOs = IOs(ref.add(v))
def get: Double < IOs = IOs(ref.sum())
def reset: Unit < IOs = IOs(ref.reset())
}
def add(v: Double): Unit < IOs = IOs(ref.add(v))
def get: Double < IOs = IOs(ref.sum())
def reset: Unit < IOs = IOs(ref.reset())
}
103 changes: 50 additions & 53 deletions kyo-core/shared/src/main/scala/kyo/aspects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,73 @@ package kyo

import kyo._
import locals._
import kyo.clocks.Clocks
import kyo.consoles.Consoles
import fibers.Fibers
import kyo.ios.IOs
import kyo.randoms.Randoms
import kyo.resources.Resources
import timers.Timers

object aspects {
object Aspects {

private val local = Locals.init(Map.empty[Aspect[_, _, _], Cut[_, _, _]])
private[kyo] val local = Locals.init(Map.empty[Aspect[_, _, _], Cut[_, _, _]])

object Aspects {
def init[T, U, S]: Aspect[T, U, S] =
init(new Cut[T, U, S] {
def apply[S2](v: T < S2)(f: T => U < (IOs with S)) =
v.map(f)
})

def init[T, U, S]: Aspect[T, U, S] =
init(new Cut[T, U, S] {
def apply[S2](v: T < S2)(f: T => U < (IOs with S)) =
v.map(f)
})
def init[T, U, S](default: Cut[T, U, S]): Aspect[T, U, S] =
new Aspect[T, U, S](default)
}

def init[T, U, S](default: Cut[T, U, S]): Aspect[T, U, S] =
new Aspect[T, U, S](default)
}
import Aspects._

abstract class Cut[T, U, S] {
def apply[S2](v: T < S2)(f: T => U < (IOs with S)): U < (IOs with S with S2)
abstract class Cut[T, U, S] {
def apply[S2](v: T < S2)(f: T => U < (IOs with S)): U < (IOs with S with S2)

def andThen(other: Cut[T, U, S]): Cut[T, U, S] =
new Cut[T, U, S] {
def apply[S2](v: T < S2)(f: T => U < (IOs with S)) =
Cut.this(v)(other(_)(f))
}
}
def andThen(other: Cut[T, U, S]): Cut[T, U, S] =
new Cut[T, U, S] {
def apply[S2](v: T < S2)(f: T => U < (IOs with S)) =
Cut.this(v)(other(_)(f))
}
}

final class Aspect[T, U, S] private[aspects] (default: Cut[T, U, S]) extends Cut[T, U, S] {
final class Aspect[T, U, S] private[kyo] (default: Cut[T, U, S]) extends Cut[T, U, S] {

def apply[S2](v: T < S2)(f: T => U < (IOs with S)) =
local.get.map { map =>
map.get(this) match {
case Some(a: Cut[T, U, S] @unchecked) =>
local.let(map - this) {
a(v)(f)
}
case _ =>
default(v)(f)
}
def apply[S2](v: T < S2)(f: T => U < (IOs with S)) =
local.get.map { map =>
map.get(this) match {
case Some(a: Cut[T, U, S] @unchecked) =>
local.let(map - this) {
a(v)(f)
}
case _ =>
default(v)(f)
}
}

def sandbox[S](v: T < S): T < (IOs with S) =
local.get.map { map =>
map.get(this) match {
case Some(a: Cut[T, U, S] @unchecked) =>
local.let(map - this) {
v
}
case _ =>
def sandbox[S](v: T < S): T < (IOs with S) =
local.get.map { map =>
map.get(this) match {
case Some(a: Cut[T, U, S] @unchecked) =>
local.let(map - this) {
v
}
}

def let[V, S2](a: Cut[T, U, S])(v: V < (IOs with S2)): V < (IOs with S with S2) =
local.get.map { map =>
val cut =
map.get(this) match {
case Some(b: Cut[T, U, S] @unchecked) =>
b.andThen(a)
case _ =>
a
}
local.let(map + (this -> cut))(v)
case _ =>
v
}
}
}

def let[V, S2](a: Cut[T, U, S])(v: V < (IOs with S2)): V < (IOs with S with S2) =
local.get.map { map =>
val cut =
map.get(this) match {
case Some(b: Cut[T, U, S] @unchecked) =>
b.andThen(a)
case _ =>
a
}
local.let(map + (this -> cut))(v)
}
}
131 changes: 64 additions & 67 deletions kyo-core/shared/src/main/scala/kyo/atomics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,68 @@ import java.util.concurrent.atomic.{AtomicInteger => JAtomicInteger}
import java.util.concurrent.atomic.{AtomicLong => JAtomicLong}
import java.util.concurrent.atomic.{AtomicReference => JAtomicReference}

object atomics {

object Atomics {

def initInt(v: Int): AtomicInt < IOs = IOs(new AtomicInt(new JAtomicInteger(v)))
def initLong(v: Long): AtomicLong < IOs = IOs(new AtomicLong(new JAtomicLong(v)))
def initBoolean(v: Boolean): AtomicBoolean < IOs = IOs(new AtomicBoolean(new JAtomicBoolean(v)))
def initRef[T](v: T): AtomicRef[T] < IOs = IOs(new AtomicRef(new JAtomicReference[T](v)))
}

class AtomicInt private[atomics] (private val ref: JAtomicInteger) extends AnyVal {

def get: Int < IOs = IOs(ref.get())
def set(v: Int): Unit < IOs = IOs(ref.set(v))
def lazySet(v: Int): Unit < IOs = IOs(ref.lazySet(v))
def getAndSet(v: Int): Int < IOs = IOs(ref.getAndSet(v))
def cas(curr: Int, next: Int): Boolean < IOs = IOs(ref.compareAndSet(curr, next))
def incrementAndGet: Int < IOs = IOs(ref.incrementAndGet())
def decrementAndGet: Int < IOs = IOs(ref.decrementAndGet())
def getAndIncrement: Int < IOs = IOs(ref.getAndIncrement())
def getAndDecrement: Int < IOs = IOs(ref.getAndDecrement())
def getAndAdd(v: Int): Int < IOs = IOs(ref.getAndAdd(v))
def addAndGet(v: Int): Int < IOs = IOs(ref.addAndGet(v))

override def toString = ref.toString()
}

class AtomicLong private[atomics] (private val ref: JAtomicLong) extends AnyVal {

def get: Long < IOs = IOs(ref.get())
def set(v: Long): Unit < IOs = IOs(ref.set(v))
def lazySet(v: Long): Unit < IOs = IOs(ref.lazySet(v))
def getAndSet(v: Long): Long < IOs = IOs(ref.getAndSet(v))
def cas(curr: Long, next: Long): Boolean < IOs = IOs(ref.compareAndSet(curr, next))
def incrementAndGet: Long < IOs = IOs(ref.incrementAndGet())
def decrementAndGet: Long < IOs = IOs(ref.decrementAndGet())
def getAndIncrement: Long < IOs = IOs(ref.getAndIncrement())
def getAndDecrement: Long < IOs = IOs(ref.getAndDecrement())
def getAndAdd(v: Long): Long < IOs = IOs(ref.getAndAdd(v))
def addAndGet(v: Long): Long < IOs = IOs(ref.addAndGet(v))

override def toString = ref.toString()
}

class AtomicBoolean private[atomics] (private val ref: JAtomicBoolean) extends AnyVal {

def get: Boolean < IOs = IOs(ref.get())
def set(v: Boolean): Unit < IOs = IOs(ref.set(v))
def lazySet(v: Boolean): Unit < IOs = IOs(ref.lazySet(v))
def getAndSet(v: Boolean): Boolean < IOs = IOs(ref.getAndSet(v))
def cas(curr: Boolean, next: Boolean): Boolean < IOs = IOs(ref.compareAndSet(curr, next))

override def toString = ref.toString()
}

class AtomicRef[T] private[atomics] (private val ref: JAtomicReference[T]) extends AnyVal {

def get: T < IOs = IOs(ref.get())
def set(v: T): Unit < IOs = IOs(ref.set(v))
def lazySet(v: T): Unit < IOs = IOs(ref.lazySet(v))
def getAndSet(v: T): T < IOs = IOs(ref.getAndSet(v))
def cas(curr: T, next: T): Boolean < IOs = IOs(ref.compareAndSet(curr, next))
def update[S](f: T => T): Unit < IOs = updateAndGet(f).unit
def updateAndGet[S](f: T => T): T < IOs = IOs(ref.updateAndGet(f(_)))

override def toString = ref.toString()
}
object Atomics {

def initInt(v: Int): AtomicInt < IOs = IOs(new AtomicInt(new JAtomicInteger(v)))
def initLong(v: Long): AtomicLong < IOs = IOs(new AtomicLong(new JAtomicLong(v)))
def initBoolean(v: Boolean): AtomicBoolean < IOs = IOs(new AtomicBoolean(new JAtomicBoolean(v)))
def initRef[T](v: T): AtomicRef[T] < IOs = IOs(new AtomicRef(new JAtomicReference[T](v)))
}

class AtomicInt private[kyo] (private val ref: JAtomicInteger) extends AnyVal {

def get: Int < IOs = IOs(ref.get())
def set(v: Int): Unit < IOs = IOs(ref.set(v))
def lazySet(v: Int): Unit < IOs = IOs(ref.lazySet(v))
def getAndSet(v: Int): Int < IOs = IOs(ref.getAndSet(v))
def cas(curr: Int, next: Int): Boolean < IOs = IOs(ref.compareAndSet(curr, next))
def incrementAndGet: Int < IOs = IOs(ref.incrementAndGet())
def decrementAndGet: Int < IOs = IOs(ref.decrementAndGet())
def getAndIncrement: Int < IOs = IOs(ref.getAndIncrement())
def getAndDecrement: Int < IOs = IOs(ref.getAndDecrement())
def getAndAdd(v: Int): Int < IOs = IOs(ref.getAndAdd(v))
def addAndGet(v: Int): Int < IOs = IOs(ref.addAndGet(v))

override def toString = ref.toString()
}

class AtomicLong private[kyo] (private val ref: JAtomicLong) extends AnyVal {

def get: Long < IOs = IOs(ref.get())
def set(v: Long): Unit < IOs = IOs(ref.set(v))
def lazySet(v: Long): Unit < IOs = IOs(ref.lazySet(v))
def getAndSet(v: Long): Long < IOs = IOs(ref.getAndSet(v))
def cas(curr: Long, next: Long): Boolean < IOs = IOs(ref.compareAndSet(curr, next))
def incrementAndGet: Long < IOs = IOs(ref.incrementAndGet())
def decrementAndGet: Long < IOs = IOs(ref.decrementAndGet())
def getAndIncrement: Long < IOs = IOs(ref.getAndIncrement())
def getAndDecrement: Long < IOs = IOs(ref.getAndDecrement())
def getAndAdd(v: Long): Long < IOs = IOs(ref.getAndAdd(v))
def addAndGet(v: Long): Long < IOs = IOs(ref.addAndGet(v))

override def toString = ref.toString()
}

class AtomicBoolean private[kyo] (private val ref: JAtomicBoolean) extends AnyVal {

def get: Boolean < IOs = IOs(ref.get())
def set(v: Boolean): Unit < IOs = IOs(ref.set(v))
def lazySet(v: Boolean): Unit < IOs = IOs(ref.lazySet(v))
def getAndSet(v: Boolean): Boolean < IOs = IOs(ref.getAndSet(v))
def cas(curr: Boolean, next: Boolean): Boolean < IOs = IOs(ref.compareAndSet(curr, next))

override def toString = ref.toString()
}

class AtomicRef[T] private[kyo] (private val ref: JAtomicReference[T]) extends AnyVal {

def get: T < IOs = IOs(ref.get())
def set(v: T): Unit < IOs = IOs(ref.set(v))
def lazySet(v: T): Unit < IOs = IOs(ref.lazySet(v))
def getAndSet(v: T): T < IOs = IOs(ref.getAndSet(v))
def cas(curr: T, next: T): Boolean < IOs = IOs(ref.compareAndSet(curr, next))
def update[S](f: T => T): Unit < IOs = updateAndGet(f).unit
def updateAndGet[S](f: T => T): T < IOs = IOs(ref.updateAndGet(f(_)))

override def toString = ref.toString()
}
Loading

0 comments on commit ef7e259

Please sign in to comment.