util.go 635 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright (C) 2014 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 model
  7. import (
  8. "sync"
  9. "time"
  10. )
  11. func deadlockDetect(mut sync.Locker, timeout time.Duration) {
  12. go func() {
  13. for {
  14. time.Sleep(timeout / 4)
  15. ok := make(chan bool, 2)
  16. go func() {
  17. mut.Lock()
  18. mut.Unlock()
  19. ok <- true
  20. }()
  21. go func() {
  22. time.Sleep(timeout)
  23. ok <- false
  24. }()
  25. if r := <-ok; !r {
  26. panic("deadlock detected")
  27. }
  28. }
  29. }()
  30. }