Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: goroutines #19

Merged
merged 8 commits into from
Apr 2, 2024
Merged

feat: goroutines #19

merged 8 commits into from
Apr 2, 2024

Conversation

shenyih0ng
Copy link
Owner

@shenyih0ng shenyih0ng commented Apr 1, 2024

Description

This PR implements support for goroutines (GoStatement) with a naive time-quanta based scheduler. Every goroutine is given a randomised quanta of time to run, and once it runs out it is scheduled out for other goroutines. Additionally, the scheduler also looks for the main goroutine, once the main goroutine terminates, the entire program is terminated.

Implementation Details

  1. Goroutines exist in the same address space, hence a new goroutine does not reinitialise its own Heap, AstMap or Builtins. However, it does set up its own Control, Stash and Environment

    const _C: Control = new Stack()
    _C.push(H.alloc(body))
    const _S: Stash = new Stack()
    const _E = E.copy()
    .setId(envId)
    .extend(Object.fromEntries(zip(paramNames, values)))
    return sched.schedule(new GoRoutine({ C: _C, S: _S, E: _E, B, H, A }, sched))

  2. I've also added parser support for strings (StringLit) just so i can print string to stdout to differentiate between goroutines. Note that there is still no string pools and strings do not yet get allocated on the heap! This is purely a convenience feature for debugging

Screen.Recording.2024-04-02.at.11.52.54.AM.mov

Abstracted the current program as a `main` go routine in preparation for
`GoStatement` implementation
@shenyih0ng shenyih0ng self-assigned this Apr 1, 2024
Right now we only support non-builtin calls to spawn a goroutine. We
need to consider if its even worth it spawn a new goroutine for a
builtin since its only a "1 step/tick" op. Additionally, I introduced
`GoRoutineOp` to handle spawning of new goroutines. Its payload is
same as `CallOp` so we can consider combining them if there is no
special cases to handle later on
This is mainly so that we can `println` strings to differentiate between
goroutines. Note that this is purely a convenience feature, the ECE does
not allocate strings in the heap/string pool right now.
@shenyih0ng shenyih0ng marked this pull request as ready for review April 1, 2024 11:17
@shenyih0ng shenyih0ng requested a review from alvynben April 2, 2024 03:54
@shenyih0ng
Copy link
Owner Author

Majority of the diffs come from the changes to generated parser after adding StringLit support, the actual goroutine implementation is ~300-400 lines of changes

@shenyih0ng shenyih0ng merged commit ce87563 into master Apr 2, 2024
1 check passed
@shenyih0ng shenyih0ng deleted the feat/goroutines branch April 2, 2024 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant