util: Move pipe_semaphore to u_thread.h and rename it to util_semaphore

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
Reviewed-by: Jesse Natalie <jenatali@microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19557>
This commit is contained in:
Yonggang Luo
2022-11-06 19:53:40 +08:00
committed by Marge Bot
parent b732064f9e
commit 1129537e4c
4 changed files with 62 additions and 63 deletions

View File

@@ -61,54 +61,4 @@ __pipe_mutex_assert_locked(mtx_t *mutex)
#endif
}
/*
* Semaphores
*/
typedef struct
{
mtx_t mutex;
cnd_t cond;
int counter;
} pipe_semaphore;
static inline void
pipe_semaphore_init(pipe_semaphore *sema, int init_val)
{
(void) mtx_init(&sema->mutex, mtx_plain);
cnd_init(&sema->cond);
sema->counter = init_val;
}
static inline void
pipe_semaphore_destroy(pipe_semaphore *sema)
{
mtx_destroy(&sema->mutex);
cnd_destroy(&sema->cond);
}
/** Signal/increment semaphore counter */
static inline void
pipe_semaphore_signal(pipe_semaphore *sema)
{
mtx_lock(&sema->mutex);
sema->counter++;
cnd_signal(&sema->cond);
mtx_unlock(&sema->mutex);
}
/** Wait for semaphore counter to be greater than zero */
static inline void
pipe_semaphore_wait(pipe_semaphore *sema)
{
mtx_lock(&sema->mutex);
while (sema->counter <= 0) {
cnd_wait(&sema->cond, &sema->mutex);
}
sema->counter--;
mtx_unlock(&sema->mutex);
}
#endif /* OS_THREAD_H_ */

View File

@@ -1129,7 +1129,7 @@ lp_rast_queue_scene(struct lp_rasterizer *rast,
/* signal the threads that there's work to do */
for (i = 0; i < rast->num_threads; i++) {
pipe_semaphore_signal(&rast->tasks[i].work_ready);
util_semaphore_signal(&rast->tasks[i].work_ready);
}
}
@@ -1147,7 +1147,7 @@ lp_rast_finish(struct lp_rasterizer *rast)
/* wait for work to complete */
for (i = 0; i < rast->num_threads; i++) {
pipe_semaphore_wait(&rast->tasks[i].work_done);
util_semaphore_wait(&rast->tasks[i].work_done);
}
}
}
@@ -1181,7 +1181,7 @@ thread_function(void *init_data)
/* wait for work */
if (debug)
debug_printf("thread %d waiting for work\n", task->thread_index);
pipe_semaphore_wait(&task->work_ready);
util_semaphore_wait(&task->work_ready);
if (rast->exit_flag)
break;
@@ -1218,11 +1218,11 @@ thread_function(void *init_data)
if (debug)
debug_printf("thread %d done working\n", task->thread_index);
pipe_semaphore_signal(&task->work_done);
util_semaphore_signal(&task->work_done);
}
#ifdef _WIN32
pipe_semaphore_signal(&task->work_done);
util_semaphore_signal(&task->work_done);
#endif
return 0;
@@ -1237,8 +1237,8 @@ create_rast_threads(struct lp_rasterizer *rast)
{
/* NOTE: if num_threads is zero, we won't use any threads */
for (unsigned i = 0; i < rast->num_threads; i++) {
pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
pipe_semaphore_init(&rast->tasks[i].work_done, 0);
util_semaphore_init(&rast->tasks[i].work_ready, 0);
util_semaphore_init(&rast->tasks[i].work_done, 0);
if (thrd_success != u_thread_create(rast->threads + i, thread_function,
(void *) &rast->tasks[i])) {
rast->num_threads = i; /* previous thread is max */
@@ -1321,7 +1321,7 @@ lp_rast_destroy(struct lp_rasterizer *rast)
*/
rast->exit_flag = TRUE;
for (unsigned i = 0; i < rast->num_threads; i++) {
pipe_semaphore_signal(&rast->tasks[i].work_ready);
util_semaphore_signal(&rast->tasks[i].work_ready);
}
/* Wait for threads to terminate before cleaning up per-thread data.
@@ -1335,7 +1335,7 @@ lp_rast_destroy(struct lp_rasterizer *rast)
DWORD exit_code = STILL_ACTIVE;
if (GetExitCodeThread(rast->threads[i].handle, &exit_code) &&
exit_code == STILL_ACTIVE) {
pipe_semaphore_wait(&rast->tasks[i].work_done);
util_semaphore_wait(&rast->tasks[i].work_done);
}
#else
thrd_join(rast->threads[i], NULL);
@@ -1344,8 +1344,8 @@ lp_rast_destroy(struct lp_rasterizer *rast)
/* Clean up per-thread data */
for (unsigned i = 0; i < rast->num_threads; i++) {
pipe_semaphore_destroy(&rast->tasks[i].work_ready);
pipe_semaphore_destroy(&rast->tasks[i].work_done);
util_semaphore_destroy(&rast->tasks[i].work_ready);
util_semaphore_destroy(&rast->tasks[i].work_done);
}
for (unsigned i = 0; i < MAX2(1, rast->num_threads); i++) {
align_free(rast->tasks[i].thread_data.cache);

View File

@@ -100,8 +100,8 @@ struct lp_rasterizer_task
/** Non-interpolated passthru state and occlude counter for visible pixels */
struct lp_jit_thread_data thread_data;
pipe_semaphore work_ready;
pipe_semaphore work_done;
util_semaphore work_ready;
util_semaphore work_done;
};

View File

@@ -138,6 +138,55 @@ void util_barrier_destroy(util_barrier *barrier);
bool util_barrier_wait(util_barrier *barrier);
/*
* Semaphores
*/
typedef struct
{
mtx_t mutex;
cnd_t cond;
int counter;
} util_semaphore;
static inline void
util_semaphore_init(util_semaphore *sema, int init_val)
{
(void) mtx_init(&sema->mutex, mtx_plain);
cnd_init(&sema->cond);
sema->counter = init_val;
}
static inline void
util_semaphore_destroy(util_semaphore *sema)
{
mtx_destroy(&sema->mutex);
cnd_destroy(&sema->cond);
}
/** Signal/increment semaphore counter */
static inline void
util_semaphore_signal(util_semaphore *sema)
{
mtx_lock(&sema->mutex);
sema->counter++;
cnd_signal(&sema->cond);
mtx_unlock(&sema->mutex);
}
/** Wait for semaphore counter to be greater than zero */
static inline void
util_semaphore_wait(util_semaphore *sema)
{
mtx_lock(&sema->mutex);
while (sema->counter <= 0) {
cnd_wait(&sema->cond, &sema->mutex);
}
sema->counter--;
mtx_unlock(&sema->mutex);
}
#ifdef __cplusplus
}
#endif