File tree Expand file tree Collapse file tree 6 files changed +36
-22
lines changed Expand file tree Collapse file tree 6 files changed +36
-22
lines changed Original file line number Diff line number Diff line change @@ -2,16 +2,15 @@ lit s1 9
2
2
str s2 "main.mar"
3
3
syscall
4
4
5
- push r2
6
5
7
- lit s1 12 // pause main.mar
8
- peek s2
6
+ // Run main.mar
7
+ mov r2 r1
8
+ lit s1 12
9
+ mov r1 s2
9
10
syscall
10
11
11
- prints
12
12
13
- lit g1 5000
14
- sleep g1
13
+ // Pause main.mar
15
14
16
15
str s3 "I love dogs!"
17
16
mov s3 f1
@@ -20,13 +19,17 @@ pop g1
20
19
add g1 s3
21
20
22
21
22
+ // Load "I love dogs!" into memory
23
+
23
24
lit s1 11 // mem share
24
25
mov r2 s2 // share VM
25
26
mov r4 s4
26
27
syscall
27
28
29
+ // Share main.kar's memory with main.mar
28
30
29
- lit s1 13 // continue main.mar
30
- peek s2
31
+ lit s1 13
32
+ mov r1 s2
31
33
syscall
32
34
35
+ // Continue main.mar
Original file line number Diff line number Diff line change 1
- lit g1 10
2
- printr g1
3
1
lit f1 9
4
2
call println
Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ import environment.reflection.VmTracked
6
6
import environment.reflection.reflection
7
7
import helpers.Config
8
8
import internals.Vm
9
+ import kotlinx.coroutines.DelicateCoroutinesApi
10
+ import kotlinx.coroutines.ExperimentalCoroutinesApi
11
+ import kotlinx.coroutines.newSingleThreadContext
9
12
import kotlinx.coroutines.runBlocking
10
13
import optimisations.VarRedundancy
11
14
import java.io.File
@@ -18,7 +21,8 @@ val MEMORY_LIMIT = config?.memorySize ?: 256
18
21
val taskManager = TaskManager ()
19
22
val init = Vm ()
20
23
21
- fun main (args : Array <String >): Unit = runBlocking {
24
+ @OptIn(ExperimentalCoroutinesApi ::class , DelicateCoroutinesApi ::class )
25
+ fun main (args : Array <String >): Unit = runBlocking(newSingleThreadContext(" Kotlin's main" )) {
22
26
val tracked = VmTracked (init )
23
27
tracked.thread = Thread .currentThread()
24
28
reflection.vmTracker.add(tracked)
@@ -73,10 +77,7 @@ fun main(args: Array<String>): Unit = runBlocking {
73
77
74
78
taskManager.wait()
75
79
76
-
77
80
}
78
81
79
82
80
- fun exitVM (): Nothing = exitProcess(0 )
81
-
82
83
Original file line number Diff line number Diff line change @@ -34,11 +34,15 @@ class Execute(val vm: Vm) {
34
34
suspend fun run (command : List <InstructData >) {
35
35
while (true ) {
36
36
37
+ // println("${vm.pc} | ${ reflection.vmTracker.groupBy(VmTracked::vm)[vm]!![0].id} - ${vm.runtimeState}")
38
+
39
+
37
40
when (vm.runtimeState) {
38
41
RuntimeStates .RUNNING -> {/* pass */
42
+
39
43
}
40
44
41
- RuntimeStates .PAUSED -> delay( 1 )
45
+ RuntimeStates .PAUSED -> continue
42
46
RuntimeStates .CANCELLED -> break
43
47
}
44
48
Original file line number Diff line number Diff line change @@ -6,19 +6,20 @@ import taskManager
6
6
7
7
8
8
class TaskManager {
9
- private val startTime = System .currentTimeMillis()
10
9
private val taskChannel = Channel < suspend () -> Unit > () // Channel for Unit-returning functions
11
- private val taskScope = CoroutineScope (Dispatchers .Default + SupervisorJob ())
10
+ private val taskScope = CoroutineScope (Dispatchers .IO + SupervisorJob ())
12
11
private lateinit var taskManager: Job
13
12
private val activeTasks = mutableListOf<Job >() // Keep track of active tasks
14
13
15
14
15
+ @OptIn(ExperimentalCoroutinesApi ::class , DelicateCoroutinesApi ::class )
16
16
fun addTask (block : suspend () -> Unit ) {
17
17
taskScope.launch { taskChannel.send(block) }
18
- if (! ::taskManager.isInitialized) { // Start the task manager only once.
18
+ if (! ::taskManager.isInitialized) {
19
19
taskManager = taskScope.launch {
20
20
for (task in taskChannel) {
21
- val taskJob = launch { task() } // Launch each task concurrently
21
+ val taskJob =
22
+ launch(newSingleThreadContext(" Kotlin's slaves \$ ${activeTasks.size + 1 } " )) { task() } // Launch each task concurrently
22
23
activeTasks.add(taskJob)
23
24
}
24
25
activeTasks.joinAll()
@@ -38,6 +39,7 @@ class TaskManager {
38
39
}
39
40
40
41
42
+ @OptIn(ExperimentalCoroutinesApi ::class , DelicateCoroutinesApi ::class )
41
43
fun main () = runBlocking {
42
44
43
45
println (" Main thread doing other work..." )
Original file line number Diff line number Diff line change 1
1
package helpers
2
2
3
+ import kotlinx.coroutines.*
3
4
import kotlinx.serialization.json.Json
4
5
import java.io.File
5
6
6
- class Config (f : File ) {
7
+ class Config (private val f : File ) {
7
8
private var content: ConfigStructure ? = null
8
9
9
10
init {
10
- val contents = f.readText()
11
- content = Json .decodeFromString<ConfigStructure >(contents)
11
+ runBlocking { init () }
12
12
}
13
13
14
14
val hertz = content?.hertz!! .toLong() / 2
15
15
val stackSize = content?.stackSize!!
16
16
val memorySize = content?.memorySize!!
17
17
val paths = content?.locations!!
18
+
19
+ @OptIn(ExperimentalCoroutinesApi ::class , DelicateCoroutinesApi ::class )
20
+ private suspend fun init () = CoroutineScope (Dispatchers .IO ).launch(newSingleThreadContext(" Kotlin's Config Init" )) {
21
+ val contents = f.readText()
22
+ content = Json .decodeFromString<ConfigStructure >(contents)
23
+ }.join()
18
24
}
You can’t perform that action at this time.
0 commit comments