PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 58ms app.codeStats 0ms

/src/LinFu.AOP.Interfaces/MultithreadedCallCounter.cs

http://github.com/philiplaureano/LinFu
C# | 46 lines | 38 code | 8 blank | 0 comment | 3 complexity | d42adc4fd411a08a571706f1f1d6cc70 MD5 | raw file
 1using System.Collections.Generic;
 2using System.Threading;
 3
 4namespace LinFu.AOP.Interfaces
 5{
 6    internal class MultiThreadedCallCounter : ICallCounter
 7    {
 8        private readonly Dictionary<int, SingleThreadedCallCounter> _counts =
 9            new Dictionary<int, SingleThreadedCallCounter>();
10
11
12        public void Increment(IInvocationInfo context)
13        {
14            var threadId = Thread.CurrentThread.ManagedThreadId;
15            lock (_counts)
16            {
17                if (!_counts.ContainsKey(threadId))
18                    _counts[threadId] = new SingleThreadedCallCounter();
19
20                _counts[threadId].Increment(context);
21            }
22        }
23
24        public void Decrement(IInvocationInfo context)
25        {
26            var threadId = Thread.CurrentThread.ManagedThreadId;
27            if (!_counts.ContainsKey(threadId))
28                return;
29
30            lock (_counts)
31            {
32                var counter = _counts[threadId];
33                counter.Decrement(context);
34            }
35        }
36
37        public int GetPendingCalls(IInvocationInfo context)
38        {
39            var threadId = Thread.CurrentThread.ManagedThreadId;
40            if (!_counts.ContainsKey(threadId))
41                return 0;
42
43            return _counts[threadId].GetPendingCalls(context);
44        }
45    }
46}