Added futures to kraken!!!
Added a templated future class that wraps the pthread library to allow multithreaded development.
This commit is contained in:
79
stdlib/future.krak
Normal file
79
stdlib/future.krak
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
import io:*
|
||||||
|
|
||||||
|
__if_comp__ __C__ simple_passthrough(::"-pthread") """
|
||||||
|
#include <pthread.h>
|
||||||
|
"""
|
||||||
|
|
||||||
|
def pthread_t float
|
||||||
|
|
||||||
|
fun pthread_create(thrd : *pthread_t, strt_routine : fun() : void) : int {
|
||||||
|
__if_comp__ __C__ {
|
||||||
|
simple_passthrough(thrd,strt_routine::) """
|
||||||
|
pthread_attr_t attr;
|
||||||
|
pthread_attr_init(&attr);
|
||||||
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
||||||
|
int ret = pthread_create((pthread_t*)thrd, &attr, strt_routine.func, strt_routine.data);
|
||||||
|
pthread_attr_destroy(&attr);
|
||||||
|
return ret;
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
fun pthread_join(thrd : *pthread_t) : int {
|
||||||
|
__if_comp__ __C__ { simple_passthrough(thrd::) """
|
||||||
|
pthread_t thread = *((pthread_t*)thrd);
|
||||||
|
return pthread_join(thread, NULL);
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
fun pthread_exit() : *void {
|
||||||
|
__if_comp__ __C__ { simple_passthrough """
|
||||||
|
pthread_exit(NULL);
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
fun future<T>(in : fun() : T ) : future<T> {
|
||||||
|
var out.construct(in) : future<T>
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
obj future<T> {
|
||||||
|
var result : T
|
||||||
|
var status : int
|
||||||
|
var psy : fun() : T
|
||||||
|
var wrapper : fun() : void
|
||||||
|
var thread : pthread_t
|
||||||
|
|
||||||
|
fun construct(in : fun() : T) : *future<T> {
|
||||||
|
status = 0
|
||||||
|
psy = in
|
||||||
|
wrapper = fun() : void{
|
||||||
|
result = psy()
|
||||||
|
println(status)
|
||||||
|
pthread_exit()
|
||||||
|
}
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
fun run() {
|
||||||
|
status = pthread_create(&thread,wrapper)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun get_status() {
|
||||||
|
return status
|
||||||
|
}
|
||||||
|
|
||||||
|
fun finish() : T {
|
||||||
|
pthread_join(&thread)
|
||||||
|
print("FINISHED THREAD ")
|
||||||
|
print(result)
|
||||||
|
print("\n")
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
4
tests/test_future.expected_results
Normal file
4
tests/test_future.expected_results
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
Thread 0 Started!
|
||||||
|
0
|
||||||
|
FINISHED THREAD 0
|
||||||
|
Threads have finished
|
||||||
29
tests/test_future.krak
Normal file
29
tests/test_future.krak
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import future:*
|
||||||
|
import io:*
|
||||||
|
|
||||||
|
|
||||||
|
fun main() : int {
|
||||||
|
var a = 0
|
||||||
|
var c = fun() : int {
|
||||||
|
var b = a
|
||||||
|
print("Thread ")
|
||||||
|
print(b)
|
||||||
|
println(" Started!")
|
||||||
|
var x : int = 0
|
||||||
|
for(var i = 1; i < 100; i++;) {
|
||||||
|
x++
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
var gadget1.construct(c) : future<int>
|
||||||
|
gadget1.run()
|
||||||
|
gadget1.finish()
|
||||||
|
|
||||||
|
println("Threads have finished")
|
||||||
|
|
||||||
|
pthread_exit()
|
||||||
|
println("All Done!")
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user