-
Notifications
You must be signed in to change notification settings - Fork 16
/
account.go
161 lines (128 loc) · 2.98 KB
/
account.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package transaction
// Core
// Account
// Copyright © 2017-2018 Eduard Sesigin. All rights reserved. Contacts: <claygod@yandex.ru>
import (
"runtime"
"sync/atomic"
)
/*
account - keeps a balance.
Account balance can not be less than zero.
The counter has two tasks:
counts the number of operations performed
stops the account (new operations are not started)
*/
type account struct {
counter int64
balance int64
}
/*
newAccount - create new account.
*/
func newAccount(amount int64) *account {
return &account{balance: amount}
}
/*
addition - add to the balance the input variable.
If the result of adding the balance and the input
variable is less than zero, the balance does not change.
Returned result:
greater than or equal to zero // OK
less than zero // Error
*/
func (a *account) addition(amount int64) int64 {
// The hidden part of the code allows you
// to speed up a bit by avoiding the cycle start
//b := atomic.LoadInt64(&a.balance)
//nb := b + amount
//if nb < 0 || atomic.CompareAndSwapInt64(&a.balance, b, nb) {
// return nb
//}
for i := trialLimit; i > trialStop; i-- {
b := atomic.LoadInt64(&a.balance)
nb := b + amount
if amount < 0 && nb > b {
return amount
}
if nb < 0 || atomic.CompareAndSwapInt64(&a.balance, b, nb) {
return nb
}
//if nb < 0 {
// return nb
//}
//if atomic.CompareAndSwapInt64(&a.balance, b, nb) {
// return nb
//}
runtime.Gosched()
}
return permitError
}
/*
total - current account balance.
*/
func (a *account) total() int64 {
return atomic.LoadInt64(&a.balance)
}
/*
catch - get permission to perform operations with the account.
*/
func (a *account) catch() bool {
if atomic.LoadInt64(&a.counter) < 0 {
return false
}
if atomic.AddInt64(&a.counter, 1) > 0 {
return true
}
atomic.AddInt64(&a.counter, -1)
return false
}
/*
throw - current account operation has been completed
*/
func (a *account) throw() {
atomic.AddInt64(&a.counter, -1)
}
/*
start - start an account.
*/
func (a *account) start() bool {
var currentCounter int64
for i := trialLimit; i > trialStop; i-- {
currentCounter = atomic.LoadInt64(&a.counter)
if currentCounter >= 0 {
return true
}
// the variable `currentCounter` is expected to be `permitError`
if atomic.CompareAndSwapInt64(&a.counter, permitError, 0) {
return true
}
runtime.Gosched()
}
return false
}
/*
stop - stop an account.
*/
func (a *account) stop() bool {
var currentCounter int64
for i := trialLimit; i > trialStop; i-- {
currentCounter = atomic.LoadInt64(&a.counter)
switch {
case currentCounter == 0:
if atomic.CompareAndSwapInt64(&a.counter, 0, permitError) {
return true
}
case currentCounter > 0:
atomic.CompareAndSwapInt64(&a.counter, currentCounter, currentCounter+permitError)
case currentCounter == permitError:
return true
}
runtime.Gosched()
}
currentCounter = atomic.LoadInt64(&a.counter)
if currentCounter < 0 && currentCounter > permitError {
atomic.AddInt64(&a.counter, -permitError)
}
return false
}