From 65b65fcad78ff9c97ee524616556930b979f2342 Mon Sep 17 00:00:00 2001 From: Dmitry Simonenko Date: Tue, 14 Nov 2017 16:17:39 +0300 Subject: [PATCH] machinarium: free coroutine on cache limit reach --- sources/coroutine_cache.c | 5 +++++ sources/machine.c | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/sources/coroutine_cache.c b/sources/coroutine_cache.c index ebf0214c..7ee5da56 100644 --- a/sources/coroutine_cache.c +++ b/sources/coroutine_cache.c @@ -49,6 +49,11 @@ void mm_coroutine_cache_push(mm_coroutine_cache_t *cache, mm_coroutine_t *corout { assert(coroutine->state == MM_CFREE); pthread_spin_lock(&cache->lock); + if (cache->count > cache->limit) { + pthread_spin_unlock(&cache->lock); + mm_coroutine_free(coroutine); + return; + } mm_list_append(&cache->list, &coroutine->link); cache->count++; pthread_spin_unlock(&cache->lock); diff --git a/sources/machine.c b/sources/machine.c index dfef8a11..3a338f90 100644 --- a/sources/machine.c +++ b/sources/machine.c @@ -153,8 +153,10 @@ machine_coroutine_create(machine_coroutine_t function, void *arg) mm_errno_set(0); mm_coroutine_t *coroutine; coroutine = mm_coroutine_cache_pop(&machinarium.coroutine_cache); - if (coroutine == NULL) + if (coroutine == NULL) { + mm_errno_set(ENOMEM); return -1; + } mm_scheduler_new(&mm_self->scheduler, coroutine, function, arg); return coroutine->id; }