fix(plugin): use POSIX pthreads in C11 test fixture for TSan compatibility

This commit is contained in:
faumaray committed 2026-11-26 17:10:54 +03:00
1 parent 40b579d683
commit 1a54f3857e
2 files changed
+24 -17

No files matched your search

+4
View File
@@ -265,6 +265,10 @@ if(TARGET VoidTests AND TARGET Void::Plugin)
if(VOID_WARNINGS_AS_ERRORS)
target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wpedantic -Werror)
endif()
find_package(Threads QUIET)
if(TARGET Threads::Threads)
target_link_libraries(${target_name} PRIVATE Threads::Threads)
endif()
if(VOID_ENABLE_ASAN)
target_compile_options(${target_name} PRIVATE -fsanitize=address -fno-omit-frame-pointer)
target_link_options(${target_name} PRIVATE -fsanitize=address)
+20 -17
View File
@@ -1,11 +1,14 @@
#define _POSIX_C_SOURCE 200809L
#include <Void/Plugin/CAbi.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <threads.h>
#include <time.h>
typedef struct cabi_calc_v1 {
uint32_t (*version)(void_module_handle* h);
@@ -14,7 +17,7 @@ typedef struct cabi_calc_v1 {
struct void_operation_handle {
void_module_handle* owner;
thrd_t worker;
pthread_t worker;
int worker_started;
atomic_int cancelled;
uint32_t op_kind;
@@ -32,7 +35,7 @@ struct void_module_handle {
int ckpt_fail;
int ckpt_oversized;
int ready;
mtx_t ops_mu;
pthread_mutex_t ops_mu;
struct void_operation_handle* ops[64];
size_t ops_count;
};
@@ -126,7 +129,7 @@ static int32_t cabi_create(const void_host_api_v1* host_api,
atomic_init(&h->total, 0);
atomic_init(&h->quiescing, 0);
h->ready = 1;
mtx_init(&h->ops_mu, mtx_plain);
pthread_mutex_init(&h->ops_mu, NULL);
*out_handle = h;
return VOID_STATUS_OK;
}
@@ -296,7 +299,7 @@ static void cabi_release_interface(void_module_handle* h,
(void)view;
}
static int async_worker(void* arg)
static void* async_worker(void* arg)
{
struct void_operation_handle* op = (struct void_operation_handle*)arg;
int pinned = 0;
@@ -315,9 +318,9 @@ static int async_worker(void* arg)
op->owner->host.release_activity_pin(
op->owner->host.host_ctx, op->owner->host.generation);
}
return 0;
return NULL;
}
thrd_sleep(&ts, NULL);
nanosleep(&ts, NULL);
}
int32_t val = atomic_fetch_add(&op->owner->total, op->delta) + op->delta;
uint8_t buf[4];
@@ -328,7 +331,7 @@ static int async_worker(void* arg)
op->owner->host.release_activity_pin(op->owner->host.host_ctx,
op->owner->host.generation);
}
return 0;
return NULL;
}
static int32_t cabi_submit_async(void_module_handle* h,
@@ -374,15 +377,15 @@ static int32_t cabi_submit_async(void_module_handle* h,
return VOID_STATUS_OK;
}
mtx_lock(&h->ops_mu);
pthread_mutex_lock(&h->ops_mu);
if (h->ops_count < 64) {
h->ops[h->ops_count++] = op;
}
mtx_unlock(&h->ops_mu);
pthread_mutex_unlock(&h->ops_mu);
op->worker_started = 1;
*out_op = op;
if (thrd_create(&op->worker, async_worker, op) != thrd_success) {
if (pthread_create(&op->worker, NULL, async_worker, op) != 0) {
op->worker_started = 0;
*out_op = NULL;
free(op);
@@ -407,11 +410,11 @@ static void cabi_release_async(void_module_handle* h, void_operation_handle* op)
return;
}
if (op->worker_started) {
thrd_join(op->worker, NULL);
pthread_join(op->worker, NULL);
op->worker_started = 0;
}
if (h != NULL) {
mtx_lock(&h->ops_mu);
pthread_mutex_lock(&h->ops_mu);
for (size_t i = 0; i < h->ops_count; ++i) {
if (h->ops[i] == op) {
h->ops[i] = h->ops[h->ops_count - 1];
@@ -419,7 +422,7 @@ static void cabi_release_async(void_module_handle* h, void_operation_handle* op)
break;
}
}
mtx_unlock(&h->ops_mu);
pthread_mutex_unlock(&h->ops_mu);
}
free(op);
}
@@ -430,13 +433,13 @@ static int32_t cabi_request_stop(void_module_handle* h)
return VOID_STATUS_INVALID_ARGUMENT;
}
atomic_store(&h->quiescing, 1);
mtx_lock(&h->ops_mu);
pthread_mutex_lock(&h->ops_mu);
for (size_t i = 0; i < h->ops_count; ++i) {
if (h->ops[i] != NULL) {
atomic_store(&h->ops[i]->cancelled, 1);
}
}
mtx_unlock(&h->ops_mu);
pthread_mutex_unlock(&h->ops_mu);
return VOID_STATUS_OK;
}
@@ -450,7 +453,7 @@ static void cabi_destroy(void_module_handle* h)
if (h == NULL) {
return;
}
mtx_destroy(&h->ops_mu);
pthread_mutex_destroy(&h->ops_mu);
free(h);
}