Skip to content

Instantly share code, notes, and snippets.

@nadavwr
Last active June 8, 2018 11:59
Show Gist options
  • Save nadavwr/f82e9d9187c617bbd6a52465c76d5eb1 to your computer and use it in GitHub Desktop.
Save nadavwr/f82e9d9187c617bbd6a52465c76d5eb1 to your computer and use it in GitHub Desktop.
Scala Native pthread attempt
import scala.scalanative.native._
import scala.scalanative.runtime.GC
@extern
@link("pthread")
object pthread {
type pthread_t = Ptr[CStruct0]
type pthread_attr_t = CStruct0
def pthread_create(thread: Ptr[pthread_t], attr: Ptr[pthread_attr_t],
start_routine: CFunctionPtr1[Ptr[Byte], Ptr[Byte]], arg: Ptr[Byte]): CInt = extern
def pthread_join(thread: pthread_t, value_ptr: Ptr[Ptr[Byte]]): CInt = extern
}
object Main extends App {
import pthread._
val EPERM = 1
val EINVAL = 22
val EAGAIN = 35
val f: CFunctionPtr1[Ptr[Byte], Ptr[Byte]] =
(p: Ptr[Byte]) => {
println("Entering thread")
Thread.sleep(1000)
println("Leaving thread")
null
}
val thread = stackalloc[pthread_t]
pthread_create(thread, null, f, null)
pthread_join(!thread, null)
println("done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment