thread.go (1181B)
1 // Copyright 2020 The Ebiten Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package graphicscommand 16 17 var theThread Thread 18 19 type Thread interface { 20 Call(f func() error) error 21 } 22 23 // SetMainThread must be called from the main thread (i.e, the goroutine where the thread is created). 24 func SetMainThread(thread Thread) { 25 theThread = thread 26 } 27 28 func runOnMainThread(f func() error) error { 29 // The thread is nil when 1) GOOS=js or 2) using golang.org/x/mobile/gl. 30 // When golang.org/x/mobile/gl is used, all the GL functions are called via Context, which already runs on an 31 // appropriate thread. 32 if theThread == nil { 33 return f() 34 } 35 return theThread.Call(f) 36 }