Skip to content

Commit 9945691

Browse files
committed
uuid: docs and footgun detection
1 parent 83d1c3d commit 9945691

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

uuid.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@ var (
1919
generators [ng]gen
2020
)
2121

22+
func init() {
23+
if (ng & ngmask) != 0 {
24+
panic("uuid: you modified ng to create more gens, but it needs to be a power of 2")
25+
}
26+
}
27+
2228
// V4 returns a UUIDv4. It never returns an error, never panics,
2329
// and never runs out of entropy.
2430
func V4() string {
2531
i := 0
2632
for {
33+
// this is a spinlock, nothing starves these generators
34+
// enough for anything more elaborate, as V4 completes
35+
// in 25-100ns on modern systems
2736
if atomic.CompareAndSwapUint32(&access[i], 0, 1) {
2837
u := string(generators[i].V4())
2938
atomic.StoreUint32(&access[i], 0)

0 commit comments

Comments
 (0)