From a5a12a595d0fa27ecd2de678bf13033c4ae2e020 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Mon, 24 Jun 2019 18:12:30 +0100
Subject: [PATCH] Lock the space before attempting to remove a particle from
 the system. This should prevent duplicated removals.

---
 src/cell.c   | 20 ++++++++++++++++++++
 src/runner.c | 10 ++++++++++
 2 files changed, 30 insertions(+)

diff --git a/src/cell.c b/src/cell.c
index 115a0e253e..63f28a4719 100644
--- a/src/cell.c
+++ b/src/cell.c
@@ -4340,6 +4340,8 @@ void cell_drift_part(struct cell *c, const struct engine *e, int force) {
           /* One last action before death? */
           hydro_remove_part(p, xp);
 
+          lock_lock(&e->s->lock);
+
           /* Remove the particle entirely */
           struct gpart *gp = p->gpart;
           cell_remove_part(e, c, p, xp);
@@ -4349,6 +4351,9 @@ void cell_drift_part(struct cell *c, const struct engine *e, int force) {
             cell_remove_gpart(e, c, gp);
           }
 
+          if (lock_unlock(&e->s->lock) != 0)
+            error("Failed to unlock the space!");
+
           continue;
         }
       }
@@ -4497,9 +4502,14 @@ void cell_drift_gpart(struct cell *c, const struct engine *e, int force) {
             (gp->x[1] > dim[1]) || (gp->x[1] < 0.) ||  // y
             (gp->x[2] > dim[2]) || (gp->x[2] < 0.)) {  // z
 
+          lock_lock(&e->s->lock);
+
           /* Remove the particle entirely */
           cell_remove_gpart(e, c, gp);
 
+          if (lock_unlock(&e->s->lock) != 0)
+            error("Failed to unlock the space!");
+
           continue;
         }
       }
@@ -4627,6 +4637,8 @@ void cell_drift_spart(struct cell *c, const struct engine *e, int force) {
             (sp->x[1] > dim[1]) || (sp->x[1] < 0.) ||  // y
             (sp->x[2] > dim[2]) || (sp->x[2] < 0.)) {  // z
 
+          lock_lock(&e->s->lock);
+
           /* Remove the particle entirely */
           struct gpart *gp = sp->gpart;
           cell_remove_spart(e, c, sp);
@@ -4634,6 +4646,9 @@ void cell_drift_spart(struct cell *c, const struct engine *e, int force) {
           /* and it's gravity friend */
           cell_remove_gpart(e, c, gp);
 
+          if (lock_unlock(&e->s->lock) != 0)
+            error("Failed to unlock the space!");
+
           continue;
         }
       }
@@ -4792,6 +4807,8 @@ void cell_drift_bpart(struct cell *c, const struct engine *e, int force) {
             (bp->x[1] > dim[1]) || (bp->x[1] < 0.) ||  // y
             (bp->x[2] > dim[2]) || (bp->x[2] < 0.)) {  // z
 
+          lock_lock(&e->s->lock);
+
           /* Remove the particle entirely */
           struct gpart *gp = bp->gpart;
           cell_remove_bpart(e, c, bp);
@@ -4799,6 +4816,9 @@ void cell_drift_bpart(struct cell *c, const struct engine *e, int force) {
           /* and it's gravity friend */
           cell_remove_gpart(e, c, gp);
 
+          if (lock_unlock(&e->s->lock) != 0)
+            error("Failed to unlock the space!");
+
           continue;
         }
       }
diff --git a/src/runner.c b/src/runner.c
index 622bd33717..8a966e9bc9 100644
--- a/src/runner.c
+++ b/src/runner.c
@@ -3820,10 +3820,15 @@ void runner_do_swallow(struct runner *r, struct cell *c, int timer) {
 
               message("BH %lld removing particle %lld", bp->id, p->id);
 
+              lock_lock(&e->s->lock);
+
               /* Finally, remove the gas particle from the system */
               struct gpart *gp = p->gpart;
               cell_remove_part(e, c, p, xp);
               cell_remove_gpart(e, c, gp);
+
+              if (lock_unlock(&e->s->lock) != 0)
+                error("Failed to unlock the space!");
             }
 
             /* In any case, prevent the particle from being re-swallowed */
@@ -3853,11 +3858,16 @@ void runner_do_swallow(struct runner *r, struct cell *c, int timer) {
               message("BH %lld removing particle %lld (foreign BH case)",
                       bp->id, p->id);
 
+              lock_lock(&e->s->lock);
+
               /* Finally, remove the gas particle from the system */
               struct gpart *gp = p->gpart;
               cell_remove_part(e, c, p, xp);
               cell_remove_gpart(e, c, gp);
 
+              if (lock_unlock(&e->s->lock) != 0)
+                error("Failed to unlock the space!");
+
               found = 1;
               break;
             }
-- 
GitLab