util.go 590 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. package model
  5. import (
  6. "sync"
  7. "time"
  8. )
  9. func deadlockDetect(mut sync.Locker, timeout time.Duration) {
  10. go func() {
  11. for {
  12. time.Sleep(timeout / 4)
  13. ok := make(chan bool, 2)
  14. go func() {
  15. mut.Lock()
  16. mut.Unlock()
  17. ok <- true
  18. }()
  19. go func() {
  20. time.Sleep(timeout)
  21. ok <- false
  22. }()
  23. if r := <-ok; !r {
  24. panic("deadlock detected")
  25. }
  26. }
  27. }()
  28. }