mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 03:02:49 +01:00
thread: Use standard context.Context
This commit is contained in:
parent
1e93d9c699
commit
449679665b
@ -15,6 +15,7 @@
|
|||||||
package thread
|
package thread
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -36,17 +37,17 @@ func New() *Thread {
|
|||||||
// Loop starts the thread loop.
|
// Loop starts the thread loop.
|
||||||
//
|
//
|
||||||
// Loop must be called on the thread.
|
// Loop must be called on the thread.
|
||||||
func (t *Thread) Loop(ch <-chan error) error {
|
func (t *Thread) Loop(context context.Context) {
|
||||||
atomic.StoreInt32(&t.started, 1)
|
atomic.StoreInt32(&t.started, 1)
|
||||||
defer atomic.StoreInt32(&t.started, 0)
|
defer atomic.StoreInt32(&t.started, 0)
|
||||||
|
|
||||||
|
loop:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case f := <-t.funcs:
|
case f := <-t.funcs:
|
||||||
f()
|
f()
|
||||||
case err := <-ch:
|
case <-context.Done():
|
||||||
// ch returns a value not only when an error occur but also it is closed.
|
break loop
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
package glfw
|
package glfw
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"image"
|
"image"
|
||||||
"math"
|
"math"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -171,11 +172,9 @@ func getCachedMonitor(wx, wy int) (*cachedMonitor, bool) {
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) mainThreadLoop(ch <-chan error) error {
|
func (u *UserInterface) mainThreadLoop(context context.Context) error {
|
||||||
u.setRunning(true)
|
u.setRunning(true)
|
||||||
if err := u.t.Loop(ch); err != nil {
|
u.t.Loop(context)
|
||||||
return err
|
|
||||||
}
|
|
||||||
u.setRunning(false)
|
u.setRunning(false)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -561,22 +560,23 @@ func init() {
|
|||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) Run(width, height int, scale float64, title string, context driver.UIContext, graphics driver.Graphics) error {
|
func (u *UserInterface) Run(width, height int, scale float64, title string, uicontext driver.UIContext, graphics driver.Graphics) error {
|
||||||
// Initialize the main thread first so the thread is available at u.run (#809).
|
// Initialize the main thread first so the thread is available at u.run (#809).
|
||||||
u.t = thread.New()
|
u.t = thread.New()
|
||||||
|
|
||||||
ch := make(chan error)
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
ch := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer cancel()
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
if err := u.run(width, height, scale, title, context, graphics); err != nil {
|
if err := u.run(width, height, scale, title, uicontext, graphics); err != nil {
|
||||||
ch <- err
|
ch <- err
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err := u.mainThreadLoop(ch); err != nil {
|
u.mainThreadLoop(ctx)
|
||||||
return err
|
return <-ch
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) RunWithoutMainLoop(width, height int, scale float64, title string, context driver.UIContext, graphics driver.Graphics) <-chan error {
|
func (u *UserInterface) RunWithoutMainLoop(width, height int, scale float64, title string, context driver.UIContext, graphics driver.Graphics) <-chan error {
|
||||||
|
Loading…
Reference in New Issue
Block a user