-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_caller.go
171 lines (143 loc) · 5.86 KB
/
context_caller.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package service
import (
"fmt"
"git.golaxy.org/core/ec"
"git.golaxy.org/core/internal/concurrent"
"git.golaxy.org/core/util/generic"
"git.golaxy.org/core/util/uid"
_ "unsafe"
)
type (
Ret = concurrent.Ret // 调用结果
AsyncRet = concurrent.AsyncRet // 异步调用结果
)
// Caller 异步调用发起者
type Caller interface {
// Call 查找实体并异步调用函数,有返回值。不会阻塞当前线程,会返回AsyncRet。
//
// 注意:
// - 代码片段中的线程安全问题,如临界区访问、线程死锁等。
// - 调用过程中的panic信息,均会转换为error返回。
Call(entityId uid.Id, fun generic.FuncVar1[ec.Entity, any, Ret], va ...any) AsyncRet
// CallDelegate 查找实体并异步调用委托,有返回值。不会阻塞当前线程,会返回AsyncRet。
//
// 注意:
// - 代码片段中的线程安全问题,如临界区访问、线程死锁等。
// - 调用过程中的panic信息,均会转换为error返回。
CallDelegate(entityId uid.Id, fun generic.DelegateFuncVar1[ec.Entity, any, Ret], va ...any) AsyncRet
// CallVoid 查找实体并异步调用函数,无返回值。在运行时中。不会阻塞当前线程,会返回AsyncRet。
//
// 注意:
// - 代码片段中的线程安全问题,如临界区访问、线程死锁等。
// - 调用过程中的panic信息,均会转换为error返回。
CallVoid(entityId uid.Id, fun generic.ActionVar1[ec.Entity, any], va ...any) AsyncRet
// CallVoidDelegate 查找实体并异步调用委托,无返回值。在运行时中。不会阻塞当前线程,会返回AsyncRet。
//
// 注意:
// - 代码片段中的线程安全问题,如临界区访问、线程死锁等。
// - 调用过程中的panic信息,均会转换为error返回。
CallVoidDelegate(entityId uid.Id, fun generic.DelegateActionVar1[ec.Entity, any], va ...any) AsyncRet
}
//go:linkname getCaller git.golaxy.org/core/runtime.getCaller
func getCaller(provider concurrent.ConcurrentContextProvider) concurrent.Caller
func makeAsyncErr(err error) AsyncRet {
asyncRet := concurrent.MakeAsyncRet()
asyncRet <- concurrent.MakeRet(nil, err)
close(asyncRet)
return asyncRet
}
func checkEntity(entity ec.Entity) error {
if entity.GetState() >= ec.EntityState_Leave {
return fmt.Errorf("%w: entity not living", ErrContext)
}
return nil
}
// Call 查找实体并异步调用函数,有返回值。不会阻塞当前线程,会返回AsyncRet。
//
// 注意:
// - 代码片段中的线程安全问题,如临界区访问、线程死锁等。
// - 调用过程中的panic信息,均会转换为error返回。
func (ctx *ContextBehavior) Call(entityId uid.Id, fun generic.FuncVar1[ec.Entity, any, Ret], va ...any) AsyncRet {
entity, err := ctx.getEntity(entityId)
if err != nil {
return makeAsyncErr(err)
}
return getCaller(entity).Call(func(va ...any) concurrent.Ret {
entity := va[0].(ec.Entity)
fun := va[1].(generic.FuncVar1[ec.Entity, any, Ret])
funVa := va[2].([]any)
if err := checkEntity(entity); err != nil {
return concurrent.MakeRet(nil, err)
}
return fun.Exec(entity, funVa...)
}, entity, fun, va)
}
// CallDelegate 查找实体并异步调用委托,有返回值。不会阻塞当前线程,会返回AsyncRet。
//
// 注意:
// - 代码片段中的线程安全问题,如临界区访问、线程死锁等。
// - 调用过程中的panic信息,均会转换为error返回。
func (ctx *ContextBehavior) CallDelegate(entityId uid.Id, fun generic.DelegateFuncVar1[ec.Entity, any, Ret], va ...any) AsyncRet {
entity, err := ctx.getEntity(entityId)
if err != nil {
return makeAsyncErr(err)
}
return getCaller(entity).Call(func(va ...any) concurrent.Ret {
entity := va[0].(ec.Entity)
fun := va[1].(generic.DelegateFuncVar1[ec.Entity, any, Ret])
funVa := va[2].([]any)
if err := checkEntity(entity); err != nil {
return concurrent.MakeRet(nil, err)
}
return fun.Exec(nil, entity, funVa...)
}, entity, fun, va)
}
// CallVoid 查找实体并异步调用函数,无返回值。在运行时中。不会阻塞当前线程,会返回AsyncRet。
//
// 注意:
// - 代码片段中的线程安全问题,如临界区访问、线程死锁等。
// - 调用过程中的panic信息,均会转换为error返回。
func (ctx *ContextBehavior) CallVoid(entityId uid.Id, fun generic.ActionVar1[ec.Entity, any], va ...any) AsyncRet {
entity, err := ctx.getEntity(entityId)
if err != nil {
return makeAsyncErr(err)
}
return getCaller(entity).Call(func(va ...any) concurrent.Ret {
entity := va[0].(ec.Entity)
fun := va[1].(generic.ActionVar1[ec.Entity, any])
funVa := va[2].([]any)
if err := checkEntity(entity); err != nil {
return concurrent.MakeRet(nil, err)
}
fun.Exec(entity, funVa...)
return concurrent.MakeRet(nil, nil)
}, entity, fun, va)
}
// CallVoidDelegate 查找实体并异步调用委托,无返回值。在运行时中。不会阻塞当前线程,会返回AsyncRet。
//
// 注意:
// - 代码片段中的线程安全问题,如临界区访问、线程死锁等。
// - 调用过程中的panic信息,均会转换为error返回。
func (ctx *ContextBehavior) CallVoidDelegate(entityId uid.Id, fun generic.DelegateActionVar1[ec.Entity, any], va ...any) AsyncRet {
entity, err := ctx.getEntity(entityId)
if err != nil {
return makeAsyncErr(err)
}
return getCaller(entity).Call(func(va ...any) concurrent.Ret {
entity := va[0].(ec.Entity)
fun := va[1].(generic.DelegateActionVar1[ec.Entity, any])
funVa := va[2].([]any)
if err := checkEntity(entity); err != nil {
return concurrent.MakeRet(nil, err)
}
fun.Exec(nil, entity, funVa...)
return concurrent.MakeRet(nil, nil)
}, entity, fun, va)
}
func (ctx *ContextBehavior) getEntity(id uid.Id) (ec.ConcurrentEntity, error) {
entity, ok := ctx.entityMgr.GetEntity(id)
if !ok {
return nil, fmt.Errorf("%w: entity not exist", ErrContext)
}
return entity, nil
}