From 9e7ee361c733341b72c21634837532ef4bc7fa79 Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Wed, 2 May 2012 19:56:35 -0400 Subject: [PATCH] Tags.exclusive convenience method for executing tasks in isolation --- main/Tags.scala | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/main/Tags.scala b/main/Tags.scala index 9d32dde25..231792477 100644 --- a/main/Tags.scala +++ b/main/Tags.scala @@ -52,9 +52,28 @@ object Tags def getInt(m: TagMap, tag: Tag): Int = m.getOrElse(tag, 0) + /** Constructs a custom Rule from the predicate `f`. + * The input represents the weighted tags of a set of tasks. + * The function `f` should return true if those tasks are allowed to execute concurrently and false if they are not. + * + * If there is only one task represented by the map, it must be allowed to execute.*/ def customLimit(f: TagMap => Boolean): Rule = new Custom(f) + + /** Returns a Rule that limits the maximum number of concurrently executing tasks to `max`, regardless of tags. */ def limitAll(max: Int): Rule = limit(All, max) + + /** Returns a Rule that limits the maximum number of concurrently executing tasks without a tag to `max`. */ def limitUntagged(max: Int): Rule = limit(Untagged, max) + + /** Returns a Rule that limits the maximum number of concurrent executings tasks tagged with `tag` to `max`.*/ def limit(tag: Tag, max: Int): Rule = new Single(tag, max) + def limitSum(max: Int, tags: Tag*): Rule = new Sum(tags, max) + /** Ensure that a task with the given tag always executes in isolation.*/ + def exclusive(exclusiveTag: Tag): Rule = customLimit { (tags: Map[Tag,Int]) => + // if there are no exclusive tasks in this group, this rule adds no restrictions + tags.getOrElse(exclusiveTag, 0) == 0 || + // If there is only one task, allow it to execute. + tags.getOrElse(Tags.All, 0) == 1 + } }