Skip to content

Commit 185b343

Browse files
authored
Move atomic package (#15)
1 parent 90b80df commit 185b343

File tree

5 files changed

+471
-0
lines changed

5 files changed

+471
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
This repository is the home to the common libraries used by Elastic Agent and Beats.
44

55
Provided packages:
6+
* `github.com/elastic/elastic-agent-libs/atomic` Atomic operations for integer and boolean types.
67
* `github.com/elastic/elastic-agent-libs/cloudid` is used for parsing `cloud.id` and `cloud.auth` when connecting to the Elastic stack.
78
* `github.com/elastic/elastic-agent-libs/config` the previous `config.go` file from `github.com/elastic/beats/v7/libbeat/common`. A minimal wrapper around `github.com/elastic/go-ucfg`. It contains helpers for merging and accessing configuration objects and flags.
89
* `github.com/elastic/elastic-agent-libs/file` is responsible for rotating and writing input and output files.

atomic/atomic.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
// Package atomic provides common primitive types with atomic accessors.
19+
package atomic
20+
21+
import a "sync/atomic"
22+
23+
// Bool provides an atomic boolean type.
24+
type Bool struct{ u Uint32 }
25+
26+
// Int32 provides an atomic int32 type.
27+
type Int32 struct{ value int32 }
28+
29+
// Int64 provides an atomic int64 type.
30+
type Int64 struct{ value int64 }
31+
32+
// Uint32 provides an atomic uint32 type.
33+
type Uint32 struct{ value uint32 }
34+
35+
// Uint64 provides an atomic uint64 type.
36+
type Uint64 struct{ value uint64 }
37+
38+
func MakeBool(v bool) Bool { return Bool{MakeUint32(encBool(v))} }
39+
func NewBool(v bool) *Bool { return &Bool{MakeUint32(encBool(v))} }
40+
func (b *Bool) Load() bool { return b.u.Load() == 1 }
41+
func (b *Bool) Store(v bool) { b.u.Store(encBool(v)) }
42+
func (b *Bool) Swap(new bool) bool { return b.u.Swap(encBool(new)) == 1 }
43+
func (b *Bool) CAS(old, new bool) bool { return b.u.CAS(encBool(old), encBool(new)) }
44+
45+
func MakeInt32(v int32) Int32 { return Int32{v} }
46+
func NewInt32(v int32) *Int32 { return &Int32{v} }
47+
func (i *Int32) Load() int32 { return a.LoadInt32(&i.value) }
48+
func (i *Int32) Store(v int32) { a.StoreInt32(&i.value, v) }
49+
func (i *Int32) Swap(new int32) int32 { return a.SwapInt32(&i.value, new) }
50+
func (i *Int32) Add(delta int32) int32 { return a.AddInt32(&i.value, delta) }
51+
func (i *Int32) Sub(delta int32) int32 { return a.AddInt32(&i.value, -delta) }
52+
func (i *Int32) Inc() int32 { return i.Add(1) }
53+
func (i *Int32) Dec() int32 { return i.Add(-1) }
54+
func (i *Int32) CAS(old, new int32) bool { return a.CompareAndSwapInt32(&i.value, old, new) }
55+
56+
func MakeInt64(v int64) Int64 { return Int64{v} }
57+
func NewInt64(v int64) *Int64 { return &Int64{v} }
58+
func (i *Int64) Load() int64 { return a.LoadInt64(&i.value) }
59+
func (i *Int64) Store(v int64) { a.StoreInt64(&i.value, v) }
60+
func (i *Int64) Swap(new int64) int64 { return a.SwapInt64(&i.value, new) }
61+
func (i *Int64) Add(delta int64) int64 { return a.AddInt64(&i.value, delta) }
62+
func (i *Int64) Sub(delta int64) int64 { return a.AddInt64(&i.value, -delta) }
63+
func (i *Int64) Inc() int64 { return i.Add(1) }
64+
func (i *Int64) Dec() int64 { return i.Add(-1) }
65+
func (i *Int64) CAS(old, new int64) bool { return a.CompareAndSwapInt64(&i.value, old, new) }
66+
67+
func MakeUint32(v uint32) Uint32 { return Uint32{v} }
68+
func NewUint32(v uint32) *Uint32 { return &Uint32{v} }
69+
func (u *Uint32) Load() uint32 { return a.LoadUint32(&u.value) }
70+
func (u *Uint32) Store(v uint32) { a.StoreUint32(&u.value, v) }
71+
func (u *Uint32) Swap(new uint32) uint32 { return a.SwapUint32(&u.value, new) }
72+
func (u *Uint32) Add(delta uint32) uint32 { return a.AddUint32(&u.value, delta) }
73+
func (u *Uint32) Sub(delta uint32) uint32 { return a.AddUint32(&u.value, ^(delta - uint32(1))) }
74+
func (u *Uint32) Inc() uint32 { return u.Add(1) }
75+
func (u *Uint32) Dec() uint32 { return u.Add(^uint32(0)) }
76+
func (u *Uint32) CAS(old, new uint32) bool { return a.CompareAndSwapUint32(&u.value, old, new) }
77+
78+
func MakeUint64(v uint64) Uint64 { return Uint64{v} }
79+
func NewUint64(v uint64) *Uint64 { return &Uint64{v} }
80+
func (u *Uint64) Load() uint64 { return a.LoadUint64(&u.value) }
81+
func (u *Uint64) Store(v uint64) { a.StoreUint64(&u.value, v) }
82+
func (u *Uint64) Swap(new uint64) uint64 { return a.SwapUint64(&u.value, new) }
83+
func (u *Uint64) Add(delta uint64) uint64 { return a.AddUint64(&u.value, delta) }
84+
func (u *Uint64) Sub(delta uint64) uint64 { return a.AddUint64(&u.value, ^(delta - uint64(1))) }
85+
func (u *Uint64) Inc() uint64 { return u.Add(1) }
86+
func (u *Uint64) Dec() uint64 { return u.Add(^uint64(0)) }
87+
func (u *Uint64) CAS(old, new uint64) bool { return a.CompareAndSwapUint64(&u.value, old, new) }
88+
89+
func encBool(b bool) uint32 {
90+
if b {
91+
return 1
92+
}
93+
return 0
94+
}

atomic/atomic32.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//go:build 386 || arm || mips || mipsle
19+
// +build 386 arm mips mipsle
20+
21+
package atomic
22+
23+
// atomic Uint/Int for 32bit systems
24+
25+
// Uint provides an architecture specific atomic uint.
26+
type Uint struct{ a Uint32 }
27+
28+
// Int provides an architecture specific atomic uint.
29+
type Int struct{ a Int32 }
30+
31+
func MakeUint(v uint) Uint { return Uint{MakeUint32(uint32(v))} }
32+
func NewUint(v uint) *Uint { return &Uint{MakeUint32(uint32(v))} }
33+
func (u *Uint) Load() uint { return uint(u.a.Load()) }
34+
func (u *Uint) Store(v uint) { u.a.Store(uint32(v)) }
35+
func (u *Uint) Swap(new uint) uint { return uint(u.a.Swap(uint32(new))) }
36+
func (u *Uint) Add(delta uint) uint { return uint(u.a.Add(uint32(delta))) }
37+
func (u *Uint) Sub(delta uint) uint { return uint(u.a.Add(uint32(-delta))) }
38+
func (u *Uint) Inc() uint { return uint(u.a.Inc()) }
39+
func (u *Uint) Dec() uint { return uint(u.a.Dec()) }
40+
func (u *Uint) CAS(old, new uint) bool { return u.a.CAS(uint32(old), uint32(new)) }
41+
42+
func MakeInt(v int) Int { return Int{MakeInt32(int32(v))} }
43+
func NewInt(v int) *Int { return &Int{MakeInt32(int32(v))} }
44+
func (i *Int) Load() int { return int(i.a.Load()) }
45+
func (i *Int) Store(v int) { i.a.Store(int32(v)) }
46+
func (i *Int) Swap(new int) int { return int(i.a.Swap(int32(new))) }
47+
func (i *Int) Add(delta int) int { return int(i.a.Add(int32(delta))) }
48+
func (i *Int) Sub(delta int) int { return int(i.a.Add(int32(-delta))) }
49+
func (i *Int) Inc() int { return int(i.a.Inc()) }
50+
func (i *Int) Dec() int { return int(i.a.Dec()) }
51+
func (i *Int) CAS(old, new int) bool { return i.a.CAS(int32(old), int32(new)) }

atomic/atomic64.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//go:build amd64 || arm64 || ppc64 || ppc64le || mips64 || mips64le || s390x
19+
// +build amd64 arm64 ppc64 ppc64le mips64 mips64le s390x
20+
21+
package atomic
22+
23+
// atomic Uint/Int for 64bit systems
24+
25+
// Uint provides an architecture specific atomic uint.
26+
type Uint struct{ a Uint64 }
27+
28+
// Int provides an architecture specific atomic uint.
29+
type Int struct{ a Int64 }
30+
31+
func MakeUint(v uint) Uint { return Uint{MakeUint64(uint64(v))} }
32+
func NewUint(v uint) *Uint { return &Uint{MakeUint64(uint64(v))} }
33+
func (u *Uint) Load() uint { return uint(u.a.Load()) }
34+
func (u *Uint) Store(v uint) { u.a.Store(uint64(v)) }
35+
func (u *Uint) Swap(new uint) uint { return uint(u.a.Swap(uint64(new))) }
36+
func (u *Uint) Add(delta uint) uint { return uint(u.a.Add(uint64(delta))) }
37+
func (u *Uint) Sub(delta uint) uint { return uint(u.a.Add(uint64(-delta))) }
38+
func (u *Uint) Inc() uint { return uint(u.a.Inc()) }
39+
func (u *Uint) Dec() uint { return uint(u.a.Dec()) }
40+
func (u *Uint) CAS(old, new uint) bool { return u.a.CAS(uint64(old), uint64(new)) }
41+
42+
func MakeInt(v int) Int { return Int{MakeInt64(int64(v))} }
43+
func NewInt(v int) *Int { return &Int{MakeInt64(int64(v))} }
44+
func (i *Int) Load() int { return int(i.a.Load()) }
45+
func (i *Int) Store(v int) { i.a.Store(int64(v)) }
46+
func (i *Int) Swap(new int) int { return int(i.a.Swap(int64(new))) }
47+
func (i *Int) Add(delta int) int { return int(i.a.Add(int64(delta))) }
48+
func (i *Int) Sub(delta int) int { return int(i.a.Add(int64(-delta))) }
49+
func (i *Int) Inc() int { return int(i.a.Inc()) }
50+
func (i *Int) Dec() int { return int(i.a.Dec()) }
51+
func (i *Int) CAS(old, new int) bool { return i.a.CAS(int64(old), int64(new)) }

0 commit comments

Comments
 (0)