Clarification needed about locking requirements for a custom IOProxy::pwrite implementation #3785
Replies: 1 comment 3 replies
-
Any number of threads may be calling pread and pwrite, including concurrently, and that is guaranteed to be safe without the callers doing anything special. (*) ALL other methods of IOProxy are NOT thread safe, meaning that undefined things may happen if you make any call (other than pread/pwrite calls) simultaneously with any other call (including pread/pwrite). If anything other than pread and pwrite might be called from multiple threads, it's up to the calling application to ensure (via locks, or whatever) that no two calls happen concurrently. Expressed another way: an IOProxy requires that either (a) it is used from one thread only, or (b) if used by multiple threads, the only calls that are allowed to happen concurrently are calls to pread or pwrite.
No, the other presumed caller of pwrite is the user's program!
(*) Just as a pedantic note: although concurrent calls to pread or pwrite are considered thread-safe, it is not atomic. For example, two threads A and B doing writes to the same bytes concurrently might leave the file as if A happened and then B happened, or it might be as if B happened and then A happened, or it might be something else where they just all get jumbled up. There are no particular timing or consistency guarantees unless the simultaneous writes are to different ranges, if you see what I'm getting at. |
Beta Was this translation helpful? Give feedback.
-
I'm creating a custom IOProxy for writing into some in-memory buffers. What's the exact requirements for locking inside the
IOProxy::pwrite
implementation?On the one hand the IOProxy docs say:
This function is thread-safe against all other concurrent calls to pread() and pwrite(), but not against any other function of IOProxy.
But the existing
IOVecOutput
class seems to think otherwise. It takes a mutex inside its implementation but not for any of its other methods which seems to imply thatpwrite
could be called concurrently there. As the only callers of pwrite seem to be the write method, it's also strange that it would leave them_pos
variable un-guarded. I'm unclear on the protection requirements here.Any guidance? Is concurrent writing happening against IOProxy's and if so, what methods require an implementation to manually guard?
Beta Was this translation helpful? Give feedback.
All reactions