AcidPtr & AcidCommitPtr¶
Provides transparent interface for “copy on write” and “commit when done” in a familiar unique_ptr style.
Named after the desirable properties of a database, ACID acronym:
- Atomic - reads and writes avoid skew, by using mutex locks.
- Consistent - data can only be changed by a commit, and only one commit can exist at a time per data.
- Isolated - commits of a single point of data are not concurrent. But commits of seperate data can be conncurrent.
- Durable - shared_ptr is used to keep older versions of data in memory while references exist.
Note this code is currently implemented with mutex locks, it is expected to be fast due to the very brief duration of each lock. It would be plausible to change the implementation to use atomics or spin locks.
- On construction, duplicate values from a shared_ptr to a unique_ptr. (aka copy read to write memory)
- On destruction, move value ptr to shared_ptr. (aka move write ptr to read ptr)
AcidCommitPtr executes this transparent to the writer code. It copies the data on construction, and finalizes on destruction. A MemLock is used to allow exclusive read and write access, however the access is made to as fast as possible.
When the writer is done,
~AcidCommitPtr() is called and its
AcidPtr is updated to point at the written copy, so that future read requests will use it. Existing reader will continue to use the old data.