2024-04-23 23:45:43 +03:00
|
|
|
#include <bh/internal/thread.h>
|
|
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
static unsigned __stdcall bh_thread_run(void *arg)
|
|
|
|
|
{
|
|
|
|
|
bh_thread_data_t data;
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
/* Fetch thread data, store it on stack and free from heap */
|
2024-04-27 11:25:21 +03:00
|
|
|
data = *(bh_thread_data_t *)arg;
|
|
|
|
|
free(arg);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
/* Do the task, mark as done, and if required free it */
|
2024-04-27 11:25:21 +03:00
|
|
|
data.task->func(data.task->data);
|
|
|
|
|
data.task->flags |= BH_THREAD_DONE;
|
|
|
|
|
|
|
|
|
|
if (data.task->flags & BH_THREAD_CLEANUP)
|
|
|
|
|
bh_task_free(data.task);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
/* Call thread specific end function (deallocate TLS) */
|
2024-04-27 11:25:21 +03:00
|
|
|
data.end(0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-23 23:45:43 +03:00
|
|
|
int bh_thread_init_base(bh_thread_t *thread,
|
|
|
|
|
bh_task_t *task,
|
|
|
|
|
bh_thread_begin_cb_t begin,
|
|
|
|
|
bh_thread_end_cb_t end)
|
|
|
|
|
{
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_thread_data_t *data;
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
/* Allocate thread specific data */
|
2024-04-27 11:25:21 +03:00
|
|
|
data = malloc(sizeof(*data));
|
|
|
|
|
if (!data)
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_OOM;
|
|
|
|
|
|
|
|
|
|
/* Setup thread specific data */
|
2024-04-27 11:25:21 +03:00
|
|
|
data->task = task;
|
|
|
|
|
data->end = end;
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
/* Create and setup thread (relative to the callers libc) */
|
2024-04-27 11:25:21 +03:00
|
|
|
thread->allocated = 0;
|
|
|
|
|
thread->handle = (HANDLE)_beginthreadex(NULL, 0, bh_thread_run, data, 0, NULL);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
/* Check for errors */
|
2024-04-27 11:25:21 +03:00
|
|
|
if (!thread->handle)
|
|
|
|
|
{
|
|
|
|
|
free(data);
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_ERROR;
|
2024-04-27 11:25:21 +03:00
|
|
|
}
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
return BH_OK;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bh_thread_t *bh_thread_new_base(bh_task_t *task,
|
|
|
|
|
bh_thread_begin_cb_t begin,
|
|
|
|
|
bh_thread_end_cb_t end)
|
|
|
|
|
{
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_thread_t *result;
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
/* Allocate thread object */
|
2024-04-27 11:25:21 +03:00
|
|
|
result = malloc(sizeof(*result));
|
|
|
|
|
if (result && !bh_thread_init_base(result, task, begin, end))
|
|
|
|
|
{
|
|
|
|
|
free(result);
|
|
|
|
|
result = NULL;
|
|
|
|
|
}
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
/* Mark thread as allocated for deallocation in join/detach */
|
2024-04-27 11:25:21 +03:00
|
|
|
if (result)
|
|
|
|
|
result->allocated = 1;
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
return result;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_thread_join(bh_thread_t *thread)
|
|
|
|
|
{
|
2024-06-03 22:11:05 +03:00
|
|
|
/* Join the thread */
|
2024-04-27 11:25:21 +03:00
|
|
|
WaitForSingleObject(thread->handle, INFINITE);
|
|
|
|
|
CloseHandle(thread->handle);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
/* If thread is allocated, deallocate it */
|
2024-04-27 11:25:21 +03:00
|
|
|
if (thread->allocated)
|
|
|
|
|
free(thread);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
return BH_OK;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_thread_detach(bh_thread_t *thread)
|
|
|
|
|
{
|
2024-06-03 22:11:05 +03:00
|
|
|
/* Detach from thread */
|
2024-04-27 11:25:21 +03:00
|
|
|
CloseHandle(thread->handle);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
/* If thread is allocated, deallocate it */
|
2024-04-27 11:25:21 +03:00
|
|
|
if (thread->allocated)
|
|
|
|
|
free(thread);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
return BH_OK;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_mutex_init(bh_mutex_t *mutex)
|
|
|
|
|
{
|
2024-06-03 22:11:05 +03:00
|
|
|
/* TODO: Is this spincount needed or sane? */
|
2024-04-27 11:25:21 +03:00
|
|
|
if (!InitializeCriticalSectionAndSpinCount(&mutex->handle, 0x400))
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_ERROR;
|
|
|
|
|
return BH_OK;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bh_mutex_destroy(bh_mutex_t *mutex)
|
|
|
|
|
{
|
2024-04-27 11:25:21 +03:00
|
|
|
DeleteCriticalSection(&mutex->handle);
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_mutex_lock(bh_mutex_t *mutex)
|
|
|
|
|
{
|
2024-04-27 11:25:21 +03:00
|
|
|
EnterCriticalSection(&mutex->handle);
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_OK;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_mutex_try_lock(bh_mutex_t *mutex)
|
|
|
|
|
{
|
2024-04-27 11:25:21 +03:00
|
|
|
if (!TryEnterCriticalSection(&mutex->handle))
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_ERROR;
|
|
|
|
|
return BH_OK;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_mutex_unlock(bh_mutex_t *mutex)
|
|
|
|
|
{
|
2024-04-27 11:25:21 +03:00
|
|
|
LeaveCriticalSection(&mutex->handle);
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_OK;
|
2024-04-27 11:25:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_semaphore_init(bh_semaphore_t *semaphore, int count)
|
|
|
|
|
{
|
2024-06-03 22:11:05 +03:00
|
|
|
/* Create semaphore with max value of 32767 (to match POSIX) */
|
2024-04-27 11:25:21 +03:00
|
|
|
semaphore->handle = CreateSemaphore(NULL, count, 0x7FFF, NULL);
|
|
|
|
|
if (!semaphore->handle)
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_ERROR;
|
|
|
|
|
|
|
|
|
|
return BH_OK;
|
2024-04-27 11:25:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bh_semaphore_destroy(bh_semaphore_t *semaphore)
|
|
|
|
|
{
|
|
|
|
|
CloseHandle(semaphore->handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_semaphore_post(bh_semaphore_t *semaphore)
|
|
|
|
|
{
|
|
|
|
|
if (!ReleaseSemaphore(semaphore->handle, 1, NULL))
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_ERROR;
|
|
|
|
|
return BH_OK;
|
2024-04-27 11:25:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_semaphore_wait(bh_semaphore_t *semaphore)
|
|
|
|
|
{
|
|
|
|
|
if (WaitForSingleObject(semaphore->handle, INFINITE))
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_ERROR;
|
|
|
|
|
return BH_OK;
|
2024-04-27 11:25:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_semaphore_wait_for(bh_semaphore_t *semaphore,
|
|
|
|
|
unsigned long timeout)
|
|
|
|
|
{
|
2024-06-03 22:11:05 +03:00
|
|
|
/* FIXME: Check if we timed out or errored out */
|
2024-04-27 11:25:21 +03:00
|
|
|
if (WaitForSingleObject(semaphore->handle, timeout))
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_TIMEOUT;
|
|
|
|
|
return BH_ERROR;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
int bh_semaphore_try_wait(bh_semaphore_t *semaphore)
|
|
|
|
|
{
|
|
|
|
|
if (WaitForSingleObject(semaphore->handle, 0))
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_ERROR;
|
|
|
|
|
return BH_OK;
|
2024-04-27 11:25:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if WINVER >= _WIN32_WINNT_VISTA
|
2024-04-23 23:45:43 +03:00
|
|
|
int bh_cond_init(bh_cond_t *cond)
|
|
|
|
|
{
|
2024-04-27 11:25:21 +03:00
|
|
|
InitializeConditionVariable(&cond->handle);
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_OK;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bh_cond_destroy(bh_cond_t *cond)
|
|
|
|
|
{
|
|
|
|
|
(void)cond;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_cond_wait(bh_cond_t *cond,
|
|
|
|
|
bh_mutex_t *mutex)
|
|
|
|
|
{
|
2024-04-27 11:25:21 +03:00
|
|
|
return bh_cond_wait_for(cond, mutex, INFINITE);
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_cond_wait_for(bh_cond_t *cond,
|
|
|
|
|
bh_mutex_t *mutex,
|
|
|
|
|
unsigned long timeout)
|
|
|
|
|
{
|
2024-06-03 22:11:05 +03:00
|
|
|
/* FIXME: Check if we timed out or errored out */
|
2024-04-27 11:25:21 +03:00
|
|
|
if (!SleepConditionVariableCS(&cond->handle, &mutex->handle, timeout))
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_TIMEOUT;
|
|
|
|
|
return BH_OK;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_cond_signal(bh_cond_t *cond)
|
|
|
|
|
{
|
2024-04-27 11:25:21 +03:00
|
|
|
WakeConditionVariable(&cond->handle);
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_OK;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_cond_broadcast(bh_cond_t *cond)
|
|
|
|
|
{
|
2024-04-27 11:25:21 +03:00
|
|
|
WakeAllConditionVariable(&cond->handle);
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_OK;
|
2024-04-27 11:25:21 +03:00
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
/* Condition variable implementation based on BeOS article
|
|
|
|
|
* http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html
|
2024-06-03 22:11:05 +03:00
|
|
|
*
|
|
|
|
|
* Slow, but correct implementation of CVs.
|
|
|
|
|
*/
|
2024-04-27 11:25:21 +03:00
|
|
|
|
|
|
|
|
int bh_cond_init(bh_cond_t *cond)
|
|
|
|
|
{
|
|
|
|
|
if (bh_mutex_init(&cond->lock))
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_ERROR;
|
|
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
if (bh_semaphore_init(&cond->wait, 0))
|
|
|
|
|
{
|
|
|
|
|
bh_mutex_destroy(&cond->lock);
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_ERROR;
|
2024-04-27 11:25:21 +03:00
|
|
|
}
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
if (bh_semaphore_init(&cond->done, 0))
|
|
|
|
|
{
|
|
|
|
|
bh_semaphore_destroy(&cond->wait);
|
|
|
|
|
bh_mutex_destroy(&cond->lock);
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_ERROR;
|
2024-04-27 11:25:21 +03:00
|
|
|
}
|
|
|
|
|
cond->waiting = 0;
|
|
|
|
|
cond->signals = 0;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bh_cond_destroy(bh_cond_t *cond)
|
|
|
|
|
{
|
|
|
|
|
bh_semaphore_destroy(&cond->done);
|
|
|
|
|
bh_semaphore_destroy(&cond->wait);
|
|
|
|
|
bh_mutex_destroy(&cond->lock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_cond_wait(bh_cond_t *cond,
|
|
|
|
|
bh_mutex_t *mutex)
|
|
|
|
|
{
|
|
|
|
|
int retval;
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_mutex_lock(&cond->lock);
|
|
|
|
|
cond->waiting++;
|
|
|
|
|
bh_mutex_unlock(&cond->lock);
|
|
|
|
|
bh_mutex_unlock(mutex);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
retval = bh_semaphore_wait(&cond->wait);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_mutex_lock(&cond->lock);
|
|
|
|
|
if (cond->signals > 0)
|
|
|
|
|
{
|
|
|
|
|
if (retval)
|
|
|
|
|
bh_semaphore_wait(&cond->wait);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_semaphore_post(&cond->done);
|
|
|
|
|
cond->signals--;
|
|
|
|
|
}
|
|
|
|
|
cond->waiting--;
|
2024-06-03 22:11:05 +03:00
|
|
|
bh_mutex_unlock(&cond->lock);
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_mutex_lock(mutex);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_cond_wait_for(bh_cond_t *cond,
|
|
|
|
|
bh_mutex_t *mutex,
|
|
|
|
|
unsigned long timeout)
|
|
|
|
|
{
|
|
|
|
|
int retval;
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_mutex_lock(&cond->lock);
|
|
|
|
|
cond->waiting++;
|
|
|
|
|
bh_mutex_unlock(&cond->lock);
|
|
|
|
|
bh_mutex_unlock(mutex);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
retval = bh_semaphore_wait_for(&cond->wait, timeout);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_mutex_lock(&cond->lock);
|
|
|
|
|
if (cond->signals > 0)
|
|
|
|
|
{
|
|
|
|
|
if (retval)
|
|
|
|
|
bh_semaphore_wait(&cond->wait);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_semaphore_post(&cond->done);
|
|
|
|
|
cond->signals--;
|
|
|
|
|
}
|
|
|
|
|
cond->waiting--;
|
2024-06-03 22:11:05 +03:00
|
|
|
bh_mutex_unlock(&cond->lock);
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_mutex_lock(mutex);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int bh_cond_signal(bh_cond_t *cond)
|
|
|
|
|
{
|
|
|
|
|
bh_mutex_lock(&cond->lock);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
if (cond->waiting > cond->signals)
|
|
|
|
|
{
|
|
|
|
|
cond->signals++;
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_semaphore_post(&cond->wait);
|
|
|
|
|
bh_mutex_unlock(&cond->lock);
|
|
|
|
|
bh_semaphore_wait(&cond->done);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
bh_mutex_unlock(&cond->lock);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
return BH_OK;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
int bh_cond_broadcast(bh_cond_t *cond)
|
|
|
|
|
{
|
|
|
|
|
int i, waiting;
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_mutex_lock(&cond->lock);
|
|
|
|
|
if (cond->waiting > cond->signals)
|
|
|
|
|
{
|
|
|
|
|
waiting = cond->waiting - cond->signals;
|
|
|
|
|
cond->signals = cond->waiting;
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
for (i = 0; i < waiting; i++)
|
|
|
|
|
bh_semaphore_post(&cond->wait);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_mutex_unlock(&cond->lock);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
for (i = 0; i < waiting; i++)
|
|
|
|
|
bh_semaphore_wait(&cond->done);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
bh_mutex_unlock(&cond->lock);
|
2024-06-03 22:11:05 +03:00
|
|
|
|
|
|
|
|
return BH_OK;
|
2024-04-27 11:25:21 +03:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-04-23 23:45:43 +03:00
|
|
|
int bh_thread_pool_init_base(bh_thread_pool_t *pool,
|
|
|
|
|
size_t size,
|
|
|
|
|
bh_thread_begin_cb_t begin,
|
|
|
|
|
bh_thread_end_cb_t end)
|
|
|
|
|
{
|
2024-04-27 11:25:21 +03:00
|
|
|
size_t i;
|
|
|
|
|
static bh_task_t pool_task;
|
|
|
|
|
static int pool_task_init = 0;
|
|
|
|
|
|
|
|
|
|
/* Initialize static thread pool task */
|
|
|
|
|
if (!pool_task_init)
|
|
|
|
|
{
|
|
|
|
|
bh_task_init(&pool_task, bh_thread_pool_worker, pool, 0);
|
|
|
|
|
pool_task_init = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Zero out pool structure */
|
|
|
|
|
memset(pool, 0, sizeof(*pool));
|
|
|
|
|
|
|
|
|
|
/* Initialize mutex and condition variables */
|
|
|
|
|
if (bh_mutex_init(&pool->lock))
|
|
|
|
|
goto lock_fail;
|
|
|
|
|
|
|
|
|
|
if (bh_cond_init(&pool->new_task))
|
|
|
|
|
goto task_fail;
|
|
|
|
|
|
|
|
|
|
if (bh_cond_init(&pool->done_task))
|
|
|
|
|
goto done_fail;
|
|
|
|
|
|
|
|
|
|
/* Allocate array of threads */
|
|
|
|
|
pool->threads = malloc(size * sizeof(bh_thread_t));
|
|
|
|
|
pool->size = size;
|
|
|
|
|
|
|
|
|
|
if (!pool->threads)
|
|
|
|
|
goto thread_fail;
|
|
|
|
|
|
|
|
|
|
/* Initialize queue */
|
|
|
|
|
bh_queue_init(&pool->tasks);
|
|
|
|
|
if (bh_queue_reserve(&pool->tasks, 64))
|
|
|
|
|
goto queue_fail;
|
|
|
|
|
|
|
|
|
|
/* Initialize threads */
|
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
|
{
|
|
|
|
|
if (bh_thread_init_base(&pool->threads[i], &pool_task, begin, end))
|
|
|
|
|
{
|
|
|
|
|
pool->shutdown = 1;
|
|
|
|
|
bh_cond_broadcast(&pool->new_task);
|
|
|
|
|
for(; i; i--)
|
|
|
|
|
bh_thread_join(&pool->threads[i - 1]);
|
|
|
|
|
|
|
|
|
|
bh_queue_destroy(&pool->tasks);
|
|
|
|
|
goto queue_fail;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_OK;
|
2024-04-27 11:25:21 +03:00
|
|
|
|
|
|
|
|
queue_fail:
|
|
|
|
|
free(pool->threads);
|
|
|
|
|
|
|
|
|
|
thread_fail:
|
|
|
|
|
bh_cond_destroy(&pool->done_task);
|
|
|
|
|
|
|
|
|
|
done_fail:
|
|
|
|
|
bh_cond_destroy(&pool->new_task);
|
|
|
|
|
|
|
|
|
|
task_fail:
|
|
|
|
|
bh_mutex_destroy(&pool->lock);
|
|
|
|
|
|
|
|
|
|
lock_fail:
|
2024-06-03 22:11:05 +03:00
|
|
|
return BH_ERROR;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bh_thread_pool_t *bh_thread_pool_new_base(size_t size,
|
|
|
|
|
bh_thread_begin_cb_t begin,
|
|
|
|
|
bh_thread_end_cb_t end)
|
|
|
|
|
{
|
2024-04-27 11:25:21 +03:00
|
|
|
bh_thread_pool_t *result;
|
2024-06-03 22:11:05 +03:00
|
|
|
|
2024-04-27 11:25:21 +03:00
|
|
|
result = malloc(sizeof(*result));
|
|
|
|
|
if (result && bh_thread_pool_init_base(result, size, begin, end))
|
|
|
|
|
{
|
|
|
|
|
free(result);
|
|
|
|
|
result = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2024-04-23 23:45:43 +03:00
|
|
|
}
|
|
|
|
|
|