atrstm

Load and structure a combustion gas mixture
git clone git://git.meso-star.fr/atrstm.git
Log | Files | Refs | README | LICENSE

commit f04809e45d10a6a19f29e5fe5ce2726eb4db841b
parent 34fa6b6bda7d78fe9d4d7f6c55f7fad42d698974
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Thu, 14 Jan 2021 18:20:21 +0100

Fix pool issues

Diffstat:
Msrc/atrstm_partition.c | 40+++++++++++++++++++++++-----------------
Msrc/atrstm_partition.h | 15+++++++++++++++
2 files changed, 38 insertions(+), 17 deletions(-)

diff --git a/src/atrstm_partition.c b/src/atrstm_partition.c @@ -13,6 +13,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "atrstm_log.h" #include "atrstm_partition.h" #include <rsys/condition.h> @@ -40,9 +41,9 @@ pool_init(struct mem_allocator* mem_allocator, struct pool* pool) pool->mutex = mutex_create(); if(!pool->mutex) { res = RES_UNKNOWN_ERR; goto error; } pool->cond_new = cond_create(); - if(pool->cond_new) { res = RES_UNKNOWN_ERR; goto error; } + if(!pool->cond_new) { res = RES_UNKNOWN_ERR; goto error; } pool->cond_fetch = cond_create(); - if(pool->cond_fetch) { res = RES_UNKNOWN_ERR; goto error; } + if(!pool->cond_fetch) { res = RES_UNKNOWN_ERR; goto error; } FOR_EACH(ipart, 0, POOL_MAX_NPARTITIONS) { struct part* part = MEM_ALLOC_ALIGNED(pool->allocator, sizeof(*part), 64); @@ -50,13 +51,14 @@ pool_init(struct mem_allocator* mem_allocator, struct pool* pool) res = RES_MEM_ERR; goto error; } + part_init(part); list_add(&pool->parts_free, &part->node); } exit: - pool_release(pool); return res; error: + pool_release(pool); goto exit; } @@ -71,21 +73,25 @@ pool_release(struct pool* pool) if(pool->cond_new) cond_destroy(pool->cond_new); if(pool->cond_fetch) cond_destroy(pool->cond_fetch); - LIST_FOR_EACH_SAFE(node, tmp_node, &pool->parts_free) { - struct part* part = CONTAINER_OF(node, struct part, node); - list_del(node); - MEM_RM(pool->allocator, part); + if(pool->parts_free.next != NULL) { /* Is list initialised */ + LIST_FOR_EACH_SAFE(node, tmp_node, &pool->parts_free) { + struct part* part = CONTAINER_OF(node, struct part, node); + list_del(node); + part_release(part); + MEM_RM(pool->allocator, part); + } + ASSERT(is_list_empty(&pool->parts_free)); } - LIST_FOR_EACH_SAFE(node, tmp_node, &pool->parts_full) { - struct part* part = CONTAINER_OF(node, struct part, node); - list_del(node); - MEM_RM(pool->allocator, part); + if(pool->parts_full.next != NULL) { /* Is list initialised */ + LIST_FOR_EACH_SAFE(node, tmp_node, &pool->parts_full) { + struct part* part = CONTAINER_OF(node, struct part, node); + list_del(node); + MEM_RM(pool->allocator, part); + } + ASSERT(is_list_empty(&pool->parts_full)); } - ASSERT(is_list_empty(&pool->parts_free)); - ASSERT(is_list_empty(&pool->parts_full)); - memset(pool, 0, sizeof(*pool)); } @@ -117,10 +123,10 @@ pool_new_partition(struct pool* pool, const size_t ipart) void pool_free_partition(struct pool* pool, struct part* part) { - ASSERT(pool && part && is_list_empty(&part->node)); + ASSERT(pool && part); mutex_lock(pool->mutex); - list_add_tail(&pool->parts_free, &part->node); + list_move_tail(&part->node, &pool->parts_free); mutex_unlock(pool->mutex); cond_signal(pool->cond_new); @@ -168,7 +174,7 @@ pool_fetch_partition(struct pool* pool, size_t part_id) if(!found_part) { cond_wait(pool->cond_fetch, pool->mutex); } else { - list_del(&found_part->node); + /*list_del(&found_part->node);*/ break; } } diff --git a/src/atrstm_partition.h b/src/atrstm_partition.h @@ -59,6 +59,21 @@ struct pool { ATOMIC error; }; +static INLINE void +part_init(struct part* part) +{ + ASSERT(part); + list_init(&part->node); + part->id = SIZE_MAX; +} + +static INLINE void +part_release(struct part* part) +{ + ASSERT(part && is_list_empty(&part->node)); + (void)part; /* Do nothing */ +} + static FINLINE float* part_get_voxel (struct part* part,