Add documentation, expand error handling, implement file and buffer io
This commit is contained in:
@@ -5,9 +5,9 @@
|
||||
static void *bh_thread_run(void *arg)
|
||||
{
|
||||
bh_task_t *task;
|
||||
|
||||
task = (bh_task_t *)arg;
|
||||
|
||||
/* Do the task, mark as done, and if required free it */
|
||||
task->func(task->data);
|
||||
task->flags |= BH_THREAD_DONE;
|
||||
|
||||
@@ -21,21 +21,25 @@ int bh_thread_init(bh_thread_t *thread,
|
||||
bh_task_t *task)
|
||||
{
|
||||
thread->allocated = 0;
|
||||
return pthread_create(&thread->handle, NULL, bh_thread_run, task);
|
||||
if (pthread_create(&thread->handle, NULL, bh_thread_run, task))
|
||||
return BH_ERROR;
|
||||
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
bh_thread_t *bh_thread_new(bh_task_t *task)
|
||||
{
|
||||
bh_thread_t *result;
|
||||
|
||||
/* Allocate thread object */
|
||||
result = malloc(sizeof(*result));
|
||||
|
||||
if (result && bh_thread_init(result, task))
|
||||
{
|
||||
free(result);
|
||||
result = NULL;
|
||||
}
|
||||
|
||||
/* Mark thread as allocated for deallocation in join/detach */
|
||||
if (result)
|
||||
result->allocated = 1;
|
||||
|
||||
@@ -44,27 +48,35 @@ bh_thread_t *bh_thread_new(bh_task_t *task)
|
||||
|
||||
int bh_thread_join(bh_thread_t *thread)
|
||||
{
|
||||
int result = pthread_join(thread->handle, NULL);
|
||||
/* Join the thread */
|
||||
if (pthread_join(thread->handle, NULL))
|
||||
return BH_ERROR;
|
||||
|
||||
/* If thread is allocated, deallocate it */
|
||||
if (thread->allocated)
|
||||
free(thread);
|
||||
|
||||
return result;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
int bh_thread_detach(bh_thread_t *thread)
|
||||
{
|
||||
int result = pthread_detach(thread->handle);
|
||||
/* Detach the thread */
|
||||
if (pthread_detach(thread->handle))
|
||||
return BH_ERROR;
|
||||
|
||||
/* If thread is allocated, deallocate it */
|
||||
if (thread->allocated)
|
||||
free(thread);
|
||||
|
||||
return result;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
int bh_mutex_init(bh_mutex_t *mutex)
|
||||
{
|
||||
return pthread_mutex_init(&mutex->handle, NULL);
|
||||
if (pthread_mutex_init(&mutex->handle, NULL))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
void bh_mutex_destroy(bh_mutex_t *mutex)
|
||||
@@ -74,22 +86,30 @@ void bh_mutex_destroy(bh_mutex_t *mutex)
|
||||
|
||||
int bh_mutex_lock(bh_mutex_t *mutex)
|
||||
{
|
||||
return pthread_mutex_lock(&mutex->handle);
|
||||
if (pthread_mutex_lock(&mutex->handle))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
int bh_mutex_try_lock(bh_mutex_t *mutex)
|
||||
{
|
||||
return pthread_mutex_trylock(&mutex->handle);
|
||||
if (pthread_mutex_trylock(&mutex->handle))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
int bh_mutex_unlock(bh_mutex_t *mutex)
|
||||
{
|
||||
return pthread_mutex_unlock(&mutex->handle);
|
||||
if (pthread_mutex_unlock(&mutex->handle))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
int bh_semaphore_init(bh_semaphore_t *semaphore, int count)
|
||||
{
|
||||
return sem_init(&semaphore->handle, 0, count);
|
||||
if (sem_init(&semaphore->handle, 0, count))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
void bh_semaphore_destroy(bh_semaphore_t *semaphore)
|
||||
@@ -99,12 +119,16 @@ void bh_semaphore_destroy(bh_semaphore_t *semaphore)
|
||||
|
||||
int bh_semaphore_post(bh_semaphore_t *semaphore)
|
||||
{
|
||||
return sem_post(&semaphore->handle);
|
||||
if (sem_post(&semaphore->handle))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
int bh_semaphore_wait(bh_semaphore_t *semaphore)
|
||||
{
|
||||
return sem_wait(&semaphore->handle);
|
||||
if (sem_wait(&semaphore->handle))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
int bh_semaphore_wait_for(bh_semaphore_t *semaphore,
|
||||
@@ -115,17 +139,23 @@ int bh_semaphore_wait_for(bh_semaphore_t *semaphore,
|
||||
ts.tv_sec = timeout / 1000;
|
||||
ts.tv_nsec = (timeout - ts.tv_sec * 1000) * 1000000;
|
||||
|
||||
return sem_timedwait(&semaphore->handle, &ts);
|
||||
if (sem_timedwait(&semaphore->handle, &ts))
|
||||
return BH_TIMEOUT;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
int bh_semaphore_try_wait(bh_semaphore_t *semaphore)
|
||||
{
|
||||
return sem_trywait(&semaphore->handle);
|
||||
if (sem_trywait(&semaphore->handle))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
int bh_cond_init(bh_cond_t *cond)
|
||||
{
|
||||
return pthread_cond_init(&cond->handle, NULL);
|
||||
if (pthread_cond_init(&cond->handle, NULL))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
void bh_cond_destroy(bh_cond_t *cond)
|
||||
@@ -136,7 +166,9 @@ void bh_cond_destroy(bh_cond_t *cond)
|
||||
int bh_cond_wait(bh_cond_t *cond,
|
||||
bh_mutex_t *mutex)
|
||||
{
|
||||
return pthread_cond_wait(&cond->handle, &mutex->handle);
|
||||
if (pthread_cond_wait(&cond->handle, &mutex->handle))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
int bh_cond_wait_for(bh_cond_t *cond,
|
||||
@@ -148,17 +180,23 @@ int bh_cond_wait_for(bh_cond_t *cond,
|
||||
ts.tv_sec = timeout / 1000;
|
||||
ts.tv_nsec = (timeout - ts.tv_sec * 1000) * 1000000;
|
||||
|
||||
return pthread_cond_timedwait(&cond->handle, &mutex->handle, &ts);
|
||||
if (pthread_cond_timedwait(&cond->handle, &mutex->handle, &ts))
|
||||
return BH_TIMEOUT;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
int bh_cond_signal(bh_cond_t *cond)
|
||||
{
|
||||
return pthread_cond_signal(&cond->handle);
|
||||
if (pthread_cond_signal(&cond->handle))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
int bh_cond_broadcast(bh_cond_t *cond)
|
||||
{
|
||||
return pthread_cond_broadcast(&cond->handle);
|
||||
if (pthread_cond_broadcast(&cond->handle))
|
||||
return BH_ERROR;
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
int bh_thread_pool_init(bh_thread_pool_t *pool,
|
||||
@@ -215,7 +253,7 @@ int bh_thread_pool_init(bh_thread_pool_t *pool,
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return BH_OK;
|
||||
|
||||
queue_fail:
|
||||
free(pool->threads);
|
||||
@@ -230,7 +268,7 @@ task_fail:
|
||||
bh_mutex_destroy(&pool->lock);
|
||||
|
||||
lock_fail:
|
||||
return -1;
|
||||
return BH_ERROR;
|
||||
}
|
||||
|
||||
bh_thread_pool_t *bh_thread_pool_new(size_t size)
|
||||
|
||||
Reference in New Issue
Block a user