-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
86 lines (76 loc) · 1.67 KB
/
utils.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
package dnnl
import (
"os"
"path/filepath"
"strconv"
"sync"
"time"
"github.com/Unknwon/com"
)
// Random number state.
// We generate random temporary file names so that there's a good
// chance the file doesn't exist yet - keeps the number of tries in
// TempFile to a minimum.
var rand uint32
var randmu sync.Mutex
func reseed() uint32 {
return uint32(time.Now().UnixNano() + int64(os.Getpid()))
}
func nextSuffix() string {
randmu.Lock()
r := rand
if r == 0 {
r = reseed()
}
r = r*1664525 + 1013904223 // constants from Numerical Recipes
rand = r
randmu.Unlock()
return strconv.Itoa(int(1e9 + r%1e9))[1:]
}
// tempFile creates a new temporary file in the directory dir
// If dir is the empty string, TempFile uses the default directory
// for temporary files (see os.TempDir).
// Multiple programs calling TempFile simultaneously
func tempFile(dir, prefix, suffix string) (name string, err error) {
if dir == "" {
dir = os.TempDir()
}
nconflict := 0
for i := 0; i < 10000; i++ {
name = filepath.Join(dir, prefix+nextSuffix()+suffix)
if com.IsFile(name) {
if nconflict++; nconflict > 10 {
randmu.Lock()
rand = reseed()
randmu.Unlock()
}
continue
}
break
}
return
}
func uint32SliceToUint(data []uint32) []uint {
sz := len(data)
res := make([]uint, sz)
for ii, val := range data {
res[ii] = uint(val)
}
return res
}
func intSliceToUint32(data []int) []uint32 {
sz := len(data)
res := make([]uint32, sz)
for ii, val := range data {
res[ii] = uint32(val)
}
return res
}
func uintSliceToUint32(data []uint) []uint32 {
sz := len(data)
res := make([]uint32, sz)
for ii, val := range data {
res[ii] = uint32(val)
}
return res
}