PageRenderTime 8ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.AOP.Interfaces/CallCounter.cs

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