forked from microsoft/GraphEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadContext.cs
More file actions
66 lines (60 loc) · 1.93 KB
/
ThreadContext.cs
File metadata and controls
66 lines (60 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Graph Engine
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Trinity.Diagnostics;
namespace Trinity.Storage
{
public class CellLockOverflowException : Exception
{
internal long m_cellid;
public long CellId { get { return m_cellid; } }
public CellLockOverflowException(long cellid)
: base("Cell lock overflowed.")
{
m_cellid = cellid;
}
}
public class DeadlockException : Exception
{
public DeadlockException() : base("Deadlock detected.") { }
}
/// <summary>
/// Provides thread-specific cell lock context.
/// Note, a ThreadContext does not record affinity
/// with a specific managed/unmanaged thread.
/// It is up to the storage subsystem to leverage
/// TLS (thread local storage) to establish a bound.
/// </summary>
internal unsafe class ThreadContext : IDisposable
{
internal void* m_lock_ctx;
internal int m_managedThreadId;
internal bool m_disposed = false;
internal ThreadContext()
{
m_lock_ctx = CLocalMemoryStorage.CThreadContextAllocate();
m_managedThreadId = Thread.CurrentThread.ManagedThreadId;
Log.WriteLine(LogLevel.Debug, "Created thread context 0x{0:X16}", (ulong)m_lock_ctx);
}
~ThreadContext()
{
Dispose();
}
public void Dispose()
{
if (!m_disposed)
{
Log.WriteLine(LogLevel.Debug, "Releasing thread context 0x{0:X16}", (ulong)m_lock_ctx);
CLocalMemoryStorage.CThreadContextDeallocate(m_lock_ctx);
m_lock_ctx = null;
}
}
}
}