18 #include <stdatomic.h>
22 #ifndef ROOTSIM_TEST_NAME
23 #define ROOTSIM_TEST_NAME "rs_test"
26 static char *t_out_buf;
27 static size_t t_out_buf_size;
28 static size_t t_out_wrote;
30 static char **test_argv;
35 __attribute__((weak))
nid_t n_nodes = 1;
43 void
_log_log(
int level, const
char *file,
unsigned line, const
char *fmt, ...)
51 int main(
int argc,
char **argv);
69 char **argv = malloc(
sizeof(*argv) * (argc + 1));
73 argv[0] = ROOTSIM_TEST_NAME;
77 sizeof(*argv) * argc);
99 __attribute__((constructor))
104 puts(
"Starting " ROOTSIM_TEST_NAME
" test");
114 t_out_buf = malloc(t_out_buf_size);
115 if (t_out_buf == NULL)
118 int test_ret =
main(test_argc, test_argv);
123 puts(
"Test failed: output is shorter than the expected one");
127 puts(
"Successfully run " ROOTSIM_TEST_NAME
" test");
131 static int test_printf_internal(
const char *restrict fmt, va_list args)
134 va_copy(args_cpy, args);
135 size_t p_size = vsnprintf(t_out_buf, t_out_buf_size, fmt, args_cpy);
139 puts(
"Test failed: output is longer than the expected one");
143 if (p_size >= t_out_buf_size) {
146 }
while(p_size >= t_out_buf_size);
149 t_out_buf = malloc(t_out_buf_size);
150 if (t_out_buf == NULL)
153 vsnprintf(t_out_buf, t_out_buf_size, fmt, args);
157 printf(
"Test failed: output is different from the expected one %zu\n", t_out_wrote);
160 t_out_wrote += p_size;
173 int ret = test_printf_internal(fmt, args);
188 int ret = test_printf_internal(fmt, args);
202 static atomic_uint b_in, b_out, b_cr;
206 unsigned max_in_before_reset = (UINT_MAX / 2) - (UINT_MAX / 2) % count;
208 i = atomic_fetch_add_explicit(
209 &b_in, 1U, memory_order_acq_rel) + 1;
210 }
while (__builtin_expect(i > max_in_before_reset, 0));
212 unsigned cr = atomic_load_explicit(&b_cr, memory_order_relaxed);
214 bool leader = i == cr + count;
216 atomic_store_explicit(&b_cr, cr + count, memory_order_release);
219 cr = atomic_load_explicit(&b_cr, memory_order_relaxed);
222 atomic_thread_fence(memory_order_acquire);
224 unsigned o = atomic_fetch_add_explicit(&b_out, 1, memory_order_release) + 1;
225 if (__builtin_expect(o == max_in_before_reset, 0)) {
226 atomic_thread_fence(memory_order_acquire);
227 atomic_store_explicit(&b_cr, 0, memory_order_relaxed);
228 atomic_store_explicit(&b_out, 0, memory_order_relaxed);
229 atomic_store_explicit(&b_in, 0, memory_order_release);