(async_job/rsemaphore) Cleanups

This commit is contained in:
twinaphex 2016-01-13 08:58:38 +01:00
parent 028cfaa048
commit b5b8886894
2 changed files with 59 additions and 41 deletions

View File

@ -47,7 +47,7 @@ struct async_job
static void async_job_processor(void *userdata)
{
async_job_node_t *node;
async_job_node_t *node = NULL;
async_job_t *ajob = (async_job_t*)userdata;
for (;;)
@ -73,39 +73,47 @@ async_job_t *async_job_new(void)
{
async_job_t *ajob = (async_job_t*)calloc(1, sizeof(*ajob));
if (!ajob)
return NULL;
ajob->lock = slock_new();
if (!ajob->lock)
goto error;
ajob->sem = ssem_new(0);
if (!ajob->sem)
goto error;
ajob->thread = sthread_create(async_job_processor, (void*)ajob);
if (!ajob->thread)
goto error;
return ajob;
error:
if (ajob->lock)
slock_free(ajob->lock);
ajob->lock = NULL;
if (ajob->sem)
ssem_free(ajob->sem);
if (ajob)
{
ajob->lock = slock_new();
if (ajob->lock)
{
ajob->sem = ssem_new(0);
if (ajob->sem)
{
ajob->thread = sthread_create(async_job_processor, (void*)ajob);
if (ajob->thread)
return ajob;
ssem_free(ajob->sem);
}
slock_free(ajob->lock);
}
free((void*)ajob);
}
return NULL;
}
void async_job_free(async_job_t *ajob)
{
if (!ajob)
return;
ajob->finish = 1;
ssem_signal(ajob->sem);
sthread_join(ajob->thread);
ssem_free(ajob->sem);
free((void*)ajob);
}

View File

@ -39,27 +39,31 @@ struct ssem
ssem_t *ssem_new(int value)
{
ssem_t *semaphore = (ssem_t*)malloc(sizeof(*semaphore));
ssem_t *semaphore = (ssem_t*)calloc(1, sizeof(*semaphore));
if (!semaphore)
goto error;
semaphore->value = value;
semaphore->wakeups = 0;
semaphore->mutex = slock_new();
if (!semaphore->mutex)
goto error;
semaphore->cond = scond_new();
if (!semaphore->cond)
goto error;
return semaphore;
error:
if (semaphore->mutex)
slock_free(semaphore->mutex);
semaphore->mutex = NULL;
if (semaphore)
{
semaphore->value = value;
semaphore->wakeups = 0;
semaphore->mutex = slock_new();
if (semaphore->mutex)
{
semaphore->cond = scond_new();
if (semaphore->cond)
return semaphore;
slock_free(semaphore->mutex);
}
free((void*)semaphore);
}
return NULL;
}
@ -75,6 +79,9 @@ void ssem_free(ssem_t *semaphore)
void ssem_wait(ssem_t *semaphore)
{
if (!semaphore)
return;
slock_lock(semaphore->mutex);
semaphore->value--;
@ -93,6 +100,9 @@ void ssem_wait(ssem_t *semaphore)
void ssem_signal(ssem_t *semaphore)
{
if (!semaphore)
return;
slock_lock(semaphore->mutex);
semaphore->value++;