升级 golcl

This commit is contained in:
杨红岩 2022-10-07 01:31:12 +08:00
parent a679b16904
commit c2a0bf89a3
6 changed files with 13 additions and 111 deletions

View File

@ -10,42 +10,9 @@ package cef
//应用主线程异步回调
import (
. "github.com/energye/energy/commons"
"github.com/energye/energy/logger"
"github.com/energye/golcl/dylib"
"github.com/energye/golcl/lcl/api"
"math"
"sync"
"github.com/energye/golcl/lcl"
)
var (
qac = &queueAsyncCall{id: 0, calls: sync.Map{}}
applicationQueueAsyncCallFunc *dylib.LazyProc
)
func applicationQueueAsyncCallInit() {
applicationQueueAsyncCallFunc = api.GetLibLCL().NewProc("SetApplicationQueueAsyncCallFunc")
applicationQueueAsyncCallFunc.Call(applicationQueueAsyncCallEvent)
}
func applicationQueueAsyncCallProc(id uintptr) uintptr {
qac.call(id)
return 0
}
// 队列异步调用函数 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
}
// 1.在UI主进程中执行, 队列异步调用-适用大多场景(包括UI线程和非UI线程)
//
// 2.大多数非UI线程操作都需要使用该函数
@ -53,18 +20,8 @@ type queueAsyncCall struct {
// 3.在任何变更UI的操作都有可能导致UI线程不一至出现程序错误或程序崩溃, 可以尝试使用该回调函数解决.
//
// 4.在windows linux macos 可同时使用
func QueueAsyncCall(fn QacFn) int {
defer func() {
if err := recover(); err != nil {
logger.Error("QueueAsyncCall Error:", err)
}
}()
id := qac.set(&queueCall{
isSync: false,
fn: fn,
})
Proc("CEFApplication_QueueAsyncCall").Call(id)
return int(id)
func QueueAsyncCall(fn lcl.QacFn) int {
return lcl.QueueAsyncCall(fn)
}
// 1.在UI主进程中执行, 队列异步调用-适用大多场景(非UI线程)
@ -74,48 +31,6 @@ func QueueAsyncCall(fn QacFn) int {
// 3.在任何变更UI的操作都有可能导致UI线程不一至出现程序错误或程序崩溃, 可以尝试使用该回调函数解决.
//
// 4.在windows linux macos 需要注意使用场景, 当非UI线程使用时正常执行, UI线程使用时会造成UI线程锁死, 这种情况建议使用 QueueAsyncCall 自己增加同步锁
func QueueSyncCall(fn QacFn) int {
defer func() {
if err := recover(); err != nil {
logger.Error("QueueSyncCall Error:", err)
}
}()
qc := &queueCall{
isSync: true,
fn: fn,
wg: &sync.WaitGroup{},
}
qc.wg.Add(1)
id := qac.set(qc)
Proc("CEFApplication_QueueAsyncCall").Call(id)
qc.wg.Wait()
qc.fn = nil
qc.wg = nil
qc = nil
return int(id)
}
func (m *queueAsyncCall) call(id uintptr) {
defer func() {
if err := recover(); err != nil {
logger.Error("QueueSyncCall Call Error:", err)
}
}()
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
func QueueSyncCall(fn lcl.QacFn) int {
return lcl.QueueSyncCall(fn)
}

View File

@ -24,11 +24,6 @@ package cef
// static void* doGetCefWindowBindEventAddr() {
// return &doCefWindowBindEventProc;
// }
//
// extern void* doApplicationQueueAsyncCallEventProc(void* f);
// static void* doGetApplicationQueueAsyncCallEventAddr() {
// return &doApplicationQueueAsyncCallEventProc;
// }
import "C"
import (
@ -47,14 +42,7 @@ func doCefWindowBindEventProc(f unsafe.Pointer, args unsafe.Pointer, argcount C.
return nullptr
}
//export doApplicationQueueAsyncCallEventProc
func doApplicationQueueAsyncCallEventProc(f unsafe.Pointer) unsafe.Pointer {
applicationQueueAsyncCallProc(uintptr(f))
return nullptr
}
var (
cefIPCCallbackFuncEvent = uintptr(C.doGetCefIPCCallbackFuncEventAddr())
cefWindowBindEvent = uintptr(C.doGetCefWindowBindEventAddr())
applicationQueueAsyncCallEvent = uintptr(C.doGetApplicationQueueAsyncCallEventAddr())
cefIPCCallbackFuncEvent = uintptr(C.doGetCefIPCCallbackFuncEventAddr())
cefWindowBindEvent = uintptr(C.doGetCefWindowBindEventAddr())
)

View File

@ -11,7 +11,6 @@ package cef
import "syscall"
var (
cefIPCCallbackFuncEvent = syscall.NewCallback(cefIPCEventProc)
cefWindowBindEvent = syscall.NewCallback(cefWindowBindCallbackEventProc)
applicationQueueAsyncCallEvent = syscall.NewCallback(applicationQueueAsyncCallProc)
cefIPCCallbackFuncEvent = syscall.NewCallback(cefIPCEventProc)
cefWindowBindEvent = syscall.NewCallback(cefWindowBindCallbackEventProc)
)

View File

@ -47,7 +47,7 @@ func GlobalCEFInit(libs *embed.FS, resources *embed.FS) {
//macos的命令行设置
setMacOSXCommandLine(GoStrToDStr(Args.CommandLine()))
//在主应用UI线程中执行以队列异步方式调用的UI线程执行回调函数
applicationQueueAsyncCallInit()
//applicationQueueAsyncCallInit()
//通用对象初始化
CommonInstanceInit()
//对于go绑定到v8引擎js的事件处理函数

2
go.mod
View File

@ -2,4 +2,4 @@ module github.com/energye/energy
go 1.18
require github.com/energye/golcl v0.0.0-20221006075803-dbb5e8f09580
require github.com/energye/golcl v0.0.0-20221006170558-3f7f584ba009

4
go.sum
View File

@ -1,2 +1,2 @@
github.com/energye/golcl v0.0.0-20221006075803-dbb5e8f09580 h1:1ORmWBsyH3hUbYxgwIN/uGBkIxH+nINx5++wpOoKxw0=
github.com/energye/golcl v0.0.0-20221006075803-dbb5e8f09580/go.mod h1:8JYrNlYBZ+XbHA99DUWvj5CqIp8txgYvMjL7ipAtLDE=
github.com/energye/golcl v0.0.0-20221006170558-3f7f584ba009 h1:0pqAGtOT+3ea4q7NA4GW7JJZSel9x9qjtbI0Qs7a7qY=
github.com/energye/golcl v0.0.0-20221006170558-3f7f584ba009/go.mod h1:8JYrNlYBZ+XbHA99DUWvj5CqIp8txgYvMjL7ipAtLDE=