Based on Microsoft sample code,¹ it seems like the expectation is that consumers
of the `IStream` API may expect to concurrently manipulate reference counts.
This change guards against this by making the referencing counting code use
lock-free atomics, based on the same Microsoft sample.
Note that this is essentially proactively addressing a latent issue as it is, in
general, unsafe to use Graphviz libraries or plugins in a multithreaded
environment right now.
¹ https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms752876(v=vs.85)
ULONG FileStream::AddRef()
{
- return ++_ref;
+ return (ULONG)InterlockedIncrement((ULONG*)&_ref);
}
ULONG FileStream::Release()
{
- ULONG ref = --_ref;
+ ULONG ref = (ULONG)InterlockedDecrement((ULONG*)&_ref);
if (ref == 0)
delete this;
return ref;