bytesemaphore.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (C) 2018 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "sync"
  9. )
  10. type byteSemaphore struct {
  11. max int
  12. available int
  13. mut sync.Mutex
  14. cond *sync.Cond
  15. }
  16. func newByteSemaphore(max int) *byteSemaphore {
  17. s := byteSemaphore{
  18. max: max,
  19. available: max,
  20. }
  21. s.cond = sync.NewCond(&s.mut)
  22. return &s
  23. }
  24. func (s *byteSemaphore) take(bytes int) {
  25. s.mut.Lock()
  26. if bytes > s.max {
  27. bytes = s.max
  28. }
  29. for bytes > s.available {
  30. s.cond.Wait()
  31. if bytes > s.max {
  32. bytes = s.max
  33. }
  34. }
  35. s.available -= bytes
  36. s.mut.Unlock()
  37. }
  38. func (s *byteSemaphore) give(bytes int) {
  39. s.mut.Lock()
  40. if bytes > s.max {
  41. bytes = s.max
  42. }
  43. if s.available+bytes > s.max {
  44. s.available = s.max
  45. } else {
  46. s.available += bytes
  47. }
  48. s.cond.Broadcast()
  49. s.mut.Unlock()
  50. }
  51. func (s *byteSemaphore) setCapacity(cap int) {
  52. s.mut.Lock()
  53. diff := cap - s.max
  54. s.max = cap
  55. s.available += diff
  56. if s.available < 0 {
  57. s.available = 0
  58. } else if s.available > s.max {
  59. s.available = s.max
  60. }
  61. s.cond.Broadcast()
  62. s.mut.Unlock()
  63. }