Commit cd0f858e authored by Matthieu Schaller's avatar Matthieu Schaller
Browse files

Merge branch 'sink_lock2' into 'master'

Implement cell lock

See merge request !1120
parents 19bfdbab 09a660e7
......@@ -1486,6 +1486,66 @@ int cell_slocktree(struct cell *c) {
}
}
/**
* @brief Lock a cell for access to its array of #sink and hold its parents.
*
* @param c The #cell.
* @return 0 on success, 1 on failure
*/
int cell_sink_locktree(struct cell *c) {
TIMER_TIC;
/* First of all, try to lock this cell. */
if (c->sinks.hold || lock_trylock(&c->sinks.lock) != 0) {
TIMER_TOC(timer_locktree);
return 1;
}
/* Did somebody hold this cell in the meantime? */
if (c->sinks.hold) {
/* Unlock this cell. */
if (lock_unlock(&c->sinks.lock) != 0) error("Failed to unlock cell.");
/* Admit defeat. */
TIMER_TOC(timer_locktree);
return 1;
}
/* Climb up the tree and lock/hold/unlock. */
struct cell *finger;
for (finger = c->parent; finger != NULL; finger = finger->parent) {
/* Lock this cell. */
if (lock_trylock(&finger->sinks.lock) != 0) break;
/* Increment the hold. */
atomic_inc(&finger->sinks.hold);
/* Unlock the cell. */
if (lock_unlock(&finger->sinks.lock) != 0) error("Failed to unlock cell.");
}
/* If we reached the top of the tree, we're done. */
if (finger == NULL) {
TIMER_TOC(timer_locktree);
return 0;
}
/* Otherwise, we hit a snag. */
else {
/* Undo the holds up to finger. */
for (struct cell *finger2 = c->parent; finger2 != finger;
finger2 = finger2->parent)
atomic_dec(&finger2->sinks.hold);
/* Unlock this cell. */
if (lock_unlock(&c->sinks.lock) != 0) error("Failed to unlock cell.");
/* Admit defeat. */
TIMER_TOC(timer_locktree);
return 1;
}
}
/**
* @brief Lock a cell for access to its array of #bpart and hold its parents.
*
......@@ -1619,6 +1679,24 @@ void cell_sunlocktree(struct cell *c) {
TIMER_TOC(timer_locktree);
}
/**
* @brief Unlock a cell's parents for access to #sink array.
*
* @param c The #cell.
*/
void cell_sink_unlocktree(struct cell *c) {
TIMER_TIC;
/* First of all, try to unlock this cell. */
if (lock_unlock(&c->sinks.lock) != 0) error("Failed to unlock cell.");
/* Climb up the tree and unhold the parents. */
for (struct cell *finger = c->parent; finger != NULL; finger = finger->parent)
atomic_dec(&finger->sinks.hold);
TIMER_TOC(timer_locktree);
}
/**
* @brief Unlock a cell's parents for access to #bpart array.
*
......
......@@ -764,6 +764,12 @@ struct cell {
/*! Nr of #sink this cell can hold after addition of new one. */
int count_total;
/*! Is the #sink data of this cell being used in a sub-cell? */
int hold;
/*! Spin lock for various uses (#sink case). */
swift_lock_type lock;
/*! Last (integer) time the cell's sink were drifted forward in time. */
integertime_t ti_old_part;
......@@ -883,6 +889,8 @@ int cell_mlocktree(struct cell *c);
void cell_munlocktree(struct cell *c);
int cell_slocktree(struct cell *c);
void cell_sunlocktree(struct cell *c);
int cell_sink_locktree(struct cell *c);
void cell_sink_unlocktree(struct cell *c);
int cell_blocktree(struct cell *c);
void cell_bunlocktree(struct cell *c);
int cell_pack(struct cell *c, struct pcell *pc, const int with_gravity);
......
......@@ -595,6 +595,8 @@ void space_regrid(struct space *s, int verbose) {
error("Failed to init spinlock for star formation (gpart).");
if (lock_init(&s->cells_top[k].stars.lock) != 0)
error("Failed to init spinlock for stars.");
if (lock_init(&s->cells_top[k].sinks.lock) != 0)
error("Failed to init spinlock for sinks.");
if (lock_init(&s->cells_top[k].black_holes.lock) != 0)
error("Failed to init spinlock for black holes.");
if (lock_init(&s->cells_top[k].stars.star_formation_lock) != 0)
......@@ -4346,6 +4348,7 @@ void space_recycle(struct space *s, struct cell *c) {
/* Clear the cell. */
if (lock_destroy(&c->hydro.lock) != 0 || lock_destroy(&c->grav.plock) != 0 ||
lock_destroy(&c->grav.mlock) != 0 || lock_destroy(&c->stars.lock) != 0 ||
lock_destroy(&c->sinks.lock) != 0 ||
lock_destroy(&c->black_holes.lock) != 0 ||
lock_destroy(&c->grav.star_formation_lock) != 0 ||
lock_destroy(&c->stars.star_formation_lock) != 0)
......@@ -4400,6 +4403,7 @@ void space_recycle_list(struct space *s, struct cell *cell_list_begin,
lock_destroy(&c->grav.plock) != 0 ||
lock_destroy(&c->grav.mlock) != 0 ||
lock_destroy(&c->stars.lock) != 0 ||
lock_destroy(&c->sinks.lock) != 0 ||
lock_destroy(&c->black_holes.lock) != 0 ||
lock_destroy(&c->stars.star_formation_lock) != 0 ||
lock_destroy(&c->grav.star_formation_lock) != 0)
......@@ -4502,6 +4506,7 @@ void space_getcells(struct space *s, int nr_cells, struct cell **cells) {
lock_init(&cells[j]->grav.plock) != 0 ||
lock_init(&cells[j]->grav.mlock) != 0 ||
lock_init(&cells[j]->stars.lock) != 0 ||
lock_init(&cells[j]->sinks.lock) != 0 ||
lock_init(&cells[j]->black_holes.lock) != 0 ||
lock_init(&cells[j]->stars.star_formation_lock) != 0 ||
lock_init(&cells[j]->grav.star_formation_lock) != 0)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment