Skip to content

feat: migrate all write flag and cnt0 accesses to atomic macros#247

Open
oguyon wants to merge 2 commits intoframework-devfrom
feat/atomic-write-flag
Open

feat: migrate all write flag and cnt0 accesses to atomic macros#247
oguyon wants to merge 2 commits intoframework-devfrom
feat/atomic-write-flag

Conversation

@oguyon
Copy link
Copy Markdown
Member

@oguyon oguyon commented Apr 15, 2026

Summary

Migrate all direct md->write and md[0].write assignments, and all direct md->cnt0++ increments across coremods, milk-extra-src plugins, examples, and CLI to use the new SHMIM_WRITE_* and SHMIM_CNT0_* atomic accessor macros from ImageStruct.h.

This is the milk-side companion to the ImageStreamIO PR (milk-org/ImageStreamIO#69) that defines the macros. Together, they eliminate a class of cross-process race conditions from stale reads and compiler reordering of the write flag and frame counter.

Dependency: Requires milk-org/ImageStreamIO#69 to be merged first (the SHMIM_WRITE_* macros are defined in ImageStruct.h which is part of the ImageStreamIO submodule).

Rationale

See milk-org/ImageStreamIO#69 for the full rationale on why atomic accessors are needed for IMAGE_METADATA.write and cnt0. This PR performs the mechanical migration of ~95 call sites across 34 files in the milk repository to use the new macros.

What changed

Every instance of:

// Old — no compiler/CPU fence
img->md[0].write = 1;   // start write
// ... copy data ...
img->md[0].write = 0;   // end write
img->md[0].cnt0++;       // bump counter

was replaced with:

// New — proper release/acquire fences
SHMIM_WRITE_ACQUIRE(&img->md[0]);  // start write
// ... copy data ...
SHMIM_CNT0_INCREMENT(&img->md[0]); // bump counter
SHMIM_WRITE_RELEASE(&img->md[0]);  // end write

Diagnostic reads (e.g., image_stats.c, imagemon.c) now use SHMIM_WRITE_LOAD() and SHMIM_CNT0_LOAD() for acquire-ordered reads.

Backward compatibility

This is a code-only change — no struct layout, ABI, or shared memory format changes. All modifications are source-level substitutions of direct field access with macro calls that operate on the same underlying fields at the same offsets. The compiled binaries produce identical SHM layouts and are fully interoperable with older processes.

Opt-in write blocking

The write flag remains an opt-in cooperative mutex. Readers that want to block until a write completes call ImageStreamIO_BusywaitForNoWrite(), which now uses atomic loads internally. All writer code paths now use SHMIM_WRITE_ACQUIRE/RELEASE consistently, ensuring that any reader opting into write blocking will see a fully consistent frame.

Readers that do not call BusywaitForNoWrite are unaffected — they continue to read immediately after semaphore wakeup, as before.

Files changed (34 files, +140 −140)

coremods:

  • COREMOD_memory/: image_copy, image_copy_shm, stream_diff, stream_halfimdiff, stream_merge, stream_paste, stream_pixmapdecode, stream_updateloop, stream_TCP/UDP_receive, im3D_to_stream2D, image_mk_from
  • COREMOD_arith/imfunctions.c

milk-extra-src plugins:

  • linalgebra/: GPU_loop_MultMat_execute, MVMextractModes, cublas_Coeff2Map_Loop
  • linARfilterPred/: applyPF, build_linPF, linARfilterPred
  • image_basic/streamfeed.c
  • image_format/: extract_utr, stream_temporal_stats
  • img_reduce/img_reduce.c
  • info/: image_stats, imagemon, stream_monproc

examples:

  • example01_ImageStreamIO, example02_processinfo

module templates:

  • examplefunc3_updatestreamloop, examplefunc4_streamprocess

AI Authorship

  • Model: Antigravity
  • User edits: None (mechanical migration).

Prompt Summary

Codebase-wide migration of all direct IMAGE_METADATA.write and cnt0 field accesses to use the new atomic accessor macros, as part of the cross-process race condition remediation effort.

oguyon added 2 commits April 15, 2026 12:18
Replace all direct accesses to IMAGE_METADATA.write and cnt0++
across coremods, milk-extra-src, examples, and CLI with the new
SHMIM_WRITE_ACQUIRE/RELEASE/LOAD and SHMIM_CNT0_INCREMENT macros
from ImageStruct.h.

This ensures proper memory fences for cross-process synchronization
of shared memory streams, eliminating race conditions from stale
reads and compiler reordering.
Point submodule to commit 5176658 which adds the
SHMIM_WRITE_*/SHMIM_CNT0_* atomic accessor macros
in ImageStruct.h. Required for the milk-side migration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant