Skip to content

Commit 0f08f9d

Browse files
Moved RAII classes to header
1 parent 30a7579 commit 0f08f9d

4 files changed

Lines changed: 104 additions & 91 deletions

File tree

include/dxc/Support/RAIIClasses.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
///////////////////////////////////////////////////////////////////////////////
2+
// //
3+
// RAIIClasses.h //
4+
// Copyright (C) Microsoft Corporation. All rights reserved. //
5+
// This file is distributed under the University of Illinois Open Source //
6+
// License. See LICENSE.TXT for details. //
7+
// //
8+
// RAII Helpers. //
9+
// //
10+
///////////////////////////////////////////////////////////////////////////////
11+
12+
#pragma once
13+
#include "WinIncludes.h"
14+
15+
class HhEvent {
16+
public:
17+
HANDLE m_handle = INVALID_HANDLE_VALUE;
18+
19+
public:
20+
HRESULT Init() {
21+
if (m_handle == INVALID_HANDLE_VALUE) {
22+
m_handle = CreateEvent(nullptr, TRUE, FALSE, nullptr);
23+
if (m_handle == INVALID_HANDLE_VALUE) {
24+
return HRESULT_FROM_WIN32(GetLastError());
25+
}
26+
}
27+
return S_OK;
28+
}
29+
HANDLE Get() { return m_handle; }
30+
void SetEvent() { ::SetEvent(m_handle); }
31+
void ResetEvent() { ::ResetEvent(m_handle); }
32+
~HhEvent() {
33+
if (m_handle != INVALID_HANDLE_VALUE) {
34+
CloseHandle(m_handle);
35+
}
36+
}
37+
};
38+
39+
class HhCriticalSection {
40+
private:
41+
CRITICAL_SECTION m_cs;
42+
43+
public:
44+
HhCriticalSection() { InitializeCriticalSection(&m_cs); }
45+
~HhCriticalSection() { DeleteCriticalSection(&m_cs); }
46+
class Lock {
47+
private:
48+
CRITICAL_SECTION *m_pLock;
49+
50+
public:
51+
Lock() = delete;
52+
Lock(const Lock &) = delete;
53+
Lock(Lock &&other) { std::swap(m_pLock, other.m_pLock); }
54+
Lock(CRITICAL_SECTION *pLock) : m_pLock(pLock) {
55+
EnterCriticalSection(m_pLock);
56+
}
57+
~Lock() {
58+
if (m_pLock)
59+
LeaveCriticalSection(m_pLock);
60+
}
61+
};
62+
Lock LockCS() { return Lock(&m_cs); }
63+
};
64+
65+
class ClassObjectRegistration {
66+
private:
67+
DWORD m_reg = 0;
68+
HRESULT m_hr = E_FAIL;
69+
70+
public:
71+
HRESULT Register(REFCLSID rclsid, IUnknown *pUnk, DWORD dwClsContext,
72+
DWORD flags) {
73+
m_hr = CoRegisterClassObject(rclsid, pUnk, dwClsContext, flags, &m_reg);
74+
return m_hr;
75+
}
76+
~ClassObjectRegistration() {
77+
if (SUCCEEDED(m_hr))
78+
CoRevokeClassObject(m_reg);
79+
}
80+
};
81+
82+
class CoInit {
83+
private:
84+
HRESULT m_hr = E_FAIL;
85+
86+
public:
87+
HRESULT Initialize(DWORD dwCoInit) {
88+
m_hr = CoInitializeEx(nullptr, dwCoInit);
89+
return m_hr;
90+
}
91+
~CoInit() {
92+
if (SUCCEEDED(m_hr))
93+
CoUninitialize();
94+
}
95+
};
96+

tools/clang/unittests/HLSLExec/ShaderOpTest.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ void ShaderOpTest::CopyBackResources() {
290290
}
291291
pList->Close();
292292
ExecuteCommandList(ResCommandList.Queue, pList);
293-
WaitForSignal(ResCommandList.Queue, m_pFence, m_hFence, m_FenceValue++);
293+
WaitForSignal(ResCommandList.Queue, m_pFence, m_hFence.Get(), m_FenceValue++);
294294
}
295295

296296
void ShaderOpTest::CreateCommandList() {
@@ -423,10 +423,7 @@ void ShaderOpTest::CreateDevice() {
423423
CHECK_HR(m_pDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE,
424424
__uuidof(ID3D12Fence), (void **)&m_pFence));
425425
m_pFence->SetName(L"ShaderOpTest Fence");
426-
m_hFence = CreateEvent(nullptr, FALSE, FALSE, nullptr);
427-
if (m_hFence == nullptr) {
428-
AtlThrow(HRESULT_FROM_WIN32(GetLastError()));
429-
}
426+
m_hFence.Init();
430427
}
431428

432429
static void InitByteCode(D3D12_SHADER_BYTECODE *pBytecode, ID3D10Blob *pBlob) {
@@ -749,7 +746,7 @@ void ShaderOpTest::CreateResources() {
749746

750747
CHECK_HR(pList->Close());
751748
ExecuteCommandList(ResCommandList.Queue, pList);
752-
WaitForSignal(ResCommandList.Queue, m_pFence, m_hFence, m_FenceValue++);
749+
WaitForSignal(ResCommandList.Queue, m_pFence, m_hFence.Get(), m_FenceValue++);
753750
}
754751

755752
void ShaderOpTest::CreateRootSignature() {
@@ -1054,7 +1051,7 @@ void ShaderOpTest::RunCommandList() {
10541051
}
10551052
CHECK_HR(pList->Close());
10561053
ExecuteCommandList(m_CommandList.Queue, pList);
1057-
WaitForSignal(m_CommandList.Queue, m_pFence, m_hFence, m_FenceValue++);
1054+
WaitForSignal(m_CommandList.Queue, m_pFence, m_hFence.Get(), m_FenceValue++);
10581055
}
10591056

10601057
void ShaderOpTest::RunShaderOp(ShaderOp *pShaderOp) {
@@ -1212,7 +1209,7 @@ void ShaderOpTest::PresentRenderTarget(ShaderOp *pShaderOp,
12121209
D3D12_RESOURCE_STATE_PRESENT);
12131210
pList->Close();
12141211
ExecuteCommandList(ResCommandList.Queue, pList);
1215-
WaitForSignal(ResCommandList.Queue, m_pFence, m_hFence, m_FenceValue++);
1212+
WaitForSignal(ResCommandList.Queue, m_pFence, m_hFence.Get(), m_FenceValue++);
12161213
}
12171214

12181215
ShaderOp *ShaderOpSet::GetShaderOp(LPCSTR pName) {

tools/clang/unittests/HLSLExec/ShaderOpTest.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <set>
2525
#include <unordered_set>
2626
#include <vector>
27+
#include "dxc/Support/RAIIClasses.h" // For HhEvent
2728

2829
// We need to keep & fix these warnings to integrate smoothly with HLK
2930
#pragma warning(error : 4100 4146 4242 4244 4267 4701 4389)
@@ -312,7 +313,7 @@ class ShaderOpTest {
312313
CComPtr<ID3D12Resource> m_pQueryBuffer;
313314
dxc::DxcDllSupport *m_pDxcSupport = nullptr;
314315
CommandListRefs m_CommandList;
315-
HANDLE m_hFence;
316+
HhEvent m_hFence;
316317
ShaderOp *m_pShaderOp;
317318
UINT64 m_FenceValue;
318319
std::map<LPCSTR, CComPtr<ID3D10Blob>> m_Shaders;

tools/clang/unittests/HLSLHost/HLSLHost.cpp

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "dxc/Support/Global.h"
1414
#include "dxc/Support/WinIncludes.h"
1515
#include "dxc/Support/microcom.h"
16+
#include "dxc/Support/RAIIClasses.h" // RAII Helpers
1617
#include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH
1718
#include <algorithm>
1819
#include <atlcoll.h>
@@ -38,88 +39,6 @@ Pending work for rendering hosting.
3839
// Forward declarations.
3940
class ServerFactory;
4041

41-
// RAII helpers.
42-
class HhEvent {
43-
public:
44-
HANDLE m_handle = INVALID_HANDLE_VALUE;
45-
46-
public:
47-
HRESULT Init() {
48-
if (m_handle == INVALID_HANDLE_VALUE) {
49-
m_handle = CreateEvent(nullptr, TRUE, FALSE, nullptr);
50-
if (m_handle == INVALID_HANDLE_VALUE) {
51-
return HRESULT_FROM_WIN32(GetLastError());
52-
}
53-
}
54-
return S_OK;
55-
}
56-
void SetEvent() { ::SetEvent(m_handle); }
57-
void ResetEvent() { ::ResetEvent(m_handle); }
58-
~HhEvent() {
59-
if (m_handle != INVALID_HANDLE_VALUE) {
60-
CloseHandle(m_handle);
61-
}
62-
}
63-
};
64-
65-
class HhCriticalSection {
66-
private:
67-
CRITICAL_SECTION m_cs;
68-
69-
public:
70-
HhCriticalSection() { InitializeCriticalSection(&m_cs); }
71-
~HhCriticalSection() { DeleteCriticalSection(&m_cs); }
72-
class Lock {
73-
private:
74-
CRITICAL_SECTION *m_pLock;
75-
76-
public:
77-
Lock() = delete;
78-
Lock(const Lock &) = delete;
79-
Lock(Lock &&other) { std::swap(m_pLock, other.m_pLock); }
80-
Lock(CRITICAL_SECTION *pLock) : m_pLock(pLock) {
81-
EnterCriticalSection(m_pLock);
82-
}
83-
~Lock() {
84-
if (m_pLock)
85-
LeaveCriticalSection(m_pLock);
86-
}
87-
};
88-
Lock LockCS() { return Lock(&m_cs); }
89-
};
90-
91-
class ClassObjectRegistration {
92-
private:
93-
DWORD m_reg = 0;
94-
HRESULT m_hr = E_FAIL;
95-
96-
public:
97-
HRESULT Register(REFCLSID rclsid, IUnknown *pUnk, DWORD dwClsContext,
98-
DWORD flags) {
99-
m_hr = CoRegisterClassObject(rclsid, pUnk, dwClsContext, flags, &m_reg);
100-
return m_hr;
101-
}
102-
~ClassObjectRegistration() {
103-
if (SUCCEEDED(m_hr))
104-
CoRevokeClassObject(m_reg);
105-
}
106-
};
107-
108-
class CoInit {
109-
private:
110-
HRESULT m_hr = E_FAIL;
111-
112-
public:
113-
HRESULT Initialize(DWORD dwCoInit) {
114-
m_hr = CoInitializeEx(nullptr, dwCoInit);
115-
return m_hr;
116-
}
117-
~CoInit() {
118-
if (SUCCEEDED(m_hr))
119-
CoUninitialize();
120-
}
121-
};
122-
12342
// Globals.
12443
static const GUID
12544
CLSID_HLSLHostServer = // {7FD7A859-6C6B-4352-8F1E-C67BB62E774B}

0 commit comments

Comments
 (0)