Skip to content

Commit 3ed7b64

Browse files
committed
complete. Not stable!
1 parent 02967aa commit 3ed7b64

File tree

6 files changed

+36
-22
lines changed

6 files changed

+36
-22
lines changed

main.kar

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ lit s1 9
22
str s2 "main.mar"
33
syscall
44

5-
push r2
65

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
910
syscall
1011

11-
prints
1212

13-
lit g1 5000
14-
sleep g1
13+
// Pause main.mar
1514

1615
str s3 "I love dogs!"
1716
mov s3 f1
@@ -20,13 +19,17 @@ pop g1
2019
add g1 s3
2120

2221

22+
// Load "I love dogs!" into memory
23+
2324
lit s1 11 // mem share
2425
mov r2 s2 // share VM
2526
mov r4 s4
2627
syscall
2728

29+
// Share main.kar's memory with main.mar
2830

29-
lit s1 13 // continue main.mar
30-
peek s2
31+
lit s1 13
32+
mov r1 s2
3133
syscall
3234

35+
// Continue main.mar

main.mar

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
lit g1 10
2-
printr g1
31
lit f1 9
42
call println

src/main/kotlin/Main.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import environment.reflection.VmTracked
66
import environment.reflection.reflection
77
import helpers.Config
88
import internals.Vm
9+
import kotlinx.coroutines.DelicateCoroutinesApi
10+
import kotlinx.coroutines.ExperimentalCoroutinesApi
11+
import kotlinx.coroutines.newSingleThreadContext
912
import kotlinx.coroutines.runBlocking
1013
import optimisations.VarRedundancy
1114
import java.io.File
@@ -18,7 +21,8 @@ val MEMORY_LIMIT = config?.memorySize ?: 256
1821
val taskManager = TaskManager()
1922
val init = Vm()
2023

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")) {
2226
val tracked = VmTracked(init)
2327
tracked.thread = Thread.currentThread()
2428
reflection.vmTracker.add(tracked)
@@ -73,10 +77,7 @@ fun main(args: Array<String>): Unit = runBlocking {
7377

7478
taskManager.wait()
7579

76-
7780
}
7881

7982

80-
fun exitVM(): Nothing = exitProcess(0)
81-
8283

src/main/kotlin/engine/execution/Execute.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ class Execute(val vm: Vm) {
3434
suspend fun run(command: List<InstructData>) {
3535
while (true) {
3636

37+
// println("${vm.pc} | ${ reflection.vmTracker.groupBy(VmTracked::vm)[vm]!![0].id} - ${vm.runtimeState}")
38+
39+
3740
when (vm.runtimeState) {
3841
RuntimeStates.RUNNING -> {/* pass */
42+
3943
}
4044

41-
RuntimeStates.PAUSED -> delay(1)
45+
RuntimeStates.PAUSED -> continue
4246
RuntimeStates.CANCELLED -> break
4347
}
4448

src/main/kotlin/environment/TaskManager.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ import taskManager
66

77

88
class TaskManager {
9-
private val startTime = System.currentTimeMillis()
109
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())
1211
private lateinit var taskManager: Job
1312
private val activeTasks = mutableListOf<Job>() // Keep track of active tasks
1413

1514

15+
@OptIn(ExperimentalCoroutinesApi::class, DelicateCoroutinesApi::class)
1616
fun addTask(block: suspend () -> Unit) {
1717
taskScope.launch { taskChannel.send(block) }
18-
if (!::taskManager.isInitialized) { //Start the task manager only once.
18+
if (!::taskManager.isInitialized) {
1919
taskManager = taskScope.launch {
2020
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
2223
activeTasks.add(taskJob)
2324
}
2425
activeTasks.joinAll()
@@ -38,6 +39,7 @@ class TaskManager {
3839
}
3940

4041

42+
@OptIn(ExperimentalCoroutinesApi::class, DelicateCoroutinesApi::class)
4143
fun main() = runBlocking {
4244

4345
println("Main thread doing other work...")

src/main/kotlin/helpers/Config.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
package helpers
22

3+
import kotlinx.coroutines.*
34
import kotlinx.serialization.json.Json
45
import java.io.File
56

6-
class Config(f: File) {
7+
class Config(private val f: File) {
78
private var content: ConfigStructure? = null
89

910
init {
10-
val contents = f.readText()
11-
content = Json.decodeFromString<ConfigStructure>(contents)
11+
runBlocking { init() }
1212
}
1313

1414
val hertz = content?.hertz!!.toLong() / 2
1515
val stackSize = content?.stackSize!!
1616
val memorySize = content?.memorySize!!
1717
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()
1824
}

0 commit comments

Comments
 (0)