Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches: ["master","main"]
pull_request:
branches: ["master","main"]

jobs:
test:
Expand Down Expand Up @@ -86,13 +85,13 @@ jobs:
- name: Test
run: meson test -C build-cov

# Gate on 100% line coverage of the library sources. Branch coverage is
# reported but not gated: some branches are concurrency-dependent and
# cannot be forced deterministically on every run.
- name: Coverage report (100% line gate)
# Gate on 100% line and branch coverage of the library sources, merged
# across every backend build. The reader retry branches are reached
# deterministically by test_seqlock_retry, not by the concurrent tests.
- name: Coverage report (100% line and branch gate)
run: |
gcovr --root . --filter 'src/' --filter 'include/' \
--fail-under-line 100 --print-summary
--fail-under-line 100 --fail-under-branch 100 --print-summary

release-build:
name: Release build
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

### Changed

- CI gates on 100% branch coverage as well as line coverage. The reader retry branches are reached deterministically by a single-threaded test, and the no-atomics backend is built and tested alongside the C11 backend.

- Added security policy, SPDX headers, and cleaned ignore rules while leaving the existing special coverage/concurrency handling unchanged.

## [0.1.1] - 2026-06-03
Expand Down
13 changes: 8 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ meson setup build-tsan --buildtype=debug -Dbuild_tests=true \
meson compile -C build-tsan
meson test -C build-tsan --verbose

