Skip to content

Commit 1255308

Browse files
Richard WhalingRichard Whaling
authored andcommitted
removed loopextension trait and setting up release plugins
1 parent 3a164e7 commit 1255308

File tree

7 files changed

+95
-76
lines changed

7 files changed

+95
-76
lines changed

build.sbt

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
1+
2+
homepage := Some(url("https://github.com/scala-native/scala-native-loop"))
3+
licenses := Seq("Apache 2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0"))
4+
publishMavenStyle := true
5+
publishArtifact in Test := false
6+
pomIncludeRepository := { _ => false }
7+
publishTo in ThisBuild := {
8+
val nexus = "https://oss.sonatype.org/"
9+
if (isSnapshot.value)
10+
Some("snapshots" at nexus + "content/repositories/snapshots")
11+
else
12+
Some("releases" at nexus + "service/local/staging/deploy/maven2")
13+
}
14+
scmInfo := Some(
15+
ScmInfo(
16+
url("https://github.com/scala-native/scala-native-loop"),
17+
"scm:git:[email protected]:scala-native/scala-native-loop.git"
18+
)
19+
)
20+
developers := List(
21+
Developer("rwhaling", "Richard Whaling", "[email protected]", url("http://whaling.dev"))
22+
)
23+
124
lazy val commonSettings = Seq(
225
organization := "dev.whaling",
3-
version := "0.1-SNAPSHOT",
4-
scalaVersion := "2.11.12"
26+
version := "0.1.1-SNAPSHOT",
27+
scalaVersion := "2.11.12",
28+
skip in publish := true,
29+
skip in publishLocal := true
530
)
631

732
lazy val core = (project in file("core"))
33+
.settings(name := "native-loop-core")
834
.settings(commonSettings:_*)
35+
.settings(skip in publish := false)
36+
.settings(skip in publishLocal := false)
937
.enablePlugins(ScalaNativePlugin)
1038

1139
lazy val pipe = (project in file("pipe"))
@@ -23,6 +51,14 @@ lazy val server = (project in file("server"))
2351
.enablePlugins(ScalaNativePlugin)
2452
.dependsOn(core)
2553

54+
lazy val scalaJsCompat = (project in file("scalajs-compat"))
55+
.settings(name := "native-loop-js-compat")
56+
.settings(commonSettings:_*)
57+
.settings(skip in publish := false)
58+
.settings(skip in publishLocal := false)
59+
.enablePlugins(ScalaNativePlugin)
60+
.dependsOn(core)
61+
2662
lazy val serverExample = (project in file("examples/server"))
2763
.settings(commonSettings:_*)
2864
.enablePlugins(ScalaNativePlugin)

client/curl.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import scala.scalanative.runtime.Intrinsics
1010

1111
case class ResponseState(var code:Int = 200,var headers:mutable.Map[String,String] = mutable.Map(),var body:String = "")
1212

