Skip to content
Snippets Groups Projects
Verified Commit 84e5b412 authored by Jacob Lorentzon's avatar Jacob Lorentzon
Browse files

Improve once test.

parent d8bc60e0
No related branches found
No related tags found
1 merge request!380Replace pthreads-emb with a native implementation
Pipeline #12082 failed
...@@ -6,29 +6,26 @@ ...@@ -6,29 +6,26 @@
#include <unistd.h> #include <unistd.h>
#include "common.h" #include "common.h"
#include "../test_helpers.h"
struct once_data { _Thread_local size_t this_thread_count = 0;
size_t count;
};
_Thread_local struct once_data once_data = {0};
#define COUNT 1024 #define COUNT 1024
#define THREADS 4 #define THREADS 4
void constructor() { static void constructor(void) {
once_data.count += 1; this_thread_count++;
} }
struct arg { struct arg {
pthread_barrier_t *barrier; pthread_barrier_t *barrier;
pthread_once_t *onces; pthread_once_t *onces;
int status;
size_t count; size_t count;
size_t index; size_t index;
}; };
void *routine(void *arg_raw) { void *routine(void *arg_raw) {
int status;
struct arg *arg = arg_raw; struct arg *arg = arg_raw;
if (arg->index == THREADS - 1) { if (arg->index == THREADS - 1) {
...@@ -37,21 +34,21 @@ void *routine(void *arg_raw) { ...@@ -37,21 +34,21 @@ void *routine(void *arg_raw) {
printf("spawned %zu\n", arg->index); printf("spawned %zu\n", arg->index);
} }
int wait_status = pthread_barrier_wait(arg->barrier); status = pthread_barrier_wait(arg->barrier);
printf("waited %zu leader=%s\n", arg->index, (wait_status == PTHREAD_BARRIER_SERIAL_THREAD) ? "true" : "false"); printf("waited %zu leader=%s\n", arg->index, (status == PTHREAD_BARRIER_SERIAL_THREAD) ? "true" : "false");
if (wait_status != PTHREAD_BARRIER_SERIAL_THREAD && wait_status != 0) { if (status != PTHREAD_BARRIER_SERIAL_THREAD) {
ERROR_IF(pthread_barrier_wait, status, != 0);
return NULL; return NULL;
} }
for (size_t i = 0; i < COUNT; i++) { for (size_t i = 0; i < COUNT; i++) {
if ((arg->status = pthread_once(&arg->onces[i], constructor)) != 0) { status = pthread_once(&arg->onces[i], constructor);
return NULL; ERROR_IF(pthread_once, status, != 0);
}
} }
arg->count = once_data.count; arg->count = this_thread_count;
return NULL; return NULL;
} }
...@@ -71,44 +68,32 @@ int main(void) { ...@@ -71,44 +68,32 @@ int main(void) {
printf("Barrier at %p, onces at %p\n", &barrier, onces); printf("Barrier at %p, onces at %p\n", &barrier, onces);
if ((status = pthread_barrier_init(&barrier, NULL, THREADS)) != 0) { status = pthread_barrier_init(&barrier, NULL, THREADS);
return fail(status, "barrier init"); ERROR_IF(pthread_barrier_init, status, != 0);
}
pthread_t threads[THREADS]; pthread_t threads[THREADS];
struct arg args[THREADS]; struct arg args[THREADS];
threads[0] = pthread_self();
for (size_t i = 0; i < THREADS; i++) { for (size_t i = 0; i < THREADS; i++) {
args[i] = (struct arg){ .barrier = &barrier, .onces = onces, .status = 0, .count = 0, .index = i }; args[i] = (struct arg){ .barrier = &barrier, .onces = onces, .count = 0, .index = i };
printf("spawning %zu\n", i); printf("spawning %zu\n", i);
if (i == 0) { status = pthread_create(&threads[i], NULL, routine, &args[i]);
continue; ERROR_IF(pthread_create, status, != 0);
}
if ((status = pthread_create(&threads[i], NULL, routine, &args[i])) != 0) {
return fail(status, "thread create");
}
} }
routine(&args[0]);
size_t total_count = 0; size_t total_count = 0;
for (size_t i = 0; i < THREADS; i++) { for (size_t i = 0; i < THREADS; i++) {
if (i != 0) { status = pthread_join(threads[i], NULL);
if ((status = pthread_join(threads[i], NULL)) != 0) { ERROR_IF(pthread_join, status, != 0);
return fail(status, "join");
}
}
if (args[i].status != 0) {
return fail(args[i].status, "thread");
}
total_count += args[i].count; total_count += args[i].count;
} }
status = pthread_barrier_destroy(&barrier);
ERROR_IF(pthread_barrier_destroy, status, != 0);
assert(total_count == COUNT); assert(total_count == COUNT);
return EXIT_SUCCESS; return EXIT_SUCCESS;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment