mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.
## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
106 lines
2.0 KiB
Go
106 lines
2.0 KiB
Go
package executor
|
|
|
|
import (
|
|
"container/list"
|
|
"sync"
|
|
)
|
|
|
|
type (
|
|
runQueue struct {
|
|
tasks chan Task
|
|
deferredTasks *threadSafeList
|
|
stopC chan struct{}
|
|
}
|
|
|
|
// threadSafeList is a wrapper around list.List which
|
|
// guarantees thread safety
|
|
threadSafeList struct {
|
|
sync.Mutex
|
|
list *list.List
|
|
}
|
|
)
|
|
|
|
// newRunQueue returns an instance of task run queue
|
|
func newRunQueue(size int, stopC chan struct{}) *runQueue {
|
|
return &runQueue{
|
|
tasks: make(chan Task, size),
|
|
deferredTasks: newThreadSafeList(),
|
|
stopC: stopC,
|
|
}
|
|
}
|
|
|
|
func (rq *runQueue) add(task Task) bool {
|
|
select {
|
|
case rq.tasks <- task:
|
|
return true
|
|
case <-rq.stopC:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (rq *runQueue) addAndDefer(task Task) {
|
|
rq.deferredTasks.add(task)
|
|
}
|
|
|
|
// remove returns the next task from run queue
|
|
// if there are no active tasks in runq, a task
|
|
// from deferredQ is returned
|
|
func (rq *runQueue) remove() (Task, bool) {
|
|
// Give 1st priority to taskQ -if there are no more tasks, handout tasks from deferredQ
|
|
select {
|
|
case job, ok := <-rq.tasks:
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
return job, true
|
|
default:
|
|
if task := rq.deferredTasks.remove(); task != nil {
|
|
return task.Value.(Task), true
|
|
}
|
|
}
|
|
// at this point, there are no tasks either in taskQ or deferredQ
|
|
// simply block on taskQ until we have tasks
|
|
select {
|
|
case job, ok := <-rq.tasks:
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
return job, true
|
|
case <-rq.stopC:
|
|
return nil, false
|
|
}
|
|
}
|
|
|
|
func (rq *runQueue) deferredCount() int {
|
|
return rq.deferredTasks.len()
|
|
}
|
|
|
|
// newThreadSafeList returns a new thread safe linked list
|
|
func newThreadSafeList() *threadSafeList {
|
|
return &threadSafeList{
|
|
list: list.New(),
|
|
}
|
|
}
|
|
|
|
func (tl *threadSafeList) add(elem interface{}) {
|
|
tl.Lock()
|
|
defer tl.Unlock()
|
|
tl.list.PushBack(elem)
|
|
}
|
|
|
|
func (tl *threadSafeList) len() int {
|
|
tl.Lock()
|
|
defer tl.Unlock()
|
|
return tl.list.Len()
|
|
}
|
|
|
|
func (tl *threadSafeList) remove() *list.Element {
|
|
tl.Lock()
|
|
defer tl.Unlock()
|
|
e := tl.list.Front()
|
|
if e != nil {
|
|
tl.list.Remove(e)
|
|
}
|
|
return e
|
|
}
|