aboutsummaryrefslogtreecommitdiff
path: root/src/database
diff options
context:
space:
mode:
authorcuiweixie <cuiweixie@gmail.com>2022-08-27 11:38:32 +0800
committerDaniel Theophanes <kardianos@gmail.com>2022-09-08 15:52:57 +0000
commit0e50bf0e404e65385c597b9bb3339764a444b7f0 (patch)
tree2eaf2ed2c9ab1c2d28edb5b4fde45a24bb74d33d /src/database
parentd61ffe8b6c2125cb3110753351405136b837c97d (diff)
downloadgo-0e50bf0e404e65385c597b9bb3339764a444b7f0.tar.gz
go-0e50bf0e404e65385c597b9bb3339764a444b7f0.zip
database: convert Tx.done to atomic type
Change-Id: I9ec725009376f5865adedca6c159b14140dde097 Reviewed-on: https://go-review.googlesource.com/c/go/+/426086 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Run-TryBot: Daniel Theophanes <kardianos@gmail.com>
Diffstat (limited to 'src/database')
-rw-r--r--src/database/sql/sql.go13
1 files changed, 6 insertions, 7 deletions
diff --git a/src/database/sql/sql.go b/src/database/sql/sql.go
index e74dd875f9..0e0c3ef2ab 100644
--- a/src/database/sql/sql.go
+++ b/src/database/sql/sql.go
@@ -2136,11 +2136,10 @@ type Tx struct {
// any held driverConn back to the pool.
releaseConn func(error)
- // done transitions from 0 to 1 exactly once, on Commit
+ // done transitions from false to true exactly once, on Commit
// or Rollback. once done, all operations fail with
// ErrTxDone.
- // Use atomic operations on value when checking value.
- done int32
+ done atomic.Bool
// keepConnOnRollback is true if the driver knows
// how to reset the connection's session and if need be discard
@@ -2179,7 +2178,7 @@ func (tx *Tx) awaitDone() {
}
func (tx *Tx) isDone() bool {
- return atomic.LoadInt32(&tx.done) != 0
+ return tx.done.Load()
}
// ErrTxDone is returned by any operation that is performed on a transaction
@@ -2248,12 +2247,12 @@ func (tx *Tx) Commit() error {
select {
default:
case <-tx.ctx.Done():
- if atomic.LoadInt32(&tx.done) == 1 {
+ if tx.done.Load() {
return ErrTxDone
}
return tx.ctx.Err()
}
- if !atomic.CompareAndSwapInt32(&tx.done, 0, 1) {
+ if !tx.done.CompareAndSwap(false, true) {
return ErrTxDone
}
@@ -2281,7 +2280,7 @@ var rollbackHook func()
// rollback aborts the transaction and optionally forces the pool to discard
// the connection.
func (tx *Tx) rollback(discardConn bool) error {
- if !atomic.CompareAndSwapInt32(&tx.done, 0, 1) {
+ if !tx.done.CompareAndSwap(false, true) {
return ErrTxDone
}