Skip to content

Commit

Permalink
use atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Sep 26, 2024
1 parent 73f31d2 commit 62309d3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
9 changes: 3 additions & 6 deletions x/move/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"context"
"errors"
"sync"

"golang.org/x/sync/semaphore"

Expand Down Expand Up @@ -46,9 +45,8 @@ type Keeper struct {

// TODO - remove after loader v2
moveVMs []types.VMEngine
moveVMMutx *sync.Mutex
moveVMIdx *uint64
moveVMSemaphore *semaphore.Weighted
moveVMIdx *int

feeCollector string
authority string
Expand Down Expand Up @@ -92,7 +90,7 @@ func NewKeeper(
}

vmCount := 10
moveVMIdx := int(0)
moveVMIdx := uint64(0)
vms := make([]types.VMEngine, vmCount)
for i := 0; i < vmCount; i++ {
moveVM, err := vm.NewVM(vmtypes.InitiaVMConfig{
Expand All @@ -117,9 +115,8 @@ func NewKeeper(
grpcRouter: grpcRouter,
config: moveConfig,
moveVMs: vms,
moveVMMutx: new(sync.Mutex),
moveVMSemaphore: semaphore.NewWeighted(int64(vmCount)),
moveVMIdx: &moveVMIdx,
moveVMSemaphore: semaphore.NewWeighted(int64(vmCount)),
distrKeeper: distrKeeper,
StakingKeeper: stakingKeeper,
RewardKeeper: rewardKeeper,
Expand Down
8 changes: 3 additions & 5 deletions x/move/keeper/vmpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"sync/atomic"

"github.com/initia-labs/initia/x/move/types"
)
Expand All @@ -12,11 +13,8 @@ func (k Keeper) acquireVM(ctx context.Context) (vm types.VMEngine) {
panic(err)
}

k.moveVMMutx.Lock()
idx := *k.moveVMIdx
*k.moveVMIdx = (idx + 1) % len(k.moveVMs)
vm = k.moveVMs[idx]
k.moveVMMutx.Unlock()
idx := atomic.AddUint64(k.moveVMIdx, 1)
vm = k.moveVMs[idx%uint64(len(k.moveVMs))]

return
}
Expand Down

0 comments on commit 62309d3

Please sign in to comment.