Chez-CPS: Implement threads that can use handlers of the main thread - #1415
Draft
martin-ilgner wants to merge 4 commits into
Draft
Chez-CPS: Implement threads that can use handlers of the main thread#1415martin-ilgner wants to merge 4 commits into
martin-ilgner wants to merge 4 commits into
Conversation
Contributor
Author
|
Example Program: extern type Thread
extern type ThreadBoundary
extern chezCPS """
; Block, Boundary -> b
(define (with-boundary prog b)
(prog (make-meta-cont top-level-k
(gensym "thread")
(create-store)
(make-thread-boundary (thread-boundary-mutex b)
(thread-boundary-rest b)))
top-level-k))
"""
extern def fork[T](boundary: ThreadBoundary){p: => T} at async: Thread =
chezCPS "(k (fork-thread (lambda () (with-boundary ${p} ${boundary}))) ks)"
extern def initializeThreadBoundary() at async: ThreadBoundary =
chezCPS "(k (make-thread-boundary (make-mutex) ks) ks)"
extern def join(thread: Thread): Unit =
chezCPS "(thread-join ${thread})"
// For debugging
extern def displayMetaCont() at async: Unit =
chezCPS """
(begin
(display "Metacontinuation:\n")
(display ks)
(display "\n\n")
(k (void) ks))
"""
// Rules:
// - Threads accessing a handler of the main thread have to pass a thread boundary
// - The same thread boundary has to be used by all threads that use this handler/part of the main thread metacontinuation
// - The handler HAS TO RESUME
// - the main thread has to join
def main(): Unit = {
try {
val boundary = initializeThreadBoundary()
val t1 = fork(boundary){
println("Thread 1")
do emit("1")
do emit("2")
println("Thread 1 terminated!")
}
val t2 = fork(boundary){
println("Thread 2")
do emit("11")
do emit("22")
}
// do emit("main")
join(t1)
join(t2)
println("Joined!")
} with emit[String] { x =>
resume(())
println(x)
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
*with many restrictions