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

WIP: threading support #4559

Draft
wants to merge 17 commits into
base: dev
Choose a base branch
from
Draft

WIP: threading support #4559

wants to merge 17 commits into from

Commits on Oct 29, 2024

  1. internal/task: add non-atomic atomic operations

    This adds some non-atomic types that have the same interface as the ones
    in sync/atomic.
    
    We currently don't need these to be atomic (because the scheduler is
    entirely cooperative), but once we add support for a scheduler with
    multiple threads and/or preemptive scheduling we can trivially add some
    type aliases under a different build tag in the future when real
    atomicity is needed for threading.
    aykevl committed Oct 29, 2024
    Configuration menu
    Copy the full SHA
    83dac2f View commit details
    Browse the repository at this point in the history
  2. runtime: prepare the leaking GC for concurrent operations

    This uses the internal/llsync package to convert some non-atomic
    operations to pseudo-atomic operations that will become truly atomic on
    systems that need this (such as multicore chips and when we implement
    true parallelism on Linux).
    aykevl committed Oct 29, 2024
    Configuration menu
    Copy the full SHA
    1be9858 View commit details
    Browse the repository at this point in the history

Commits on Nov 1, 2024

  1. runtime: move scheduler code around

    This moves all scheduler code into a separate file that is only compiled
    when there's a scheduler in use (the tasks or asyncify scheduler, which
    are both cooperative). The main goal of this change is to make it easier
    to add a new "scheduler" based on OS threads.
    
    It also fixes a few subtle issues with `-gc=none`:
    
      - Gosched() panicked. This is now fixed to just return immediately
        (the only logical thing to do when there's only one goroutine).
      - Timers aren't supported without a scheduler, but the relevant code
        was still present and would happily add a timer to the queue. It
        just never ran. So now it exits with a runtime error, similar to any
        blocking operation.
    aykevl committed Nov 1, 2024
    Configuration menu
    Copy the full SHA
    0bc7d3e View commit details
    Browse the repository at this point in the history
  2. internal/task: add cooperative implementation of Futex

    See the code comments for details. But in short, this implements a futex
    for the cooperative scheduler (that is single threaded and
    non-reentrant). Similar implementations can be made for basically every
    other operating system, and even WebAssembly with the threading
    (actually: atomics) proposal.
    Using this basic futex implementation means we can use the same
    implementation for synchronisation primitives on cooperative and
    multicore systems.
    
    For more information on futex across operating systems:
    https://outerproduct.net/futex-dictionary.html
    aykevl committed Nov 1, 2024
    Configuration menu
    Copy the full SHA
    00980b5 View commit details
    Browse the repository at this point in the history
  3. internal/task: implement PMutex

    PMutex is a mutex when threading is possible, and a dummy mutex-like
    object (that doesn't do anything) otherwise.
    aykevl committed Nov 1, 2024
    Configuration menu
    Copy the full SHA
    c0bdb4a View commit details
    Browse the repository at this point in the history
  4. sync: implement WaitGroup using a futex

    Code size for the cooperative scheduler is nearly unchanged.
    aykevl committed Nov 1, 2024
    Configuration menu
    Copy the full SHA
    c48233d View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    efd60ca View commit details
    Browse the repository at this point in the history
  6. sync: make Pool thread-safe

    Make sure the object is locked when trying to modify it.
    Binary size seems unaffected when not using threading.
    aykevl committed Nov 1, 2024
    Configuration menu
    Copy the full SHA
    18443c1 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    2c036dc View commit details
    Browse the repository at this point in the history

Commits on Nov 3, 2024

  1. Configuration menu
    Copy the full SHA
    98e7150 View commit details
    Browse the repository at this point in the history
  2. runtime: refactor GC mark phase into gcMarkReachable

    This is a small refactor to prepare GC marking for multithreaded
    stop-the-world.
    aykevl committed Nov 3, 2024
    Configuration menu
    Copy the full SHA
    5f0f814 View commit details
    Browse the repository at this point in the history
  3. runtime: make conservative and precise GC MT-safe

    Using a global lock may be slow, but it is certainly simple and safe.
    If this global lock becomes a bottleneck, we can of course look into
    making the GC truly support multithreading.
    aykevl committed Nov 3, 2024
    Configuration menu
    Copy the full SHA
    b927048 View commit details
    Browse the repository at this point in the history
  4. runtime: refactor timerQueue

    Move common functions to scheduler.go. They will be used both from the
    cooperative and from the threads scheduler.
    aykevl committed Nov 3, 2024
    Configuration menu
    Copy the full SHA
    bca037e View commit details
    Browse the repository at this point in the history
  5. sync: implement RWMutex using futexes

    Somewhat surprisingly, this results in smaller code than the old code
    with the cooperative (tasks) scheduler. Probably because the new RWMutex
    is also simpler.
    aykevl committed Nov 3, 2024
    Configuration menu
    Copy the full SHA
    341e6d4 View commit details
    Browse the repository at this point in the history

Commits on Nov 4, 2024

  1. sync: make Cond MT-safe

    This actually simplifies the code and avoids a heap allocation in the
    call to Wait. Instead, it uses the Data field of the task to store
    information on whether a task was signalled early.
    aykevl committed Nov 4, 2024
    Configuration menu
    Copy the full SHA
    036e8a5 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    6ae73cb View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    d328dd4 View commit details
    Browse the repository at this point in the history