jbakic / Shielded

A strict and mostly lock-free Software Transactional Memory (STM) for .NET
MIT License
239 stars 21 forks source link

Transaction identification #12

Closed janslav closed 7 years ago

janslav commented 7 years ago

For logging/tracing purposes it might make sense to have some sort of id of the current transaction available. Currently there's just "IsInTransaction" boolean but no way of telling one transaction from the other. Could be conceivably constructed in user code by inserting some code around the InTransaction actions, but it won't be very reliable. Maybe make this an optional InTransaction() parameter?

jbakic commented 7 years ago

You can do this yourself:

public static class TransactionId
{
    private static ShieldedLocal<Guid> _id = new ShieldedLocal<Guid>();
    public static Guid Value
    {
        get
        {
            if (!_id.HasValue) _id.Value = Guid.NewGuid();
            return _id.Value;
        }
    }
}

The ShieldedLocal Value lasts as long as the transaction run is in progress, and is visible from lambdas passed to SyncSideEffect and WhenCommitting.

janslav commented 7 years ago

Cool, looks like what I need, thanks :)