2022-11-25 18:19:33 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022 tursom. All rights reserved.
|
|
|
|
* Use of this source code is governed by a GPL-3
|
|
|
|
* license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2022-03-30 20:49:59 +08:00
|
|
|
package atomic
|
|
|
|
|
2022-12-03 19:13:29 +08:00
|
|
|
type Bool Int32
|
2022-03-30 20:49:59 +08:00
|
|
|
|
|
|
|
func (v *Bool) Load() (val bool) {
|
2022-12-03 19:13:29 +08:00
|
|
|
return (*Int32)(v).Load() != 0
|
2022-03-30 20:49:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Bool) Store(val bool) {
|
|
|
|
if val {
|
2022-12-03 19:13:29 +08:00
|
|
|
(*Int32)(v).Store(1)
|
2022-03-30 20:49:59 +08:00
|
|
|
} else {
|
2022-12-03 19:13:29 +08:00
|
|
|
(*Int32)(v).Store(0)
|
2022-03-30 20:49:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Bool) Swap(new bool) (old bool) {
|
|
|
|
n := int32(0)
|
|
|
|
if new {
|
|
|
|
n = 1
|
|
|
|
}
|
2022-12-03 19:13:29 +08:00
|
|
|
return (*Int32)(v).Swap(n) != 0
|
2022-03-30 20:49:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Bool) CompareAndSwap(old, new bool) (swapped bool) {
|
|
|
|
o := int32(0)
|
|
|
|
if old {
|
|
|
|
o = 1
|
|
|
|
}
|
|
|
|
n := int32(0)
|
|
|
|
if new {
|
|
|
|
n = 1
|
|
|
|
}
|
2022-12-03 19:13:29 +08:00
|
|
|
return (*Int32)(v).CompareAndSwap(o, n)
|
2022-03-30 20:49:59 +08:00
|
|
|
}
|