mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-09-24 03:13:32 +08:00
moved. there are some deadlock. WIP
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package util
|
package buffered_queue
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
@@ -22,20 +22,25 @@ type BufferedQueue[T any] struct {
|
|||||||
count int // Total number of items in the queue
|
count int // Total number of items in the queue
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
nodeCounter int
|
nodeCounter int
|
||||||
|
waitOnRead bool
|
||||||
|
waitCond *sync.Cond
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBufferedQueue creates a new buffered queue with the specified chunk size
|
// NewBufferedQueue creates a new buffered queue with the specified chunk size
|
||||||
func NewBufferedQueue[T any](chunkSize int) *BufferedQueue[T] {
|
func NewBufferedQueue[T any](chunkSize int, waitOnRead bool) *BufferedQueue[T] {
|
||||||
// Create an empty chunk to initialize head and tail
|
// Create an empty chunk to initialize head and tail
|
||||||
chunk := &ItemChunkNode[T]{items: make([]T, chunkSize), nodeId: 0}
|
chunk := &ItemChunkNode[T]{items: make([]T, chunkSize), nodeId: 0}
|
||||||
return &BufferedQueue[T]{
|
bq := &BufferedQueue[T]{
|
||||||
chunkSize: chunkSize,
|
chunkSize: chunkSize,
|
||||||
head: chunk,
|
head: chunk,
|
||||||
tail: chunk,
|
tail: chunk,
|
||||||
last: chunk,
|
last: chunk,
|
||||||
count: 0,
|
count: 0,
|
||||||
mutex: sync.Mutex{},
|
mutex: sync.Mutex{},
|
||||||
|
waitOnRead: waitOnRead,
|
||||||
}
|
}
|
||||||
|
bq.waitCond = sync.NewCond(&bq.mutex)
|
||||||
|
return bq
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enqueue adds a job to the queue
|
// Enqueue adds a job to the queue
|
||||||
@@ -65,6 +70,9 @@ func (q *BufferedQueue[T]) Enqueue(job T) {
|
|||||||
q.tail.items[q.tail.tailIndex] = job
|
q.tail.items[q.tail.tailIndex] = job
|
||||||
q.tail.tailIndex++
|
q.tail.tailIndex++
|
||||||
q.count++
|
q.count++
|
||||||
|
if q.waitOnRead {
|
||||||
|
q.waitCond.Signal()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dequeue removes and returns a job from the queue
|
// Dequeue removes and returns a job from the queue
|
||||||
@@ -72,10 +80,16 @@ func (q *BufferedQueue[T]) Dequeue() (T, bool) {
|
|||||||
q.mutex.Lock()
|
q.mutex.Lock()
|
||||||
defer q.mutex.Unlock()
|
defer q.mutex.Unlock()
|
||||||
|
|
||||||
|
if q.waitOnRead {
|
||||||
|
for q.count <= 0 {
|
||||||
|
q.waitCond.Wait()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if q.count == 0 {
|
if q.count == 0 {
|
||||||
var a T
|
var a T
|
||||||
return a, false
|
return a, false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
job := q.head.items[q.head.headIndex]
|
job := q.head.items[q.head.headIndex]
|
||||||
q.head.headIndex++
|
q.head.headIndex++
|
@@ -1,4 +1,4 @@
|
|||||||
package util
|
package buffered_queue
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
@@ -9,7 +9,7 @@ func TestJobQueue(t *testing.T) {
|
|||||||
Data T
|
Data T
|
||||||
}
|
}
|
||||||
|
|
||||||
queue := NewBufferedQueue[Job[string]](2) // Chunk size of 5
|
queue := NewBufferedQueue[Job[string]](2, false) // Chunk size of 5
|
||||||
queue.Enqueue(Job[string]{ID: 1, Action: "task1", Data: "hello"})
|
queue.Enqueue(Job[string]{ID: 1, Action: "task1", Data: "hello"})
|
||||||
queue.Enqueue(Job[string]{ID: 2, Action: "task2", Data: "world"})
|
queue.Enqueue(Job[string]{ID: 2, Action: "task2", Data: "world"})
|
||||||
|
|
||||||
@@ -76,3 +76,25 @@ func TestJobQueue(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkBufferedQueue(b *testing.B) {
|
||||||
|
type Job[T any] struct {
|
||||||
|
ID int
|
||||||
|
Action string
|
||||||
|
Data T
|
||||||
|
}
|
||||||
|
|
||||||
|
queue := NewBufferedQueue[Job[string]](1024, true)
|
||||||
|
|
||||||
|
b.Run("Enqueue", func(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
queue.Enqueue(Job[string]{ID: i, Action: "task", Data: "data"})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Run("Dequeue", func(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_, _ = queue.Dequeue()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Reference in New Issue
Block a user