Files
openchamber-android-native/third_party/mygo/internal/raster/team.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

80 lines
1.8 KiB
Go

package raster
import (
"sync"
"sync/atomic"
)
// A team does a piece of work on several cores: the goroutine asking for
// it, and helpers waiting for work, started the first time they are needed
// and kept, shared by every team. Its parts are taken in turn, as they
// differ in cost. A frame drawn on several cores so allocates nothing, as
// goroutines started for each area did (a closure each, and what they
// shared).
type team struct {
// do does a part of the work as a member, 0 for the goroutine asking:
// set once, as a method value allocates. It must not run a team.
do func(member, part int)
parts int
next atomic.Int32
wg sync.WaitGroup
// shares are what the helpers of a run get, a member each.
shares [maxWorkers]share
}
type share struct {
t *team
member int
}
var (
// helped are the shares of runs, which helpers take.
helped = make(chan *share, maxWorkers)
helpers atomic.Int32
hiring sync.Mutex
)
// run does parts parts of the work on members cores at most.
func (t *team) run(members, parts int) {
members = max(min(members, parts, maxWorkers), 1)
t.parts = parts
t.next.Store(0)
hire(members - 1)
t.wg.Add(members - 1)
for m := 1; m < members; m++ {
t.shares[m] = share{t, m}
helped <- &t.shares[m]
}
t.work(0)
t.wg.Wait()
}
// work does parts until none is left.
func (t *team) work(member int) {
for p := int(t.next.Add(1)) - 1; p < t.parts; p = int(t.next.Add(1)) - 1 {
t.do(member, p)
}
}
// hire starts helpers until there are n.
func hire(n int) {
if int(helpers.Load()) >= n {
return
}
hiring.Lock()
defer hiring.Unlock()
for int(helpers.Load()) < n {
helpers.Add(1)
go help()
}
}
// help does the shares of runs, as long as the program runs.
func help() {
for s := range helped {
s.t.work(s.member)
s.t.wg.Done()
}
}