-
Notifications
You must be signed in to change notification settings - Fork 1
/
unique-id-int.go
42 lines (35 loc) · 1023 Bytes
/
unique-id-int.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
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"strconv"
"sync/atomic"
"unsafe"
"github.com/haraldrudell/parl/perrors"
)
// UniqueIDUint64 generates executable-invocation-unique uint64 numbers 1…
// Different series of uint64 from different generators does not have identity,
// ie. they cannot be told apart.
// Consider UniqueIDTypedUint64 to have identity.
//
// Usage:
//
// var generator parl.UniqueIDUint64
// id := generator.ID()
type UniqueIDint int
var intSize = int(unsafe.Sizeof(2))
// ID generates a unique uint64 1…. thread-safe
func (u *UniqueIDint) ID() (uniqueID int) {
switch intSize {
case 4:
return int(atomic.AddInt32((*int32)(unsafe.Pointer(u)), 1))
case 8:
return int(atomic.AddInt64((*int64)(unsafe.Pointer(u)), 1))
}
panic(perrors.ErrorfPF("int size not 4 or 8: %d", intSize))
}
func (u *UniqueIDint) String() (s string) {
return "IDint:" + strconv.FormatInt(int64(*u), 10)
}