-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad9fa14
commit 0c62bb2
Showing
19 changed files
with
226 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package. | ||
// | ||
// Copyright 2022 The Go-MySQL-Driver Authors. All rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at http://mozilla.org/MPL/2.0/. | ||
//go:build go1.19 | ||
// +build go1.19 | ||
|
||
package mysql | ||
|
||
import "sync/atomic" | ||
|
||
/****************************************************************************** | ||
* Sync utils * | ||
******************************************************************************/ | ||
|
||
type atomicBool = atomic.Bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package. | ||
// | ||
// Copyright 2022 The Go-MySQL-Driver Authors. All rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at http://mozilla.org/MPL/2.0/. | ||
//go:build !go1.19 | ||
// +build !go1.19 | ||
|
||
package mysql | ||
|
||
import "sync/atomic" | ||
|
||
/****************************************************************************** | ||
* Sync utils * | ||
******************************************************************************/ | ||
|
||
// atomicBool is an implementation of atomic.Bool for older version of Go. | ||
// it is a wrapper around uint32 for usage as a boolean value with | ||
// atomic access. | ||
type atomicBool struct { | ||
_ noCopy | ||
value uint32 | ||
} | ||
|
||
// Load returns whether the current boolean value is true | ||
func (ab *atomicBool) Load() bool { | ||
return atomic.LoadUint32(&ab.value) > 0 | ||
} | ||
|
||
// Store sets the value of the bool regardless of the previous value | ||
func (ab *atomicBool) Store(value bool) { | ||
if value { | ||
atomic.StoreUint32(&ab.value, 1) | ||
} else { | ||
atomic.StoreUint32(&ab.value, 0) | ||
} | ||
} | ||
|
||
// Swap sets the value of the bool and returns the old value. | ||
func (ab *atomicBool) Swap(value bool) bool { | ||
if value { | ||
return atomic.SwapUint32(&ab.value, 1) > 0 | ||
} | ||
return atomic.SwapUint32(&ab.value, 0) > 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package. | ||
// | ||
// Copyright 2022 The Go-MySQL-Driver Authors. All rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at http://mozilla.org/MPL/2.0/. | ||
//go:build !go1.19 | ||
// +build !go1.19 | ||
|
||
package mysql | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestAtomicBool(t *testing.T) { | ||
var ab atomicBool | ||
if ab.Load() { | ||
t.Fatal("Expected value to be false") | ||
} | ||
|
||
ab.Store(true) | ||
if ab.value != 1 { | ||
t.Fatal("Set(true) did not set value to 1") | ||
} | ||
if !ab.Load() { | ||
t.Fatal("Expected value to be true") | ||
} | ||
|
||
ab.Store(true) | ||
if !ab.Load() { | ||
t.Fatal("Expected value to be true") | ||
} | ||
|
||
ab.Store(false) | ||
if ab.value != 0 { | ||
t.Fatal("Set(false) did not set value to 0") | ||
} | ||
if ab.Load() { | ||
t.Fatal("Expected value to be false") | ||
} | ||
|
||
ab.Store(false) | ||
if ab.Load() { | ||
t.Fatal("Expected value to be false") | ||
} | ||
if ab.Swap(false) { | ||
t.Fatal("Expected the old value to be false") | ||
} | ||
if ab.Swap(true) { | ||
t.Fatal("Expected the old value to be false") | ||
} | ||
if !ab.Load() { | ||
t.Fatal("Expected value to be true") | ||
} | ||
|
||
ab.Store(true) | ||
if !ab.Load() { | ||
t.Fatal("Expected value to be true") | ||
} | ||
if !ab.Swap(true) { | ||
t.Fatal("Expected the old value to be true") | ||
} | ||
if !ab.Swap(false) { | ||
t.Fatal("Expected the old value to be true") | ||
} | ||
if ab.Load() { | ||
t.Fatal("Expected value to be false") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.