# Mutex for Parallel execution

**URL:** <https://discuss.gradle.org/t/mutex-for-parallel-execution/530>\
**Category:** Old Forum Archive\
**Created:** [August 26, 2013, 10:35am UTC](https://discuss.gradle.org/t/mutex-for-parallel-execution/530 "2013-08-26T10:35:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![corneil](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/corneil/32/90_2.png) [@corneil](https://discuss.gradle.org/u/corneil)\
**Post date:** [August 26, 2013, 10:35am UTC](https://discuss.gradle.org/t/mutex-for-parallel-execution/530/1 "2013-08-26T10:35:00Z")

</div>

I would suggest a simple mechanism where a task can be linked to named mutex to ensure that the task does not run at same time as other tasks with named mutex. The task execution mechanism will lock using named mutex before execution starts and unlock when execution is completed. If no mutex is defined the task executes as normal. Sample of definition could be:

```gradle
task MyTask {
    mutex = 'MyTaskMutex'
}

```

In a project where the same task is executed for many sub-projects the mutex will ensure serial execution for the task.

---

<div class="post-metadata">

**Author:** ![Attila\_Kelemen](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/attila_kelemen/32/12083_2.png) [@Attila\_Kelemen](https://discuss.gradle.org/u/Attila_Kelemen)\
**Post date:** [August 26, 2013, 5:58pm UTC](https://discuss.gradle.org/t/mutex-for-parallel-execution/530/2 "2013-08-26T17:58:00Z")

</div>

In general, I like the idea of allowing a form explicit synchronization. I’d adjust the idea by:

- Allow multiple mutexes. - Any type, not just strings (based on ‘equals’ and ‘hashCode’). Although this might require some special care. For example, call ‘toString’ on ‘GString’ instances. - There is a better way to implement this than explicit locks.

---

<div class="post-metadata">

**Author:** ![corneil](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/corneil/32/90_2.png) [@corneil](https://discuss.gradle.org/u/corneil)\
**Post date:** [August 28, 2013, 12:37pm UTC](https://discuss.gradle.org/t/mutex-for-parallel-execution/530/3 "2013-08-28T12:37:00Z")

</div>

Can you elaborate on ‘There is a better way to implement this than explicit locks.’ The reason I was thinking of a mutex is that I have a large project with over 300 sub projects and when I use parallel I get a nice improvement but failures with uploadArchives to local maven repo. A mutex on uploadArchives will resolve this problem.

---

<div class="post-metadata">

**Author:** ![Attila\_Kelemen](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/attila_kelemen/32/12083_2.png) [@Attila\_Kelemen](https://discuss.gradle.org/u/Attila_Kelemen)\
**Post date:** [August 28, 2013, 4:54pm UTC](https://discuss.gradle.org/t/mutex-for-parallel-execution/530/4 "2013-08-28T16:54:00Z")

</div>

Lock and wait is very dead-lock prone in this situation since there could be many locks and preventing the lock reordering issue is not trivial.

However, it can be implemented without using locks (like ReentrantLock), at least not for this purpose. For example, when Gradle executes a task it effectively pulls the task (I don’t know how it is actually implemented but should be something like that) to be executed from a set of tasks allowed to be executed (their dependencies has already been run). It should be possible to prevent pulling a task if a task is currently being executed which needs a “mutex” if the “mutex” is being held, simply by storing the held “mutexes” in a set.

So, it is effectively the same as locking without risking a dead-lock.

---

<div class="post-metadata">

**Author:** ![corneil](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/corneil/32/90_2.png) [@corneil](https://discuss.gradle.org/u/corneil)\
**Post date:** [August 28, 2013, 5:58pm UTC](https://discuss.gradle.org/t/mutex-for-parallel-execution/530/5 "2013-08-28T17:58:00Z")

</div>

This sounds promising. I’ll take a look at the code.

---

<div class="post-metadata">

**Author:** ![Mark\_Koops](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@Mark\_Koops](https://discuss.gradle.org/u/Mark_Koops)\
**Post date:** [September 10, 2013, 8:42am UTC](https://discuss.gradle.org/t/mutex-for-parallel-execution/530/6 "2013-09-10T08:42:00Z")

</div>

Did anyone of you already made progress on this?

Setting a mutex on my integration tests will allow me to run my build in parallel mode while the integration tests that depend on a single external server can performed sequentially.

---

<div class="post-metadata">

**Author:** ![Attila\_Kelemen](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/attila_kelemen/32/12083_2.png) [@Attila\_Kelemen](https://discuss.gradle.org/u/Attila_Kelemen)\
**Post date:** [September 10, 2013, 9:11am UTC](https://discuss.gradle.org/t/mutex-for-parallel-execution/530/7 "2013-09-10T09:11:00Z")

</div>

I didn’t do anything. I’m currently working on better NetBeans support, exploiting the changes expected in Gradle 1.8. So, I probably won’t work on this issue in the near future.

---

<div class="post-metadata">

**Author:** ![Mark\_Koops](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@Mark\_Koops](https://discuss.gradle.org/u/Mark_Koops)\
**Post date:** [October 25, 2013, 4:28pm UTC](https://discuss.gradle.org/t/mutex-for-parallel-execution/530/8 "2013-10-25T16:28:00Z")

</div>

I have just submitted my implementation for it on Github: [https://github.com/gradle/gradle/pull/206](https://github.com/gradle/gradle/pull/206)

–Mark
