clock.go 605 B

12345678910111213141516171819202122232425262728293031
  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 lamport implements a simple Lamport Clock for versioning
  7. package lamport
  8. import "sync"
  9. var Default = Clock{}
  10. type Clock struct {
  11. val int64
  12. mut sync.Mutex
  13. }
  14. func (c *Clock) Tick(v int64) int64 {
  15. c.mut.Lock()
  16. if v > c.val {
  17. c.val = v + 1
  18. c.mut.Unlock()
  19. return v + 1
  20. } else {
  21. c.val++
  22. v = c.val
  23. c.mut.Unlock()
  24. return v
  25. }
  26. }