forked from xelpool/xelishash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.go
More file actions
47 lines (35 loc) · 880 Bytes
/
threadpool.go
File metadata and controls
47 lines (35 loc) · 880 Bytes
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
package xelishash
import "unsafe"
type ThreadPool struct {
scratch chan *ScratchPadV2
}
func NewThreadPool(threads int) *ThreadPool {
tp := &ThreadPool{
scratch: make(chan *ScratchPadV2, threads),
}
for i := 0; i < threads; i++ {
tp.scratch <- &ScratchPadV2{}
}
return tp
}
// Hash accepts algorithm name as string (example: xel/0, xel/1)
func (t *ThreadPool) Hash(algo string, input []byte) [32]byte {
if algo == "xel/1" { // XelisHash v2
return t.XelisHashV2(input)
}
return t.XelisHash(input)
}
func (t *ThreadPool) XelisHash(input []byte) [32]byte {
scratch := <-t.scratch
defer func() {
t.scratch <- scratch
}()
return XelisHash(input, (*ScratchPad)(unsafe.Pointer(scratch)))
}
func (t *ThreadPool) XelisHashV2(input []byte) [32]byte {
scratch := <-t.scratch
defer func() {
t.scratch <- scratch
}()
return XelisHashV2(input, scratch)
}