sync.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (C) 2015 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 http://mozilla.org/MPL/2.0/.
  6. package sync
  7. import (
  8. "fmt"
  9. "path/filepath"
  10. "runtime"
  11. "strings"
  12. "sync"
  13. "sync/atomic"
  14. "time"
  15. )
  16. type Mutex interface {
  17. Lock()
  18. Unlock()
  19. }
  20. type RWMutex interface {
  21. Mutex
  22. RLock()
  23. RUnlock()
  24. }
  25. type WaitGroup interface {
  26. Add(int)
  27. Done()
  28. Wait()
  29. }
  30. func NewMutex() Mutex {
  31. if debug {
  32. return &loggedMutex{}
  33. }
  34. return &sync.Mutex{}
  35. }
  36. func NewRWMutex() RWMutex {
  37. if debug {
  38. return &loggedRWMutex{
  39. unlockers: make([]string, 0),
  40. }
  41. }
  42. return &sync.RWMutex{}
  43. }
  44. func NewWaitGroup() WaitGroup {
  45. if debug {
  46. return &loggedWaitGroup{}
  47. }
  48. return &sync.WaitGroup{}
  49. }
  50. type loggedMutex struct {
  51. sync.Mutex
  52. start time.Time
  53. lockedAt string
  54. }
  55. func (m *loggedMutex) Lock() {
  56. m.Mutex.Lock()
  57. m.start = time.Now()
  58. m.lockedAt = getCaller()
  59. }
  60. func (m *loggedMutex) Unlock() {
  61. duration := time.Now().Sub(m.start)
  62. if duration >= threshold {
  63. l.Debugf("Mutex held for %v. Locked at %s unlocked at %s", duration, m.lockedAt, getCaller())
  64. }
  65. m.Mutex.Unlock()
  66. }
  67. type loggedRWMutex struct {
  68. sync.RWMutex
  69. start time.Time
  70. lockedAt string
  71. logUnlockers uint32
  72. unlockers []string
  73. unlockersMut sync.Mutex
  74. }
  75. func (m *loggedRWMutex) Lock() {
  76. start := time.Now()
  77. atomic.StoreUint32(&m.logUnlockers, 1)
  78. m.RWMutex.Lock()
  79. m.logUnlockers = 0
  80. m.start = time.Now()
  81. duration := m.start.Sub(start)
  82. m.lockedAt = getCaller()
  83. if duration > threshold {
  84. l.Debugf("RWMutex took %v to lock. Locked at %s. RUnlockers while locking: %s", duration, m.lockedAt, strings.Join(m.unlockers, ", "))
  85. }
  86. m.unlockers = m.unlockers[0:]
  87. }
  88. func (m *loggedRWMutex) Unlock() {
  89. duration := time.Now().Sub(m.start)
  90. if duration >= threshold {
  91. l.Debugf("RWMutex held for %v. Locked at %s: unlocked at %s", duration, m.lockedAt, getCaller())
  92. }
  93. m.RWMutex.Unlock()
  94. }
  95. func (m *loggedRWMutex) RUnlock() {
  96. if atomic.LoadUint32(&m.logUnlockers) == 1 {
  97. m.unlockersMut.Lock()
  98. m.unlockers = append(m.unlockers, getCaller())
  99. m.unlockersMut.Unlock()
  100. }
  101. m.RWMutex.RUnlock()
  102. }
  103. type loggedWaitGroup struct {
  104. sync.WaitGroup
  105. }
  106. func (wg *loggedWaitGroup) Wait() {
  107. start := time.Now()
  108. wg.WaitGroup.Wait()
  109. duration := time.Now().Sub(start)
  110. if duration >= threshold {
  111. l.Debugf("WaitGroup took %v at %s", duration, getCaller())
  112. }
  113. }
  114. func getCaller() string {
  115. _, file, line, _ := runtime.Caller(2)
  116. file = filepath.Join(filepath.Base(filepath.Dir(file)), filepath.Base(file))
  117. return fmt.Sprintf("%s:%d", file, line)
  118. }