energy/cef/cef-application-queue-async-call.go

106 lines
2.3 KiB
Go
Raw Normal View History

2022-10-04 13:21:05 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
2022-10-04 13:21:05 +08:00
//
//----------------------------------------
2023-02-26 21:39:42 +08:00
// 在应用主线程中执行(非主线程使用)异步&同步执行包裹函数
2022-10-04 13:21:05 +08:00
package cef
import (
"github.com/energye/energy/common/imports"
"github.com/energye/golcl/lcl/api/dllimports"
"math"
"sync"
2022-10-04 13:21:05 +08:00
)
var (
qac = &queueAsyncCall{id: 0, calls: sync.Map{}}
applicationQueueAsyncCallFunc dllimports.ProcAddr
)
func applicationQueueAsyncCallInit() {
applicationQueueAsyncCallFunc = imports.Proc(internale_SetApplicationQueueAsyncCallFunc)
applicationQueueAsyncCallFunc.Call(applicationQueueAsyncCallEvent)
}
func applicationQueueAsyncCallProc(id uintptr) uintptr {
qac.call(id)
return 0
}
2023-02-26 21:39:42 +08:00
// qacFn 队列异步调用函数 id:事件id
type qacFn func(id int)
type queueCall struct {
IsSync bool
Fn qacFn
Wg *sync.WaitGroup
}
type queueAsyncCall struct {
id uintptr
calls sync.Map
}
2023-02-26 21:39:42 +08:00
// QueueAsyncCall 仅LCL在主进程中异步调用
//
2023-02-26 21:39:42 +08:00
// 在UI主进程中执行, 异步执行
2022-10-04 13:21:05 +08:00
//
2023-02-26 21:39:42 +08:00
// 非主进程的多线程操作可使用该函数包裹
2022-10-04 13:21:05 +08:00
//
2023-02-26 21:39:42 +08:00
// 在任何变更UI的操作都有可能因非主线程出现不一至, 而出现程序错误或程序崩溃, 可以尝试使用该回调函数解决.
2022-10-04 13:21:05 +08:00
//
2023-02-26 21:39:42 +08:00
// 提示: CEF事件或函数中不应使用该函数包裹
func QueueAsyncCall(fn qacFn) int {
id := qac.set(&queueCall{
IsSync: false,
Fn: fn,
})
imports.Proc(internale_CEFApplication_QueueAsyncCall).Call(id)
return int(id)
2022-10-04 13:21:05 +08:00
}
2023-02-26 21:39:42 +08:00
// QueueSyncCall 同 QueueAsyncCall
//
2023-02-26 21:39:42 +08:00
// 同步执行 - 阻塞UI
func QueueSyncCall(fn qacFn) int {
qc := &queueCall{
IsSync: true,
Fn: fn,
Wg: &sync.WaitGroup{},
}
qc.Wg.Add(1)
id := qac.set(qc)
imports.Proc(internale_CEFApplication_QueueAsyncCall).Call(id)
qc.Wg.Wait()
qc.Fn = nil
qc.Wg = nil
qc = nil
return int(id)
}
func (m *queueAsyncCall) call(id uintptr) {
if call, ok := m.calls.LoadAndDelete(id); ok {
qc := call.(*queueCall)
if qc.IsSync {
qc.Fn(int(id))
qc.Wg.Done()
} else {
qc.Fn(int(id))
}
}
}
func (m *queueAsyncCall) set(fn *queueCall) uintptr {
if m.id >= math.MaxUint {
m.id = 0
}
m.id++
m.calls.Store(m.id, fn)
return m.id
2022-10-04 13:21:05 +08:00
}