Skip to content

Commit

Permalink
Merge pull request #1 from vkuptcov/feature-fix-possible-concurrent-t…
Browse files Browse the repository at this point in the history
…imer-update

+ use AtomicLong instead a local long as a logical timestamp
  • Loading branch information
maartendecat committed Mar 19, 2015
2 parents 426904a + fd0ec6e commit 75c027b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
*.class
target/
*~
build.sbt
project
stapl-core.iml
11 changes: 5 additions & 6 deletions src/main/scala/stapl/core/pdp/TimestampGenerator.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package stapl.core.pdp

import java.util.concurrent.atomic.AtomicLong

/**
* Class responsible for generating a globally unique timestamp. These timestamps
* are longs that have an ascending order. Notice that these timestamps do not
Expand All @@ -15,22 +17,19 @@ trait TimestampGenerator {
}

/**
* Simple implementation of a timing server: just increment a local variable.
* Simple implementation of a timing server.
* Not a robust implementation for distributed scenarios: it is hard to replace it
* by another timing server because the count is not known externally.
*/
class SimpleTimestampGenerator extends TimestampGenerator {

private var counter: Long = 0
private val counter = new AtomicLong(0)

/**
* Simple implementation using an internal counter. This leads to a possible
* exception in uniqueness in case of long roll-over, but this is assumed to fall
* outside the expected lifespan of any request that might still be executing system-wide.
*/
override def getTimestamp(): String = {
counter = counter + 1
s"$counter"
}
override def getTimestamp(): String = counter.incrementAndGet().toString

}

0 comments on commit 75c027b

Please sign in to comment.