-
Notifications
You must be signed in to change notification settings - Fork 719
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…3509) * cherry pick #3506 to release-4.0 Signed-off-by: ti-srebot <ti-srebot@pingcap.com> * fix conflict Signed-off-by: yisaer <disxiaofei@163.com> * use init Signed-off-by: yisaer <disxiaofei@163.com> Co-authored-by: Song Gao <disxiaofei@163.com> Co-authored-by: Ti Chi Robot <71242396+ti-chi-bot@users.noreply.github.com>
- Loading branch information
1 parent
cd3b13e
commit 05b9d6d
Showing
4 changed files
with
92 additions
and
5 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
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,54 @@ | ||
// Copyright 2021 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package statistics | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/phf/go-queue/queue" | ||
) | ||
|
||
// SafeQueue is a concurrency safe queue | ||
type SafeQueue struct { | ||
mu sync.Mutex | ||
que *queue.Queue | ||
} | ||
|
||
// NewSafeQueue return a SafeQueue | ||
func NewSafeQueue() *SafeQueue { | ||
sq := &SafeQueue{} | ||
sq.que = queue.New() | ||
return sq | ||
} | ||
|
||
// Init implement init | ||
func (sq *SafeQueue) Init() { | ||
sq.mu.Lock() | ||
defer sq.mu.Unlock() | ||
sq.que.Init() | ||
} | ||
|
||
// PushBack implement PushBack | ||
func (sq *SafeQueue) PushBack(v interface{}) { | ||
sq.mu.Lock() | ||
defer sq.mu.Unlock() | ||
sq.que.PushBack(v) | ||
} | ||
|
||
// PopFront implement PopFront | ||
func (sq *SafeQueue) PopFront() interface{} { | ||
sq.mu.Lock() | ||
defer sq.mu.Unlock() | ||
return sq.que.PopFront() | ||
} |
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,28 @@ | ||
// Copyright 2021 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package statistics | ||
|
||
import ( | ||
. "github.com/pingcap/check" | ||
) | ||
|
||
func (t *testMovingAvg) TestQueue(c *C) { | ||
sq := NewSafeQueue() | ||
sq.PushBack(1) | ||
sq.PushBack(2) | ||
v1 := sq.PopFront() | ||
v2 := sq.PopFront() | ||
c.Assert(1, Equals, v1.(int)) | ||
c.Assert(2, Equals, v2.(int)) | ||
} |