forked from zllangct/ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.go
More file actions
113 lines (85 loc) · 1.78 KB
/
Copy pathruntime.go
File metadata and controls
113 lines (85 loc) · 1.78 KB
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
package ecs
import "sync"
var Runtime = NewRuntime()
const(
STATUS_INIT = iota
STATUS_RUNNING
STATUS_PAUSE
STATUS_STOP
)
type RuntimeStatus int
type ecsRuntime struct {
//mutex
mutex sync.Mutex
//world status
status RuntimeStatus
//world config
config *RuntimeConfig
//world worker pool
workPool *Pool
//logger
logger IInternalLogger
//world collections
world []*World
stop chan struct{}
}
//TODO world global event system
func NewRuntime() *ecsRuntime {
config := NewDefaultRuntimeConfig()
rt := &ecsRuntime{
config: config,
logger: NewStdLogger(),
}
rt.workPool = NewPool(rt, config.MaxPoolThread, config.MaxPoolJobQueue)
return rt
}
func (r *ecsRuntime) NewWorld() *World{
r.mutex.Lock()
defer r.mutex.Unlock()
world := NewWorld(r)
r.world = append(r.world, NewWorld(r))
return world
}
// SetConfig config the world
func (r *ecsRuntime) SetConfig(config *RuntimeConfig) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.config = config
}
// SetLogger set logger
func (r *ecsRuntime) SetLogger(logger IInternalLogger) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.logger = logger
}
func (r *ecsRuntime) Status() RuntimeStatus {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.status
}
func (r *ecsRuntime) Run() {
r.run()
}
func (r *ecsRuntime) run() {
//default config
r.mutex.Lock()
defer r.mutex.Unlock()
if r.status == STATUS_INIT {
//start the work pool
r.workPool.Start()
r.status = STATUS_RUNNING
}
}
func (r *ecsRuntime) Stop() {
r.mutex.Lock()
defer r.mutex.Unlock()
for _, world := range r.world {
if status := world.GetStatus(); status != STATUS_STOP {
world.Stop()
}
}
r.stop<- struct{}{}
}
func (r *ecsRuntime) AddJob(handler func(JobContext, ...interface{}), args ...interface{}) {
r.workPool.AddJob(handler, args...)
}