bytesemaphore.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. if max < 0 {
  18. max = 0
  19. }
  20. s := byteSemaphore{
  21. max: max,
  22. available: max,
  23. }
  24. s.cond = sync.NewCond(&s.mut)
  25. return &s
  26. }
  27. func (s *byteSemaphore) take(bytes int) {
  28. s.mut.Lock()
  29. if bytes > s.max {
  30. bytes = s.max
  31. }
  32. for bytes > s.available {
  33. s.cond.Wait()
  34. if bytes > s.max {
  35. bytes = s.max
  36. }
  37. }
  38. s.available -= bytes
  39. s.mut.Unlock()
  40. }
  41. func (s *byteSemaphore) give(bytes int) {
  42. s.mut.Lock()
  43. if bytes > s.max {
  44. bytes = s.max
  45. }
  46. if s.available+bytes > s.max {
  47. s.available = s.max
  48. } else {
  49. s.available += bytes
  50. }
  51. s.cond.Broadcast()
  52. s.mut.Unlock()
  53. }
  54. func (s *byteSemaphore) setCapacity(cap int) {
  55. if cap < 0 {
  56. cap = 0
  57. }
  58. s.mut.Lock()
  59. diff := cap - s.max
  60. s.max = cap
  61. s.available += diff
  62. if s.available < 0 {
  63. s.available = 0
  64. } else if s.available > s.max {
  65. s.available = s.max
  66. }
  67. s.cond.Broadcast()
  68. s.mut.Unlock()
  69. }