Skip to content

Commit bec71e0

Browse files
committed
Initial release
0 parents  commit bec71e0

File tree

7 files changed

+993
-0
lines changed

7 files changed

+993
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 Orbital Labs, LLC <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/orbstack/securefs
2+
3+
go 1.20
4+
5+
require golang.org/x/sys v0.10.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
2+
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

internal/syncx/once.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package syncx
2+
3+
import "sync"
4+
5+
type Once[T any] struct {
6+
sync.Once
7+
Value T
8+
}
9+
10+
func (o *Once[T]) Do(f func() T) T {
11+
o.Once.Do(func() {
12+
o.Value = f()
13+
})
14+
return o.Value
15+
}

internal/syncx/once_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package syncx
2+
3+
import "testing"
4+
5+
func TestOnce(t *testing.T) {
6+
t.Parallel()
7+
8+
var once Once[int]
9+
var count int
10+
11+
once.Do(func() int {
12+
count++
13+
return 1
14+
})
15+
16+
if count != 1 {
17+
t.Errorf("count = %d, want 1", count)
18+
}
19+
20+
once.Do(func() int {
21+
count++
22+
return 2
23+
})
24+
25+
if count != 1 {
26+
t.Errorf("count = %d, want 1", count)
27+
}
28+
}
29+
30+
func TestOnceParallel(t *testing.T) {
31+
t.Parallel()
32+
33+
var once Once[int]
34+
var count int
35+
36+
f := func() int {
37+
once.Do(func() int {
38+
count++
39+
return 1
40+
})
41+
return once.Value
42+
}
43+
44+
if got, want := f(), 1; got != want {
45+
t.Errorf("f() = %d, want %d", got, want)
46+
}
47+
48+
if got, want := f(), 1; got != want {
49+
t.Errorf("f() = %d, want %d", got, want)
50+
}
51+
52+
if count != 1 {
53+
t.Errorf("count = %d, want 1", count)
54+
}
55+
}

0 commit comments

Comments
 (0)