machinarium: free coroutine on cache limit reach

This commit is contained in:
Dmitry Simonenko 2017-11-14 16:17:39 +03:00
parent 8136104e31
commit 65b65fcad7
2 changed files with 8 additions and 1 deletions

View File

@ -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);

View File

@ -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;
}