2018-11-13 07:53:55 +00:00
|
|
|
// Copyright (C) 2018 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
2018-12-05 07:40:05 +00:00
|
|
|
import (
|
2020-02-24 20:57:15 +00:00
|
|
|
"context"
|
2018-12-05 07:40:05 +00:00
|
|
|
"sync"
|
|
|
|
)
|
2018-11-13 07:53:55 +00:00
|
|
|
|
|
|
|
type byteSemaphore struct {
|
|
|
|
max int
|
|
|
|
available int
|
|
|
|
mut sync.Mutex
|
|
|
|
cond *sync.Cond
|
|
|
|
}
|
|
|
|
|
|
|
|
func newByteSemaphore(max int) *byteSemaphore {
|
2020-02-01 07:02:18 +00:00
|
|
|
if max < 0 {
|
|
|
|
max = 0
|
|
|
|
}
|
2018-11-13 07:53:55 +00:00
|
|
|
s := byteSemaphore{
|
|
|
|
max: max,
|
|
|
|
available: max,
|
|
|
|
}
|
|
|
|
s.cond = sync.NewCond(&s.mut)
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
2020-02-24 20:57:15 +00:00
|
|
|
func (s *byteSemaphore) takeWithContext(ctx context.Context, bytes int) error {
|
|
|
|
done := make(chan struct{})
|
|
|
|
var err error
|
|
|
|
go func() {
|
|
|
|
err = s.takeInner(ctx, bytes)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-ctx.Done():
|
|
|
|
s.cond.Broadcast()
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-13 07:53:55 +00:00
|
|
|
func (s *byteSemaphore) take(bytes int) {
|
2020-02-24 20:57:15 +00:00
|
|
|
_ = s.takeInner(context.Background(), bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *byteSemaphore) takeInner(ctx context.Context, bytes int) error {
|
2018-12-05 07:40:05 +00:00
|
|
|
s.mut.Lock()
|
2020-02-24 20:57:15 +00:00
|
|
|
defer s.mut.Unlock()
|
2018-11-13 07:53:55 +00:00
|
|
|
if bytes > s.max {
|
|
|
|
bytes = s.max
|
|
|
|
}
|
|
|
|
for bytes > s.available {
|
|
|
|
s.cond.Wait()
|
2020-02-24 20:57:15 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2018-12-05 07:40:05 +00:00
|
|
|
if bytes > s.max {
|
|
|
|
bytes = s.max
|
|
|
|
}
|
2018-11-13 07:53:55 +00:00
|
|
|
}
|
|
|
|
s.available -= bytes
|
2020-02-24 20:57:15 +00:00
|
|
|
return nil
|
2018-11-13 07:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *byteSemaphore) give(bytes int) {
|
2018-12-05 07:40:05 +00:00
|
|
|
s.mut.Lock()
|
2018-11-13 07:53:55 +00:00
|
|
|
if bytes > s.max {
|
|
|
|
bytes = s.max
|
|
|
|
}
|
|
|
|
if s.available+bytes > s.max {
|
2018-12-05 07:40:05 +00:00
|
|
|
s.available = s.max
|
|
|
|
} else {
|
|
|
|
s.available += bytes
|
|
|
|
}
|
|
|
|
s.cond.Broadcast()
|
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *byteSemaphore) setCapacity(cap int) {
|
2020-02-01 07:02:18 +00:00
|
|
|
if cap < 0 {
|
|
|
|
cap = 0
|
|
|
|
}
|
2018-12-05 07:40:05 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
diff := cap - s.max
|
|
|
|
s.max = cap
|
|
|
|
s.available += diff
|
|
|
|
if s.available < 0 {
|
|
|
|
s.available = 0
|
|
|
|
} else if s.available > s.max {
|
|
|
|
s.available = s.max
|
2018-11-13 07:53:55 +00:00
|
|
|
}
|
|
|
|
s.cond.Broadcast()
|
|
|
|
s.mut.Unlock()
|
|
|
|
}
|