util/c11: Update function u_thread_create to be c11 conformance

Do not assume thrd_t to be a pointer or integer, as the C11 standard tells us:
  thrd_t: implementation-defined complete object type identifying a thread
At https://en.cppreference.com/w/c/thread
So we always return the thread creation return code instead of thrd_t value, and judge the return
code properly.

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/15087>
This commit is contained in:
Yonggang Luo 2022-06-14 14:00:57 +08:00 committed by Marge Bot
parent 6e33ef2bb9
commit a9e2c699aa
12 changed files with 40 additions and 34 deletions

View File

@ -959,8 +959,7 @@ dd_context_create(struct dd_screen *dscreen, struct pipe_context *pipe)
list_inithead(&dctx->records); list_inithead(&dctx->records);
(void) mtx_init(&dctx->mutex, mtx_plain); (void) mtx_init(&dctx->mutex, mtx_plain);
(void) cnd_init(&dctx->cond); (void) cnd_init(&dctx->cond);
dctx->thread = u_thread_create(dd_thread_main, dctx); if (thrd_success != u_thread_create(&dctx->thread,dd_thread_main, dctx)) {
if (!dctx->thread) {
mtx_destroy(&dctx->mutex); mtx_destroy(&dctx->mutex);
goto fail; goto fail;
} }

View File

@ -841,7 +841,10 @@ rbug_start(struct rbug_screen *rb_screen)
tr_rbug->rb_screen = rb_screen; tr_rbug->rb_screen = rb_screen;
tr_rbug->running = true; tr_rbug->running = true;
tr_rbug->thread = u_thread_create(rbug_thread, tr_rbug); if (thrd_success != u_thread_create(&tr_rbug->thread, rbug_thread, tr_rbug)) {
FREE(tr_rbug);
return NULL;
}
return tr_rbug; return tr_rbug;
} }

View File

@ -96,9 +96,13 @@ lp_cs_tpool_create(unsigned num_threads)
list_inithead(&pool->workqueue); list_inithead(&pool->workqueue);
assert (num_threads <= LP_MAX_THREADS); assert (num_threads <= LP_MAX_THREADS);
for (unsigned i = 0; i < num_threads; i++) {
if (thrd_success != u_thread_create(pool->threads + i, lp_cs_tpool_worker, pool)) {
num_threads = i; /* previous thread is max */
break;
}
}
pool->num_threads = num_threads; pool->num_threads = num_threads;
for (unsigned i = 0; i < num_threads; i++)
pool->threads[i] = u_thread_create(lp_cs_tpool_worker, pool);
return pool; return pool;
} }

View File

@ -1238,9 +1238,8 @@ create_rast_threads(struct lp_rasterizer *rast)
for (unsigned i = 0; i < rast->num_threads; i++) { 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_ready, 0);
pipe_semaphore_init(&rast->tasks[i].work_done, 0); pipe_semaphore_init(&rast->tasks[i].work_done, 0);
rast->threads[i] = u_thread_create(thread_function, if (thrd_success != u_thread_create(rast->threads + i, thread_function,
(void *) &rast->tasks[i]); (void *) &rast->tasks[i])) {
if (!rast->threads[i]) {
rast->num_threads = i; /* previous thread is max */ rast->num_threads = i; /* previous thread is max */
break; break;
} }

View File

@ -144,24 +144,27 @@ r600_gpu_load_thread(void *param)
void r600_gpu_load_kill_thread(struct r600_common_screen *rscreen) void r600_gpu_load_kill_thread(struct r600_common_screen *rscreen)
{ {
if (!rscreen->gpu_load_thread) if (!rscreen->gpu_load_thread_created)
return; return;
p_atomic_inc(&rscreen->gpu_load_stop_thread); p_atomic_inc(&rscreen->gpu_load_stop_thread);
thrd_join(rscreen->gpu_load_thread, NULL); thrd_join(rscreen->gpu_load_thread, NULL);
rscreen->gpu_load_thread = 0; rscreen->gpu_load_thread_created = false;
} }
static uint64_t r600_read_mmio_counter(struct r600_common_screen *rscreen, static uint64_t r600_read_mmio_counter(struct r600_common_screen *rscreen,
unsigned busy_index) unsigned busy_index)
{ {
/* Start the thread if needed. */ /* Start the thread if needed. */
if (!rscreen->gpu_load_thread) { if (!rscreen->gpu_load_thread_created) {
mtx_lock(&rscreen->gpu_load_mutex); mtx_lock(&rscreen->gpu_load_mutex);
/* Check again inside the mutex. */ /* Check again inside the mutex. */
if (!rscreen->gpu_load_thread) if (!rscreen->gpu_load_thread_created) {
rscreen->gpu_load_thread = int ret = u_thread_create(&rscreen->gpu_load_thread, r600_gpu_load_thread, rscreen);
u_thread_create(r600_gpu_load_thread, rscreen); if (ret == thrd_success) {
rscreen->gpu_load_thread_created = true;
}
}
mtx_unlock(&rscreen->gpu_load_mutex); mtx_unlock(&rscreen->gpu_load_mutex);
} }

View File

@ -360,6 +360,7 @@ struct r600_common_screen {
/* GPU load thread. */ /* GPU load thread. */
mtx_t gpu_load_mutex; mtx_t gpu_load_mutex;
thrd_t gpu_load_thread; thrd_t gpu_load_thread;
bool gpu_load_thread_created;
union r600_mmio_counters mmio_counters; union r600_mmio_counters mmio_counters;
volatile unsigned gpu_load_stop_thread; /* bool */ volatile unsigned gpu_load_stop_thread; /* bool */

View File

@ -159,22 +159,25 @@ static int si_gpu_load_thread(void *param)
void si_gpu_load_kill_thread(struct si_screen *sscreen) void si_gpu_load_kill_thread(struct si_screen *sscreen)
{ {
if (!sscreen->gpu_load_thread) if (!sscreen->gpu_load_thread_created)
return; return;
p_atomic_inc(&sscreen->gpu_load_stop_thread); p_atomic_inc(&sscreen->gpu_load_stop_thread);
thrd_join(sscreen->gpu_load_thread, NULL); thrd_join(sscreen->gpu_load_thread, NULL);
sscreen->gpu_load_thread = 0; sscreen->gpu_load_thread_created = false;
} }
static uint64_t si_read_mmio_counter(struct si_screen *sscreen, unsigned busy_index) static uint64_t si_read_mmio_counter(struct si_screen *sscreen, unsigned busy_index)
{ {
/* Start the thread if needed. */ /* Start the thread if needed. */
if (!sscreen->gpu_load_thread) { if (!sscreen->gpu_load_thread_created) {
simple_mtx_lock(&sscreen->gpu_load_mutex); simple_mtx_lock(&sscreen->gpu_load_mutex);
/* Check again inside the mutex. */ /* Check again inside the mutex. */
if (!sscreen->gpu_load_thread) if (!sscreen->gpu_load_thread_created) {
sscreen->gpu_load_thread = u_thread_create(si_gpu_load_thread, sscreen); if (thrd_success == u_thread_create(&sscreen->gpu_load_thread, si_gpu_load_thread, sscreen)) {
sscreen->gpu_load_thread_created = true;
}
}
simple_mtx_unlock(&sscreen->gpu_load_mutex); simple_mtx_unlock(&sscreen->gpu_load_mutex);
} }

View File

@ -630,6 +630,7 @@ struct si_screen {
/* GPU load thread. */ /* GPU load thread. */
simple_mtx_t gpu_load_mutex; simple_mtx_t gpu_load_mutex;
thrd_t gpu_load_thread; thrd_t gpu_load_thread;
bool gpu_load_thread_created;
union si_mmio_counters mmio_counters; union si_mmio_counters mmio_counters;
volatile unsigned gpu_load_stop_thread; /* bool */ volatile unsigned gpu_load_stop_thread; /* bool */

View File

@ -163,8 +163,7 @@ nine_csmt_create( struct NineDevice9 *This )
ctx->device = This; ctx->device = This;
ctx->worker = u_thread_create(nine_csmt_worker, ctx); if (thrd_success != u_thread_create(&ctx->worker, nine_csmt_worker, ctx)) {
if (!ctx->worker) {
nine_queue_delete(ctx->pool); nine_queue_delete(ctx->pool);
FREE(ctx); FREE(ctx);
return NULL; return NULL;

View File

@ -114,7 +114,7 @@ int main(int argc, char *argv[])
for (i = 0; i < NUM_THREADS; i++) { for (i = 0; i < NUM_THREADS; i++) {
thread_ids[i] = i; thread_ids[i] = i;
threads[i] = u_thread_create(thread_function, (void *) &thread_ids[i]); u_thread_create(threads + i, thread_function, (void *) &thread_ids[i]);
} }
for (i = 0; i < NUM_THREADS; i++ ) { for (i = 0; i < NUM_THREADS; i++ ) {

View File

@ -337,9 +337,7 @@ util_queue_create_thread(struct util_queue *queue, unsigned index)
input->queue = queue; input->queue = queue;
input->thread_index = index; input->thread_index = index;
queue->threads[index] = u_thread_create(util_queue_thread_func, input); if (thrd_success != u_thread_create(queue->threads + index, util_queue_thread_func, input)) {
if (!queue->threads[index]) {
free(input); free(input);
return false; return false;
} }

View File

@ -101,26 +101,22 @@ util_get_current_cpu(void)
#endif #endif
} }
static inline thrd_t u_thread_create(int (*routine)(void *), void *param) static inline int u_thread_create(thrd_t *thrd, int (*routine)(void *), void *param)
{ {
thrd_t thread; int ret = thrd_error;
#ifdef HAVE_PTHREAD #ifdef HAVE_PTHREAD
sigset_t saved_set, new_set; sigset_t saved_set, new_set;
int ret;
sigfillset(&new_set); sigfillset(&new_set);
sigdelset(&new_set, SIGSYS); sigdelset(&new_set, SIGSYS);
pthread_sigmask(SIG_BLOCK, &new_set, &saved_set); pthread_sigmask(SIG_BLOCK, &new_set, &saved_set);
ret = thrd_create( &thread, routine, param ); ret = thrd_create(thrd, routine, param);
pthread_sigmask(SIG_SETMASK, &saved_set, NULL); pthread_sigmask(SIG_SETMASK, &saved_set, NULL);
#else #else
int ret; ret = thrd_create(thrd, routine, param);
ret = thrd_create( &thread, routine, param );
#endif #endif
if (ret)
return 0;
return thread; return ret;
} }
static inline void u_thread_setname( const char *name ) static inline void u_thread_setname( const char *name )