clock.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // Package lamport implements a simple Lamport Clock for versioning
  16. package lamport
  17. import "sync"
  18. var Default = Clock{}
  19. type Clock struct {
  20. val uint64
  21. mut sync.Mutex
  22. }
  23. func (c *Clock) Tick(v uint64) uint64 {
  24. c.mut.Lock()
  25. if v > c.val {
  26. c.val = v + 1
  27. c.mut.Unlock()
  28. return v + 1
  29. } else {
  30. c.val++
  31. v = c.val
  32. c.mut.Unlock()
  33. return v
  34. }
  35. }