Skip to content

protocol: Avoid write locks just to change (now-)atomic booleans - #200

Open
luke-jr wants to merge 2 commits into
OCEAN-xyz:masterfrom
luke-jr:atomic_datum_job
Open

luke-jr wants to merge 2 commits into
OCEAN-xyz:masterfrom
luke-jr:atomic_datum_job

Conversation

@luke-jr

@luke-jr luke-jr commented May 20, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@luke-jr luke-jr added this to the v0.2.7 milestone May 20, 2026
@luke-jr
luke-jr requested a review from Copilot May 21, 2026 02:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the DATUM protocol job tracking flags so they can be updated without acquiring a write lock, by switching the relevant fields to C11/C23 atomics.

Changes:

  • Convert server_has_merkle_branches, server_has_coinbase[*], and server_has_coinbase_empty from bool to atomic_bool.
  • Replace rwlock “upgrade-to-write” sections in datum_protocol_pow() with atomic_store_explicit(..., memory_order_relaxed).
  • Remove initialization/reset under datum_jobs_rwlock for these flags (since they’re now atomic).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/datum_protocol.h Switch selected T_DATUM_PROTOCOL_JOB flags to atomic_bool and include <stdatomic.h>.
src/datum_protocol.c Remove rwlock write-lock upgrades when toggling job flags; use relaxed atomic stores instead; reset flags without rwlock.
Comments suppressed due to low confidence (3)

src/datum_protocol.c:1385

  • This relaxed atomic store is good, but the corresponding read (if (!datum_jobs[...].server_has_coinbase_empty)) will be an implicit seq_cst load. Consider using atomic_load_explicit(..., memory_order_relaxed) for that check to avoid stronger-than-needed ordering and extra barriers.
			
			atomic_store_explicit(&datum_jobs[pow->datum_job_id].server_has_coinbase_empty, true, memory_order_relaxed);
		}

src/datum_protocol.c:1398

  • The store uses memory_order_relaxed, but the guard condition uses an implicit seq_cst load on the atomic_bool. Consider changing the check to atomic_load_explicit(..., memory_order_relaxed) so the load/store pair uses consistent relaxed semantics and avoids unnecessary ordering costs.
			
			atomic_store_explicit(&datum_jobs[pow->datum_job_id].server_has_coinbase[pow->coinbase_id], true, memory_order_relaxed);
		}

src/datum_protocol.c:1456

  • These assignments are now writing to atomic_bool fields, which makes them implicit seq_cst stores. If these flags are intended to be relaxed (as in the atomic_store_explicit(..., memory_order_relaxed) updates), consider initializing/resetting them with atomic_store_explicit(..., false, memory_order_relaxed) for consistency and to avoid stronger-than-needed barriers.
	for(i=0;i<MAX_DATUM_PROTOCOL_JOBS;i++) {
		datum_jobs[i].server_has_merkle_branches = false;
		datum_jobs[i].server_has_coinbase_empty = false;
		for(n=0;n<8;n++) {
			datum_jobs[i].server_has_coinbase[n] = false;
		}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/datum_protocol.c

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

src/datum_protocol.c:1374

  • atomic_load_explicit requires the address of the atomic object; this call is missing & and will not compile. Use atomic_load_explicit(&datum_jobs[pow->datum_job_id].server_has_coinbase_empty, …).
		if (!atomic_load_explicit(datum_jobs[pow->datum_job_id].server_has_coinbase_empty, memory_order_relaxed)) {

src/datum_protocol.c:1387

  • atomic_load_explicit should be passed a pointer to the atomic element; this call is missing & on server_has_coinbase[pow->coinbase_id] and will fail to compile.
		if (!atomic_load_explicit(datum_jobs[pow->datum_job_id].server_has_coinbase[pow->coinbase_id], memory_order_relaxed)) {

Comment thread src/datum_protocol.c Outdated
@luke-jr
luke-jr force-pushed the atomic_datum_job branch from 4f17676 to 4ac1821 Compare May 21, 2026 08:56
@luke-jr
luke-jr requested a review from Copilot May 21, 2026 08:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.

Comment thread src/datum_protocol.h
Comment on lines +93 to 97
atomic_bool server_has_merkle_branches;

bool server_has_coinbase[8];
bool server_has_coinbase_empty;
bool server_has_short_txnlist;

bool server_has_validated_block;
atomic_bool server_has_coinbase[8];
atomic_bool server_has_coinbase_empty;
} T_DATUM_PROTOCOL_JOB;
Comment thread src/datum_protocol.c
}

if (!datum_jobs[pow->datum_job_id].server_has_merkle_branches) {
if (!atomic_load_explicit(&datum_jobs[pow->datum_job_id].server_has_merkle_branches, memory_order_relaxed)) {
Comment thread src/datum_protocol.c
pthread_rwlock_wrlock(&datum_jobs_rwlock);
w = true;
datum_jobs[pow->datum_job_id].server_has_merkle_branches = true;
atomic_store_explicit(&datum_jobs[pow->datum_job_id].server_has_merkle_branches, true, memory_order_relaxed);
Comment thread src/datum_protocol.c

if (pow->subsidy_only) {
if (!datum_jobs[pow->datum_job_id].server_has_coinbase_empty) {
if (!atomic_load_explicit(&datum_jobs[pow->datum_job_id].server_has_coinbase_empty, memory_order_relaxed)) {
Comment thread src/datum_protocol.c
Comment on lines +1384 to +1387
atomic_store_explicit(&datum_jobs[pow->datum_job_id].server_has_coinbase_empty, true, memory_order_relaxed);
}
} else {
if (!datum_jobs[pow->datum_job_id].server_has_coinbase[pow->coinbase_id]) {
if (!atomic_load_explicit(&datum_jobs[pow->datum_job_id].server_has_coinbase[pow->coinbase_id], memory_order_relaxed)) {
Comment thread src/datum_protocol.c
}

datum_jobs[pow->datum_job_id].server_has_coinbase[pow->coinbase_id] = true;
atomic_store_explicit(&datum_jobs[pow->datum_job_id].server_has_coinbase[pow->coinbase_id], true, memory_order_relaxed);
Comment thread src/datum_protocol.c
Comment on lines 1451 to 1457
for(i=0;i<MAX_DATUM_PROTOCOL_JOBS;i++) {
datum_jobs[i].server_has_merkle_branches = false;
datum_jobs[i].server_has_coinbase_empty = false;
datum_jobs[i].server_has_short_txnlist = false;
datum_jobs[i].server_has_validated_block = false;
atomic_store_explicit(&datum_jobs[i].server_has_merkle_branches, false, memory_order_relaxed);
atomic_store_explicit(&datum_jobs[i].server_has_coinbase_empty, false, memory_order_relaxed);
for(n=0;n<8;n++) {
datum_jobs[i].server_has_coinbase[n] = false;
atomic_store_explicit(&datum_jobs[i].server_has_coinbase[n], false, memory_order_relaxed);
}
}
Comment thread src/datum_protocol.c
datum_jobs[i].server_has_coinbase_empty = false;
datum_jobs[i].server_has_short_txnlist = false;
datum_jobs[i].server_has_validated_block = false;
atomic_store_explicit(&datum_jobs[i].server_has_merkle_branches, false, memory_order_relaxed);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropping this loop's wrlock races the non-atomic memset still done under wrlock in datum_protocol_setup_new_job_idx. keep the lock.

@luke-jr luke-jr added the refactor Reorganizing code label Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Reorganizing code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants