forked from cztomczak/cef2go
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtask.go
73 lines (56 loc) · 1.54 KB
/
task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2014 The cef2go authors. All rights reserved.
// License: BSD 3-clause.
// Website: https://github.com/fromkeith/cef2go
package cef2go
/*
#include <stdlib.h>
#include "include/capi/cef_task_capi.h"
extern void initialize_task(struct _cef_task_t* self);
extern void helper_cef_post_task(cef_thread_id_t threadId, struct _cef_task_t* what);
*/
import "C"
import (
"unsafe"
"sync"
)
type ThreadId int
const (
TID_UI ThreadId = iota
TID_DB
TID_FILE
TID_FILE_USER_BLOCKING
TID_PROCESS_LAUNCHER
TID_CACHE
TID_IO
TID_RENDERER
)
var (
taskMap = make(map[unsafe.Pointer]TaskToExecute)
postTaskLock sync.Mutex
)
type TaskToExecute func()
//export go_TaskExecute
func go_TaskExecute(self *C.struct__cef_task_t) {
postTaskLock.Lock()
toExecute, ok := taskMap[unsafe.Pointer(self)]
if ok {
delete(taskMap, unsafe.Pointer(self))
}
postTaskLock.Unlock()
if ok {
toExecute()
}
}
// allows you to execute a task on the specified thread
func PostTask(thread ThreadId, t TaskToExecute) {
taskT := (*C.struct__cef_task_t)(
C.calloc(1, C.sizeof_struct__cef_task_t))
C.initialize_task(taskT)
go_AddRef(unsafe.Pointer(taskT))
// not defering the unlock, since i don't know if cef might immedialty execute it,
// and thus we deadlock ourselves.
postTaskLock.Lock()
taskMap[unsafe.Pointer(taskT)] = t
postTaskLock.Unlock()
C.helper_cef_post_task(C.cef_thread_id_t(thread), taskT)
}