mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
graphicsdriver/metal/mtl: Bug fix: Guard the handler map by a mutex
The handler is invoked in another thread than the main thread.
This commit is contained in:
parent
170ddd70a5
commit
aaff18ecf7
@ -27,6 +27,7 @@ package mtl
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@ -568,20 +569,30 @@ func (cb CommandBuffer) WaitUntilCompleted() {
|
||||
C.CommandBuffer_WaitUntilCompleted(cb.commandBuffer)
|
||||
}
|
||||
|
||||
var commandBufferCompletedHandlers = map[unsafe.Pointer]func(){}
|
||||
var (
|
||||
commandBufferCompletedHandlers = map[unsafe.Pointer]func(){}
|
||||
commandBufferCompletedHandlersM sync.Mutex
|
||||
)
|
||||
|
||||
// AddCompletedHandler registers a block of code that Metal calls immediately after the GPU finishes executing the commands in the command buffer.
|
||||
//
|
||||
// Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1442997-addcompletedhandler
|
||||
func (cb CommandBuffer) AddCompletedHandler(f func()) {
|
||||
commandBufferCompletedHandlersM.Lock()
|
||||
commandBufferCompletedHandlers[cb.commandBuffer] = f
|
||||
commandBufferCompletedHandlersM.Unlock()
|
||||
|
||||
C.CommandBuffer_AddCompletedHandler(cb.commandBuffer)
|
||||
}
|
||||
|
||||
//export commandBufferCompletedCallback
|
||||
func commandBufferCompletedCallback(commandBuffer unsafe.Pointer) {
|
||||
f := commandBufferCompletedHandlers[commandBuffer]
|
||||
|
||||
commandBufferCompletedHandlersM.Lock()
|
||||
delete(commandBufferCompletedHandlers, commandBuffer)
|
||||
commandBufferCompletedHandlersM.Unlock()
|
||||
|
||||
f()
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user