aboutsummaryrefslogtreecommitdiff
path: root/pkg/worker/scheduler.go
blob: b410b33714bcb0541a668f7af10fcce82b1dcf0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package worker

import (
	"fmt"
	"sync/atomic"
)

type Scheduler struct {
	pool  chan any
	count atomic.Int64
}

func NewScheduler(count uint) *Scheduler {
	return &Scheduler{
		pool: make(chan any, count),
	}
}

func (self *Scheduler) Take() {
	self.pool <- nil
	self.count.Add(1)
	fmt.Printf("<- %d\n", self.count.Load())
}

func (self *Scheduler) Return() {
	<-self.pool
	self.count.Add(-1)
	fmt.Printf("-> %d\n", self.count.Load())
}