# Coverage (CI gates on 100% line coverage of src/ + include/)
# Coverage (CI gates on 100% line and branch coverage of src/ + include/)
meson setup build-cov --buildtype=debug -Dbuild_tests=true -Db_coverage=true \
-Dc_args=-fprofile-update=atomic
meson compile -C build-cov && meson test -C build-cov
gcovr --root . --filter 'src/' --filter 'include/' \
--fail-under-line 100 --print-summary
--fail-under-line 100 --fail-under-branch 100 --print-summary
```

Run the TSAN build on an x86-64 host (or a 4K-page AArch64). TSAN cannot
Expand Down Expand Up @@ -119,10 +119,13 @@ have only advisory deviations and want to keep it that way.
- The type-erased core (`seqlock_core_*`) is exercised directly by
`tests/test_seqlock_core.c`, including its argument-validation paths, which
the typed shims cannot reach. New core behaviour needs a test there.
- CI gates on **100% line coverage** of `src/` and `include/`. Branch coverage
is reported but not gated, because some branches are concurrency-dependent
and cannot be forced deterministically every run. New code without tests
- CI gates on **100% line and branch coverage** of `src/` and `include/`,
merged across every backend build. Reach new branches deterministically,
not through the concurrent tests: `tests/test_seqlock_retry.c` shows how
the reader retry paths are driven single-threaded. New code without tests
fails the gate.
- Each non-default backend (C11, no-atomics) is built and run by
`tests/test_seqlock_backend.c`.
- All tests must pass on every CI configuration: Linux with
ASan/UBSan, the dedicated TSAN job, the coverage gate, and the release
build.
Expand Down
45 changes: 36 additions & 9 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,44 @@ test_wrap_exe = executable(
)
test('seqlock wrap', test_wrap_exe)

# C11 <stdatomic.h> backend. The default on GCC/Clang is the __atomic path, so
# this forces the C11 backend for both the test and the core (compiled from
# library_sources with the same -D) to exercise that path and its lock-free
# static assertion.
test_c11_exe = executable(
'test_seqlock_c11',
files('test_seqlock_c11.c') + library_sources,
c_args: ['-DSEQLOCK_USE_C11_ATOMICS=1'],
# Non-default backends. The default on GCC/Clang is the __atomic path, so each
# remaining rung of the ladder is forced for both the test and the core
# (compiled from library_sources with the same -D) to exercise that path and,
# for C11, its lock-free static assertion.
foreach backend : [['c11', 'SEQLOCK_USE_C11_ATOMICS'], ['no_atomics', 'SEQLOCK_USE_NO_ATOMICS']]
test(
'seqlock ' + backend[0],
executable(
'test_seqlock_' + backend[0],
files('test_seqlock_backend.c') + library_sources,
c_args: ['-D@0@=1'.format(backend[1])],
include_directories: [public_headers, generated_headers],
),
)
endforeach

# Reader retry branches, reached deterministically so branch coverage can be
# gated. The first build reaches them in the library itself. The second
# replaces SEQLOCK_CPU_RELAX with a test hook to reach the odd-counter retry,
# so its core is compiled from library_sources with the same definition.
test_retry_exe = executable(
'test_seqlock_retry',
files('test_seqlock_retry.c'),
dependencies: [seqlock_dep],
include_directories: [public_headers, generated_headers],
)
test('seqlock retry', test_retry_exe)

test_retry_relax_exe = executable(
'test_seqlock_retry_relax',
files('test_seqlock_retry.c') + library_sources,
c_args: [
'-DSEQLOCK_TEST_RELAX_HOOK=1',
'-DSEQLOCK_CPU_RELAX()=do { extern void seqlock_test_relax(void); seqlock_test_relax(); } while (0)',
],
include_directories: [public_headers, generated_headers],
)
test('seqlock c11', test_c11_exe)
test('seqlock retry relax', test_retry_relax_exe)

# Concurrent torn-read detection + wait-free-writer liveness + large payload.
# This is also the executable rebuilt under -Db_sanitize=thread for the
Expand Down
49 changes: 49 additions & 0 deletions tests/test_seqlock_backend.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @file test_seqlock_backend.c
* @brief Exercises a forced atomic backend.
*
* On GCC/Clang the default backend is the `__atomic` builtin path, so the C11
* and no-atomics paths would otherwise never be compiled in CI.
* tests/meson.build builds this executable once per non-default backend,
* passing the backend define for both this TU and the core so the separately
* compiled seqlock.c agrees on the backend and ABI. A failure here is most
* likely a compile error: the C11 lock-free assertion rejecting the counter
* type, or a backend's atomic macros not expanding cleanly.
*/

#include "seqlock.h"
#include "seqlock_test.h"

#include <stdint.h>

SEQLOCK_DEFINE(backend_cell, uint64_t)

TEST_CASE(test_backend_round_trip)
{
backend_cell_t cell;
uint64_t out = 0U;
uint64_t in = 0x0123456789ABCDEFULL;

backend_cell_init(&cell);
backend_cell_load(&cell, &out);
TEST_ASSERT(out == 0U);

backend_cell_store(&cell, &in);
backend_cell_load(&cell, &out);
TEST_ASSERT(out == in);

out = 0U;
TEST_ASSERT(backend_cell_try_load(&cell, &out, 8U));
TEST_ASSERT(out == in);
}

int
main(void)
{
(void)fprintf(stdout, "\n=== seqlock forced-backend tests ===\n\n");

run_test(test_backend_round_trip, "backend_round_trip");

(void)fprintf(stdout, "\n=== all forced-backend tests passed ===\n\n");
return EXIT_SUCCESS;
}
51 changes: 0 additions & 51 deletions tests/test_seqlock_c11.c

This file was deleted.

118 changes: 118 additions & 0 deletions tests/test_seqlock_retry.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* @file test_seqlock_retry.c
* @brief Drives every reader retry branch deterministically.
*
* The concurrent tests reach the retry paths only when a writer happens to
* interleave, which is not enough to gate branch coverage. This executable
* reaches them on every run, single-threaded:
* - The output buffer aliases the counter, so the copy itself moves the
* counter between the reader's two loads.
* - With SEQLOCK_TEST_RELAX_HOOK, SEQLOCK_CPU_RELAX is replaced
* (tests/meson.build) with a hook that completes a pending write, so a
* blocking load that starts on an odd counter retries exactly once.
*
* tests/meson.build builds this file twice: against the library, and with the
* hook. The hook adds a call to the core's control flow, so gcov numbers that
* build's branches separately, and each build must reach its own.
*/

#include "seqlock.h"
#include "seqlock_test.h"

#include <stdbool.h>
#include <stdint.h>

static seqlock_hdr_t hdr;

#if defined(SEQLOCK_TEST_RELAX_HOOK)

static uint32_t relax_calls;
static bool complete_write;

void seqlock_test_relax(void);

void
seqlock_test_relax(void)
{
++relax_calls;
if (complete_write) {
hdr.seq = (SEQLOCK_COUNTER_TYPE)(hdr.seq + 1U);
}
}

#define RELAX_CALLS_RESET() (relax_calls = 0U)
#define RELAX_CALLS_EQUAL(n) (relax_calls == (n))

TEST_CASE(test_load_retries_while_odd)
{
const uint64_t payload = 0x1122334455667788ULL;
uint64_t out = 0U;

hdr.seq = (SEQLOCK_COUNTER_TYPE)1U;
RELAX_CALLS_RESET();
complete_write = true;
seqlock_core_load(&hdr, &payload, &out, sizeof out);
complete_write = false;

TEST_ASSERT(RELAX_CALLS_EQUAL(1U));
TEST_ASSERT(hdr.seq == (SEQLOCK_COUNTER_TYPE)2U);
TEST_ASSERT(out == payload);
}

#else

/* The default relax hint is not observable, so only outcomes are checked. */
#define RELAX_CALLS_RESET() ((void)0)
#define RELAX_CALLS_EQUAL(n) ((void)(n), true)

#endif

TEST_CASE(test_load_retries_when_counter_moves)
{
const SEQLOCK_COUNTER_TYPE next = (SEQLOCK_COUNTER_TYPE)4U;

hdr.seq = (SEQLOCK_COUNTER_TYPE)2U;
RELAX_CALLS_RESET();
seqlock_core_load(&hdr, &next, (void *)&hdr.seq, sizeof hdr.seq);

TEST_ASSERT(RELAX_CALLS_EQUAL(1U));
TEST_ASSERT(hdr.seq == next);
}

TEST_CASE(test_try_load_retries_when_counter_moves)
{
const SEQLOCK_COUNTER_TYPE next = (SEQLOCK_COUNTER_TYPE)4U;

/* One attempt: the counter moves during the copy, so the bound is
* exhausted. */
hdr.seq = (SEQLOCK_COUNTER_TYPE)2U;
RELAX_CALLS_RESET();
TEST_ASSERT(!seqlock_core_try_load(&hdr, &next, (void *)&hdr.seq,
sizeof hdr.seq, 1U));
TEST_ASSERT(RELAX_CALLS_EQUAL(1U));

/* Two attempts: the second sees a stable counter and succeeds. */
hdr.seq = (SEQLOCK_COUNTER_TYPE)2U;
RELAX_CALLS_RESET();
TEST_ASSERT(seqlock_core_try_load(&hdr, &next, (void *)&hdr.seq,
sizeof hdr.seq, 2U));
TEST_ASSERT(RELAX_CALLS_EQUAL(1U));
TEST_ASSERT(hdr.seq == next);
}

int
main(void)
{
(void)fprintf(stdout, "\n=== seqlock reader retry tests ===\n\n");

#if defined(SEQLOCK_TEST_RELAX_HOOK)
run_test(test_load_retries_while_odd, "load_retries_while_odd");
#endif
run_test(test_load_retries_when_counter_moves,
"load_retries_when_counter_moves");
run_test(test_try_load_retries_when_counter_moves,
"try_load_retries_when_counter_moves");

(void)fprintf(stdout, "\n=== all reader retry tests passed ===\n\n");
return EXIT_SUCCESS;
}
Loading