Skip to content

Commit 7499cba

Browse files
committed
adopted to dotty-cps-async 0.9.1 (async fir future starts with spawn)
1 parent d84605c commit 7499cba

File tree

6 files changed

+67
-30
lines changed

6 files changed

+67
-30
lines changed

build.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
val dottyVersion = "3.0.1"
33
//val dottyVersion = dottyLatestNightlyBuild.get
44

5-
ThisBuild/version := "2.0.5-SNAPSHOT"
5+
ThisBuild/version := "2.0.5"
66
ThisBuild/versionScheme := Some("semver-spec")
77

88
val sharedSettings = Seq(
99
organization := "com.github.rssh",
1010
scalaVersion := dottyVersion,
1111
name := "scala-gopher",
1212
resolvers += "Local Ivy Repository" at "file://"+Path.userHome.absolutePath+"/.ivy2/local",
13-
libraryDependencies += "com.github.rssh" %%% "dotty-cps-async" % "0.9.0",
13+
libraryDependencies += "com.github.rssh" %%% "dotty-cps-async" % "0.9.1",
1414
libraryDependencies += "org.scalameta" %%% "munit" % "0.7.26" % Test,
1515
testFrameworks += new TestFramework("munit.Framework")
1616
)

jvm/src/main/scala/gopher/impl/PromiseChannel.scala

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ import scala.util.Failure
3434
case Some((a,f)) =>
3535
val ar: AnyRef = a.asInstanceOf[AnyRef] //
3636
if (ref.compareAndSet(null,ar) && !closed.get() ) then
37-
closed.lazySet(true)
38-
taskExecutor.execute(()=> f(Success(())))
37+
closed.set(true)
38+
taskExecutor.execute{ ()=>
39+
f(Success(()))
40+
}
3941
writer.markUsed()
4042
step()
4143
else
@@ -48,21 +50,24 @@ import scala.util.Failure
4850

4951

5052
def addDoneReader(reader: Reader[Unit]): Unit =
51-
if (!closed.get()) then
53+
if (!closed.get() || !readed.get) then
5254
doneReaders.add(reader)
55+
if (closed.get()) then
56+
step()
5357
else
5458
var done = false
5559
while(!done & !reader.isExpired) {
56-
reader.capture() match
57-
case Some(f) =>
58-
reader.markUsed()
59-
taskExecutor.execute(()=>f(Success(())))
60-
done = true
61-
case None =>
62-
if (!reader.isExpired)
63-
Thread.onSpinWait()
60+
reader.capture() match
61+
case Some(f) =>
62+
reader.markUsed()
63+
taskExecutor.execute(()=>f(Success(())))
64+
done = true
65+
case None =>
66+
if (!reader.isExpired)
67+
Thread.onSpinWait()
6468
}
6569

70+
6671

6772

6873
def close(): Unit =

shared/src/main/scala/gopher/ReadChannel.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ object ReadChannel:
214214
retval.close()
215215
retval
216216

217+
/**
218+
*@param c - iteratable to read from.
219+
*@return channel, which will emit all elements from 'c' and then close.
220+
**/
217221
def fromIterable[F[_],A](c: IterableOnce[A])(using Gopher[F]): ReadChannel[F,A] =
218222
given asyncMonad: CpsSchedulingMonad[F] = summon[Gopher[F]].asyncMonad
219223
val retval = makeChannel[A]()
@@ -227,6 +231,28 @@ object ReadChannel:
227231
})
228232
retval
229233

234+
/**
235+
*@return one copy of `a` and close.
236+
**/
237+
def once[F[_],A](a: A)(using Gopher[F]): ReadChannel[F,A] =
238+
fromIterable(List(a))
239+
240+
/**
241+
*@param a - value to produce
242+
*@return channel which emit value of a in loop and never close
243+
**/
244+
def always[F[_],A](a: A)(using Gopher[F]): ReadChannel[F,A] =
245+
given asyncMonad: CpsSchedulingMonad[F] = summon[Gopher[F]].asyncMonad
246+
val retval = makeChannel[A]()
247+
summon[Gopher[F]].spawnAndLogFail(
248+
async{
249+
while(true) {
250+
retval.write(a)
251+
}
252+
}
253+
)
254+
retval
255+
230256

231257

232258
def fromFuture[F[_],A](f: F[A])(using Gopher[F]): ReadChannel[F,A] =

shared/src/main/scala/gopher/monads/ReadChannelCpsMonad.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ given futureToReadChannel[F[_]](using Gopher[F]): CpsMonadConversion[F, [A] =>>
2323
def apply[T](ft: F[T]): ReadChannel[F,T] = futureInput(ft)
2424

2525

26-
2726

27+
2828

shared/src/test/scala/gopher/channels/MacroSelectSuite.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ class MacroSelectSuite extends FunSuite
419419
}
420420
val f1 = ch.awrite(1)
421421
async {
422+
await(f1)
422423
val r = await(sf)
423424
assert(r==1)
424425
}

shared/src/test/scala/gopher/monads/Queens.scala

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,25 @@ class QueensSuite extends FunSuite {
2222
busyLRDiagonals:Set[Int],
2323
busyRLDiagonals:Set[Int],
2424
queens: Vector[(Int,Int)]
25-
);
25+
) {
26+
27+
def isBusy(i:Int, j:Int): Boolean =
28+
busyRows.contains(i) ||
29+
busyColumns.contains(j) ||
30+
busyLRDiagonals.contains(i-j) ||
31+
busyRLDiagonals.contains(i+j)
32+
33+
34+
def put(i:Int, j:Int): State =
35+
copy( busyRows = busyRows + i,
36+
busyColumns = busyColumns + j,
37+
busyLRDiagonals = busyLRDiagonals + (i-j),
38+
busyRLDiagonals = busyRLDiagonals + (i+j),
39+
queens = queens :+ (i,j)
40+
)
41+
42+
43+
}
2644

2745
val N = 8
2846

@@ -31,29 +49,16 @@ class QueensSuite extends FunSuite {
3149
async[Future] {
3250
val i = state.queens.length
3351
if i < N then
34-
for{
35-
j <- 0 until N if !state.busyColumns.contains(j) &&
36-
!state.busyLRDiagonals.contains(i-j) &&
37-
!state.busyRLDiagonals.contains(i+j)
38-
} {
39-
val newPos = (i,j)
40-
val nState = state.copy( busyRows = state.busyRows + i,
41-
busyColumns = state.busyColumns + j,
42-
busyLRDiagonals = state.busyLRDiagonals + (i-j),
43-
busyRLDiagonals = state.busyRLDiagonals + (i+j),
44-
queens = state.queens :+ newPos )
45-
ch.write(nState)
46-
}
52+
for{ j <- 0 until N if !state.isBusy(i,j) }
53+
ch.write(state.put(i,j))
4754
ch.close()
4855
}
4956
ch
5057

5158
def solutions(state: State): ReadChannel[Future,State] =
5259
async[[X] =>> ReadChannel[Future,X]] {
5360
if(state.queens.size < N) then
54-
//println("state:"+state.queens)
5561
val nextState = await(putQueen(state))
56-
//println("next-state:"+state.queens)
5762
await(solutions(nextState))
5863
else
5964
state

0 commit comments

Comments
 (0)