13-
object Curl extends LoopExtension {
13+
object Curl {
1414
import LibCurl._
1515
import LibCurlConstants._
1616
import LibUV._
@@ -25,8 +25,6 @@ object Curl extends LoopExtension {
2525
val requestPromises = mutable.Map[Long,Promise[ResponseState]]()
2626
val requests = mutable.Map[Long,ResponseState]()
2727

28-
override def activeRequests = requests.size
29-
3028
var initialized = false
3129

3230
def init():Unit = {
@@ -42,7 +40,6 @@ object Curl extends LoopExtension {
4240
println(s"timerCB: $startTimerCB")
4341

4442
check(uv_timer_init(loop,timerHandle),"uv_timer_init")
45-
EventLoop.addExtension(this)
4643
initialized = true
4744
println("done")
4845
}

core/loop.scala

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,18 @@ import scala.Option
1111
import LibUV.Buffer
1212
import LibUVConstants.check
1313

14-
trait LoopExtension {
15-
def activeRequests:Int
16-
}
17-
1814
object EventLoop extends ExecutionContextExecutor {
1915
import LibUV._, LibUVConstants._
2016

21-
2217
val loop = uv_default_loop()
2318
private val taskQueue = ListBuffer[Runnable]()
24-
private val extensions = ListBuffer[LoopExtension]()
25-
var publishers = 0
19+
val handle = stdlib.malloc(uv_handle_size(UV_PREPARE_T))
2620

2721
private def initDispatcher(loop:LibUV.Loop):PrepareHandle = {
28-
val handle = stdlib.malloc(uv_handle_size(UV_PREPARE_T))
2922
check(uv_prepare_init(loop, handle), "uv_prepare_init")
30-
check(uv_prepare_start(handle, prepareCallback), "uv_prepare_start")
3123
return handle
3224
}
3325

34-
def addExtension(e:LoopExtension):Unit = {
35-
extensions.append(e)
36-
}
37-
38-
private def extensionsWorking():Boolean = {
39-
extensions.exists( _.activeRequests > 0)
40-
}
41-
4226
val prepareCallback = new PrepareCB {
4327
def apply(handle:PrepareHandle) = {
4428
while (taskQueue.nonEmpty) {
@@ -49,27 +33,29 @@ object EventLoop extends ExecutionContextExecutor {
4933
case t: Throwable => reportFailure(t)
5034
}
5135
}
52-
if (taskQueue.isEmpty && !extensionsWorking) {
53-
println("stopping dispatcher")
36+
if (taskQueue.isEmpty) {
5437
LibUV.uv_prepare_stop(handle)
5538
}
56-
5739
}
5840
}
5941

6042
private val dispatcher = initDispatcher(loop)
6143

62-
def execute(runnable: Runnable): Unit = taskQueue += runnable
44+
private val bootstrapFuture = Future(run())(scalanative.runtime.ExecutionContext.global)
45+
46+
def execute(runnable: Runnable): Unit = {
47+
taskQueue += runnable
48+
check(uv_prepare_start(handle, prepareCallback), "uv_prepare_start")
49+
}
50+
6351
def reportFailure(t: Throwable): Unit = {
64-
println(s"Future failed with Throwable $t:")
6552
t.printStackTrace()
6653
}
6754

6855
def run(mode:Int = UV_RUN_DEFAULT):Unit = {
6956
var continue = 1
7057
while (continue != 0) {
7158
continue = uv_run(loop, mode)
72-
println(s"uv_run returned $continue")
7359
}
7460
}
7561
}
@@ -192,12 +178,11 @@ object LibUVConstants {
192178

193179
def check(v:Int, label:String):Int = {
194180
if (v == 0) {
195-
println(s"$label returned $v")
196181
v
197182
} else {
198183
val error = fromCString(uv_err_name(v))
199184
val message = fromCString(uv_strerror(v))
200-
println(s"$label returned $v: $error: $message")
185+
println(s"ERROR: $label returned $v: $error: $message")
201186
v
202187
}
203188
}

core/timer.scala

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,16 @@ import scala.Option
1010
import scala.concurrent.duration._
1111
import LibUV._, LibUVConstants._
1212

13-
object Timer extends LoopExtension {
14-
EventLoop.addExtension(this)
13+
object Timer {
1514

1615
var serial = 0L
17-
var timers = mutable.HashMap[Long,Promise[Unit]]()
18-
19-
override def activeRequests():Int =
20-
timers.size
16+
var callbacks = mutable.HashMap[Long,() => Unit]()
2117

2218
def delay(dur:Duration):Future[Unit] = {
2319
val promise = Promise[Unit]()
2420
serial += 1
2521
val timer_id = serial
26-
timers(timer_id) = promise
22+
callbacks(timer_id) = () => promise.success(())
2723
val millis = dur.toMillis
2824

2925
val timer_handle = stdlib.malloc(uv_handle_size(UV_TIMER_T))
@@ -37,13 +33,46 @@ object Timer extends LoopExtension {
3733

3834
val timerCB = new TimerCB {
3935
def apply(handle:TimerHandle):Unit = {
40-
println("callback fired!")
4136
val timer_data = handle.asInstanceOf[Ptr[Long]]
4237
val timer_id = !timer_data
43-
val timer_promise = timers(timer_id)
44-
timers.remove(timer_id)
45-
println(s"completing promise ${timer_id}")
46-
timer_promise.success(())
38+
val callback = callbacks(timer_id)
39+
callbacks.remove(timer_id)
40+
callback()
41+
stdlib.free(handle)
42+
}
43+
}
44+
45+
// low-level, intended for use in scala.js compat layer - leaks memory if
46+
// caller does not free() returned handle
47+
def oneTime(dur:Double, callback: () => Unit):PrepareHandle = {
48+
serial += 1
49+
val timer_id = serial
50+
51+
val timer_handle = stdlib.malloc(uv_handle_size(UV_TIMER_T))
52+
uv_timer_init(EventLoop.loop,timer_handle)
53+
val timer_data = timer_handle.asInstanceOf[Ptr[Long]]
54+
!timer_data = timer_id
55+
56+
callbacks(timer_id) = () => {
57+
callback()
4758
}
59+
60+
uv_timer_start(timer_handle, timerCB, dur.toLong, 0)
61+
timer_handle
62+
}
63+
64+
// low-level, intended for use in scala.js compat layer - leaks memory if
65+
// caller does not free() returned handle
66+
def repeat(dur:Double, callback: () => Unit):PrepareHandle = {
67+
serial += 1
68+
val timer_id = serial
69+
70+
callbacks(timer_id) = () => callback()
71+
val timer_handle = stdlib.malloc(uv_handle_size(UV_TIMER_T))
72+
uv_timer_init(EventLoop.loop,timer_handle)
73+
val timer_data = timer_handle.asInstanceOf[Ptr[Long]]
74+
!timer_data = timer_id
75+
uv_timer_start(timer_handle, timerCB, dur.toLong, dur.toLong)
76+
timer_handle
4877
}
4978
}

pipe/pipe.scala

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,15 @@ case class Handle(serial:Long, handle:Ptr[Byte]) {
3434
}
3535
}
3636

37-
object StreamIO extends LoopExtension {
37+
object StreamIO {
3838
import LibUV._, LibUVConstants._
3939
type ItemHandler = ((String,PipeHandle,Long) => Unit)
4040
type DoneHandler = ((PipeHandle,Long) => Unit)
4141
type Handlers = (ItemHandler, DoneHandler)
4242
var streams = mutable.HashMap[Long,Handlers]()
4343
var serial = 0L
4444

45-
override def activeRequests:Int = {
46-
streams.size
47-
}
48-
49-
var initialized = false
50-
51-
def checkInit() = if (!initialized) {
52-
println("initializing PipeIO")
53-
EventLoop.addExtension(this)
54-
initialized = true
55-
}
56-
5745
def fromPipe(fd:Int):Handle = {
58-
checkInit
5946
val id = serial
6047
serial += 1
6148
val handle = stdlib.malloc(uv_handle_size(UV_PIPE_T))
@@ -66,22 +53,8 @@ object StreamIO extends LoopExtension {
6653
Handle(id,handle)
6754
}
6855

69-
// def fromSocket(fd:Int):Handle = {
70-
// checkInit
71-
// val id = serial
72-
// serial += 1
73-
// val handle = stdlib.malloc(uv_handle_size(UV_TCP_T)).asInstanceOf[Ptr[Long]]
74-
// uv_tcp_init(EventLoop.loop,handle)
75-
// val socket_data = handle
76-
// !socket_data = serial
77-
// // getaddrinfo/uv_connect here
78-
// ???
79-
// // Handle(id,handle)
80-
// }
81-
8256
def stream(fd:Int)(itemHandler:ItemHandler,
8357
doneHandler:DoneHandler):Handle = {
84-
checkInit
8558
val pipeId = serial
8659
serial += 1
8760
val handle = stdlib.malloc(uv_handle_size(UV_PIPE_T))

project/plugins.sbt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.0-SNAPSHOT")
1+
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.0-M2")
2+
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.1")
3+
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "1.1")

server/server.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.scalanative.libc.stdlib._
66
import scala.scalanative.libc.string._
77
import scala.concurrent._
88

9-
object Server extends LoopExtension {
9+
object Server {
1010
import LibUV._, LibUVConstants._
1111
import Parser._
1212

@@ -17,8 +17,6 @@ object Server extends LoopExtension {
1717
val HTTP_RESPONSE = 1
1818
val HTTP_BOTH = 2
1919

20-
override def activeRequests = listeners.size
21-
2220
type ConnectionState = CStruct3[Long,LibUV.TCPHandle,Ptr[HttpParser.Parser]]
2321
type RequestHandler = (RequestState, TCPHandle) => Unit
2422
type WriteState = (Promise[Unit],Ptr[Buffer])
@@ -61,7 +59,6 @@ object Server extends LoopExtension {
6159
}
6260

6361
def init(port:Int)(handler:RequestHandler):Unit = {
64-
EventLoop.addExtension(this)
6562
listeners(port) = handler
6663
val addr = malloc(64)
6764
check(uv_ip4_addr(c"0.0.0.0", 9999, addr),"uv_ip4_addr")

0 commit comments

Comments
 (0)