Created
August 25, 2021 00:19
-
-
Save CAFxX/6957aabdd929231c9785575e9d13d606 to your computer and use it in GitHub Desktop.
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
package main | |
import "sync" | |
import "fmt" | |
type Work interface { | |
Process() | |
} | |
type WorkFunc func() | |
func (wf WorkFunc) Process() { | |
wf() | |
} | |
type Order uint8 | |
const ( | |
Unspecified Order = iota | |
FIFO | |
LIFO | |
) | |
type Queue[W Work] struct { | |
MaxWorkers int | |
MaxQueue int | |
Order Order | |
l sync.Mutex | |
c sync.Cond | |
nwrk int | |
q queue[W] | |
wg sync.WaitGroup | |
} | |
func (q *Queue[W]) Enqueue(w W) { | |
q.l.Lock() | |
for q.MaxQueue > 0 && q.q.len() >= q.MaxQueue { | |
q.c.L = &q.l | |
q.c.Wait() | |
} | |
q.q.pushBack(w) | |
if q.MaxWorkers > 0 && q.nwrk >= q.MaxWorkers { | |
q.l.Unlock() | |
return | |
} | |
q.wg.Add(1) | |
go func() { | |
processing := false | |
q.nwrk++ | |
defer func() { | |
if processing { | |
q.l.Lock() | |
} | |
q.nwrk-- | |
q.wg.Done() | |
q.l.Unlock() | |
}() | |
for q.q.len() > 0 { | |
if q.MaxQueue > 0 && q.q.len() >= q.MaxQueue { | |
q.c.L = &q.l | |
q.c.Signal() | |
} | |
var w W | |
if q.Order == FIFO { | |
w, _ = q.q.popFront() | |
} else { | |
w, _ = q.q.popBack() | |
} | |
q.l.Unlock() | |
processing = true | |
w.Process() | |
processing = false | |
q.l.Lock() | |
} | |
}() | |
} | |
func (q *Queue[W]) Wait() { | |
q.wg.Wait() | |
} | |
type queue[T any] struct { | |
q []T | |
r int | |
w int | |
} | |
func (q *queue[T]) len() int { | |
return q.w - q.r | |
} | |
func (q *queue[T]) pushBack(v T) { | |
if q.r == q.w { | |
q.r, q.w = 0, 0 | |
} | |
if q.w >= len(q.q) { | |
if q.w-q.r < len(q.q)/2 { | |
copy(q.q, q.q[q.r:q.w]) | |
} else { | |
l := len(q.q) | |
if l == 0 { | |
l = 1 | |
} | |
qn := make([]T, l*2) | |
copy(qn, q.q[q.r:q.w]) | |
q.q = qn | |
} | |
q.r, q.w = 0, q.w-q.r | |
} | |
q.q[q.w] = v | |
q.w++ | |
} | |
func (q *queue[T]) popFront() (T, bool) { | |
var zero T | |
if q.r == q.w { | |
return zero, false | |
} | |
v := q.q[q.r] | |
q.q[q.r] = zero | |
q.r++ | |
if q.w-q.r < len(q.q)/8 { | |
qn := make([]T, len(q.q)/4) | |
copy(qn, q.q[q.r:q.w]) | |
q.q, q.r, q.w = qn, 0, q.w-q.r | |
} | |
return v, true | |
} | |
func (q *queue[T]) popBack() (T, bool) { | |
var zero T | |
if q.r == q.w { | |
return zero, false | |
} | |
q.w-- | |
v := q.q[q.w] | |
q.q[q.w] = zero | |
if q.w-q.r < len(q.q)/8 { | |
qn := make([]T, len(q.q)/4) | |
copy(qn, q.q[q.r:q.w]) | |
q.q, q.r, q.w = qn, 0, q.w-q.r | |
} | |
return v, true | |
} | |
func main() { | |
q := Queue[Work]{MaxWorkers: 4, Order: FIFO, MaxQueue: 4} | |
for i := 0; i < 10000; i++ { | |
i := i | |
q.Enqueue(WorkFunc(func() { | |
fmt.Println("hello world", i) | |
})) | |
} | |
q.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
simpler version