Skip to content

Commit

Permalink
feat: Substitute mutex with atomic operations.
Browse files Browse the repository at this point in the history
Use atomic operations instead a mutex. This approach avoids locking and
increment can be done within a single clock cycle, hence no deadlock nor
race condition can occur.

Signed-off-by: Ville Valkonen <weezel@users.noreply.github.com>
  • Loading branch information
weezel committed Sep 22, 2024
1 parent 4382fdf commit 63d1cf4
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions filepicker/filepicker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,20 @@ import (
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"

"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/dustin/go-humanize"
)

var (
lastID int
idMtx sync.Mutex
)
var lastID atomic.Int64

// Return the next ID we should use on the Model.
func nextID() int {
idMtx.Lock()
defer idMtx.Unlock()
lastID++
return lastID
lastID.Add(1)
return int(lastID.Load())
}

// New returns a new filepicker model with default styling and key bindings.
Expand Down

0 comments on commit 63d1cf4

Please sign in to comment.