Skip to content

Add 'pread'-backed method for thread-safe hunk reading#162

Open
xakep666 wants to merge 1 commit into
rtissera:masterfrom
xakep666:stateless-read
Open

Add 'pread'-backed method for thread-safe hunk reading#162
xakep666 wants to merge 1 commit into
rtissera:masterfrom
xakep666:stateless-read

Conversation

@xakep666

@xakep666 xakep666 commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Addressing proposal in #34

This PR adds method chd_read_threadsafe - a thread-safe version of chd_read. This method will be helpful for implementing threaded read-aheads purely by consumers without adding external dependencies (like pthread) .

My main programming language is Go and there's a popular approach "leave concurrency to a caller" used when concurrent code is written. And I think it fits good in this case.

Some implementation details:

  • chd_read_threadsafe method has extra argument compressed_buffer which is used to store raw compressed data read from file. This is the one of two things required for thread-safety. chd_read uses same buffer stored in chd_file structure.
  • Extra function pointer (interface method) fpread added to chd_core_file_callbacks to allow thread-safe reading from file. Used only in chd_read_threadsafe.
  • On *nix libc's pread is being utilized for thread-safe reading from file
  • On Windows ReadFile used for same purpose. Reference: https://github.com/aleitner/windows_pread
  • On other platforms or in case fpread was not provided chd_read_threadsafe will return error CHDERR_NOT_SUPPORTED.
  • hunk_read_into_memory has been tweaked a bit to accept arbitrary compressed data buffer pointer and different reading methods: seek_and_read (old one) or read_at (new one).
  • Lazy allocation of internal compressed data buffer chd->compressed as it only used inside chd_read.

@xakep666 xakep666 force-pushed the stateless-read branch 2 times, most recently from 9a1137d to 160f6af Compare June 20, 2026 18:24
@xakep666 xakep666 marked this pull request as ready for review June 20, 2026 18:52
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