forked from xzc-coder/executor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
206 lines (176 loc) · 4.12 KB
/
utils.go
File metadata and controls
206 lines (176 loc) · 4.12 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package executor
import (
"container/heap"
"context"
"github.com/petermattis/goid"
"math"
"sync"
"time"
)
const maxTimeDuration = math.MaxUint16 * time.Hour
const invalidIndex = -1
func Max(a, b int) int {
if a > b {
return a
}
return b
}
// SafeSlice 是一个使用读写锁和泛型的线程安全切片结构体
type SafeSlice[T any] struct {
data []T
rwMux sync.RWMutex
}
// Append 向切片中追加元素
func (s *SafeSlice[T]) Append(value T) {
s.rwMux.Lock()
defer s.rwMux.Unlock()
s.data = append(s.data, value)
}
// Get 获取切片中指定索引的元素
func (s *SafeSlice[T]) Get(index int) (T, bool) {
s.rwMux.RLock()
defer s.rwMux.RUnlock()
var zero T
if index < 0 || index >= len(s.data) {
return zero, false
}
return s.data[index], true
}
// Len 获取切片的长度
func (s *SafeSlice[T]) Len() int {
s.rwMux.RLock()
defer s.rwMux.RUnlock()
return len(s.data)
}
// Remove 删除切片中指定索引的元素
func (s *SafeSlice[T]) Remove(index int) bool {
s.rwMux.Lock()
defer s.rwMux.Unlock()
if index < 0 || index >= len(s.data) {
return false
}
// 删除指定索引的元素
s.data = append(s.data[:index], s.data[index+1:]...)
return true
}
func GetGoroutineID() int64 {
//更换为第三方获取,性能更高
gid := goid.Get()
//buf := make([]byte, 64)
//buf = buf[:runtime.Stack(buf, false)]
//fields := strings.Fields(strings.TrimPrefix(string(buf), "goroutine "))
//gid, _ = strconv.ParseInt(fields[0], 10, 64)
return gid
}
// contextInfo Context的上下文信息集合体
type contextInfo struct {
ctx context.Context
goroutineId int64
cancel context.CancelFunc
}
// CallableAdapter 适配器,把Runnable包装成Callable
type CallableAdapter struct {
runnable Runnable
}
func (c *CallableAdapter) Call() any {
c.runnable.Run()
return nil
}
// 获取一个Callable
func callable(runnable Runnable) Callable {
return &CallableAdapter{
runnable: runnable,
}
}
// NewHeap 通过此方式创建的堆,只能使用标准库的 heap 中的方法和 .Peek(非标准库)来调用
func NewHeap(valueLess func(a, b any) bool) *Heap {
return &Heap{valueLess: valueLess}
}
// NewSafeHeap 创建一个线程安全的堆,只能通过使用该结构体提供的方法
func NewSafeHeap(heap *Heap) *SafeHeap {
return &SafeHeap{heap: heap}
}
type SafeHeap struct {
heap *Heap
mux sync.Mutex
}
func (s *SafeHeap) Push(heapElement *HeapElement) {
s.mux.Lock()
defer s.mux.Unlock()
heap.Push(s.heap, heapElement)
}
func (s *SafeHeap) Pop() (*HeapElement, bool) {
s.mux.Lock()
defer s.mux.Unlock()
if s.heap.Len() == 0 {
return nil, false
} else {
return heap.Pop(s.heap).(*HeapElement), true
}
}
func (s *SafeHeap) Len() int {
s.mux.Lock()
defer s.mux.Unlock()
return s.heap.Len()
}
func (s *SafeHeap) Peek() *HeapElement {
s.mux.Lock()
defer s.mux.Unlock()
return s.heap.Peek().(*HeapElement)
}
func (s *SafeHeap) Remove(heapElement *HeapElement) {
s.mux.Lock()
defer s.mux.Unlock()
if heapElement.Index >= 0 {
heap.Remove(s.heap, heapElement.Index)
}
}
func (s *SafeHeap) RemoveAll() []*HeapElement {
s.mux.Lock()
defer s.mux.Unlock()
var heapElements = make([]*HeapElement, 0, s.Len())
for {
heapElement, ok := s.Pop()
if ok {
heapElements = append(heapElements, heapElement)
} else {
return heapElements
}
}
}
type HeapElement struct {
Value any
Index int
}
// Heap 堆结构体,使用 any 类型的泛型
type Heap struct {
elements []*HeapElement
valueLess func(a, b any) bool
}
func (h *Heap) Push(element any) {
h.elements = append(h.elements, element.(*HeapElement))
}
func (h *Heap) Len() int {
return len(h.elements)
}
func (h *Heap) Pop() any {
old := h.elements
n := len(old)
x := old[n-1]
old[n-1] = nil
h.elements = old[:n-1]
x.Index = invalidIndex
return x
}
func (h *Heap) Less(i, j int) bool {
return h.valueLess(h.elements[i].Value, h.elements[j].Value)
}
// Swap 交换两个元素的位置,并更新它们的下标
func (h *Heap) Swap(i, j int) {
h.elements[i], h.elements[j] = h.elements[j], h.elements[i]
h.elements[i].Index = i
h.elements[j].Index = j
}
func (h *Heap) Peek() any {
return h.elements[0]
}