-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcid.go
55 lines (47 loc) · 889 Bytes
/
cid.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
package mmoa
// Monolithic Message-Oriented Application (MMOA)
// Correlation identifier (cID)
// Copyright © 2016 Eduard Sesigin. All rights reserved. Contacts: <claygod@yandex.ru>
import (
"runtime"
"sync/atomic"
"github.com/claygod/mmoa/tools"
)
// NewCid - create a new Cid
func NewCid() *Cid {
c := &Cid{}
return c
}
// Cid structure
type Cid struct {
hasp int32
counter tools.TypeCID
}
// Get - get the current count
func (c *Cid) Get() tools.TypeCID {
c.lock()
cnt := c.counter
c.counter++
c.unlock()
return cnt
}
// lock - block Cid
func (c *Cid) lock() bool {
for {
if c.hasp == 0 && atomic.CompareAndSwapInt32(&c.hasp, 0, 1) {
break
}
runtime.Gosched()
}
return true
}
// unlock - block Cid
func (c *Cid) unlock() bool {
for {
if c.hasp == 1 && atomic.CompareAndSwapInt32(&c.hasp, 1, 0) {
break
}
runtime.Gosched()
}
return true
}