Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define TR_VerboseLog::CriticalSection as a vlog RAII lock guard
The verbose log can now be locked by declaring a local variable of type TR_VerboseLog::CriticalSection, and the lock will be automatically released at the end of the variable's scope. Locking this way is advantageous because it's not possible to forget to release the lock, and because the lock will always be released even if an exception is thrown. In general, releasing a lock while unexpectedly unwinding for an exception could leave a program in a bad state. If the lock protects an invariant, it's possible to take the lock, temporarily violate the invariant, and then unwind before restoring it. However, in the case of the verbose log lock specifically, the only effect of unlocking on unwind will be to prematurely truncate the output being generated at the time. The output is just diagnostic, so it's best to simply release the lock. There is an optional boolean parameter to specify whether or not to actually acquire the lock. This is useful for cases where we are incrementally generating output that needs to stay together, but we're doing so conditionally in the midst of other logic, e.g. if (verbose) TR_VerboseLog::write(...); ... if (verbose) TR_VerboseLog::write(...); ... if (verbose) TR_VerboseLog::writeLine(...); The boolean parameter allows for a sequence like this to use CriticalSection while still only locking when necessary: TR_VerboseLog::CriticalSection vlogLock(verbose); Without it, the use of CriticalSection would force the code either to lock each write individually, which is too fine a granularity and allows unwanted interleaving, or to take the lock even when not outputting to the verbose log.
- Loading branch information