github.com/kayoticsully/syncthing@v0.8.9-0.20140724133906-c45a2fdc03f8/lamport/clock.go (about)

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