-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibXPUInfo_IPC.h
215 lines (195 loc) · 6 KB
/
LibXPUInfo_IPC.h
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
// Utility code for using XPUInfo out-of-process via Inter-Process Communication (IPC)
//
// NOTE: These IPC classes are intended to provide some improved readability, structure,
// and exception-safety over plain C calling the OS-provided IPC APIs directly. It is the
// APPLICATION'S responsibility to implement IPC in a way that is free of race conditions,
// deadlocks, etc.
#pragma once
#ifdef XPUINFO_USE_IPC
#include "LibXPUInfo.h"
#ifdef _WIN32
#include <Windows.h>
#endif
#pragma warning(push)
#pragma warning(disable : 4251)
namespace XI
{
#ifdef _WIN32
namespace Win
{
// Wrapper for use with CreateProcess
class XPUINFO_EXPORT ProcessInformation : public PROCESS_INFORMATION, NoCopyAssign
{
public:
ProcessInformation() : PROCESS_INFORMATION{} {} // zero-initialize
~ProcessInformation(); // Close non-zero handles
};
class XPUINFO_EXPORT NamedEvent: public NoCopyAssign
{
public:
NamedEvent(const char* sharedName);
void Set()
{
SetEvent(m_hEvent);
}
UI32 Wait(UI32 timeout = INFINITE)
{
return WaitForSingleObject(m_hEvent, timeout);
}
~NamedEvent()
{
if (m_hEvent)
{
CloseHandle(m_hEvent);
}
}
protected:
HANDLE m_hEvent;
};
class XPUINFO_EXPORT NamedMutex : public NoCopyAssign
{
public:
NamedMutex(const char* sharedName);
~NamedMutex()
{
if (m_hMutex)
{
CloseHandle(m_hMutex);
}
}
class ScopedLock : public NoCopyAssign
{
public:
ScopedLock(NamedMutex& m, UI32 timeout = INFINITE, UI32* pResult = nullptr);
~ScopedLock()
{
if (m_Result != WAIT_FAILED)
{
ReleaseMutex(m_Mutex.m_hMutex);
}
}
protected:
NamedMutex& m_Mutex;
UI32 m_Result;
UI32* m_pOutResult;
};
friend class ScopedLock;
protected:
HANDLE m_hMutex;
UI32 m_CreateError;
};
class XPUINFO_EXPORT NamedSemaphore : public NoCopyAssign
{
public:
NamedSemaphore(const char* name, const I32 initialCount);
~NamedSemaphore()
{
if (m_hSem)
{
CloseHandle(m_hSem);
}
}
friend class ScopedAcquire;
class ScopedAcquire
{
public:
ScopedAcquire(NamedSemaphore& sem, UI32 timeout = INFINITE);
~ScopedAcquire();
protected:
NamedSemaphore& m_sem;
UI32 m_Result;
};
protected:
HANDLE m_hSem;
UI32 m_CreateError;
};
class XPUINFO_EXPORT NamedSharedMemory : public NoCopyAssign
{
public:
NamedSharedMemory(size_t size, const char* sharedName, bool bReadOnlyAccess = false);
~NamedSharedMemory();
UI32 getStatus() const { return m_Status; }
NamedMutex& getMutex() { return m_hMutex; }
void* getSharedMemPtr() {
XPUINFO_REQUIRE(m_pMappedMemory);
return m_pMappedMemory;
}
size_t size() const { return m_Size; }
protected:
const size_t m_Size;
HANDLE m_hSharedMemory;
NamedMutex m_hMutex;
void* m_pMappedMemory = nullptr;
UI32 m_Status = UI32(-1);
};
class XPUINFO_EXPORT NamedPipe : public NoCopyAssign
{
public:
NamedPipe(const String& pipeName, // Must have format "\\\\.\\pipe\\Name"
const String& mutexName, // Must NOT begin with "\\\\.\\pipe"
size_t bufferSize,
bool isServer = false);
~NamedPipe()
{
if (Valid())
{
XPUINFO_REQUIRE(Disconnect());
XPUINFO_REQUIRE(CloseHandle(m_hPipe));
}
}
bool Valid() const { return (m_hPipe != INVALID_HANDLE_VALUE) && (m_hPipe != 0); }
bool Connected() const { return Valid() && (!m_bServer || m_bConnected); }
bool Connect();
bool Disconnect();
template <typename T>
bool Read(T& value)
{
XPUINFO_REQUIRE(Connected());
DWORD bytesRead = 0;
return ((ReadFile(m_hPipe, &value, sizeof(value), &bytesRead, nullptr) != FALSE) &&
(bytesRead == sizeof(value)));
}
template <>
bool Read(String& outString)
{
XPUINFO_REQUIRE(Connected());
outString.resize(m_bufferSize);
DWORD bytesRead = 0;
BOOL bRet = ReadFile(m_hPipe, outString.data(), (DWORD)outString.size(), &bytesRead, nullptr);
outString[std::min(bytesRead, DWORD(m_bufferSize - 1))] = 0; // Just in case
outString.resize(bytesRead);
return !!bRet;
}
template <typename T>
bool Write(const T& value)
{
XPUINFO_REQUIRE(Connected());
DWORD bytesWritten = 0;
return ((WriteFile(m_hPipe, &value, sizeof(value), &bytesWritten, nullptr) != FALSE) &&
(bytesWritten == sizeof(value)));
}
template <>
bool Write(const String& inString)
{
XPUINFO_REQUIRE(Connected());
DWORD bytesWritten = 0;
BOOL bRet = WriteFile(m_hPipe, inString.data(),
(DWORD)std::min(inString.length(), m_bufferSize),
&bytesWritten, nullptr);
return bRet && (bytesWritten == inString.length()); // Error if string longer than buffer
}
protected:
HANDLE m_hPipe;
bool m_bConnected = false;
const bool m_bServer;
const size_t m_bufferSize;
NamedMutex m_Mutex;
std::unique_ptr<NamedMutex::ScopedLock> m_pLock;
};
} // Win
#endif // _WIN32
} // XI
#pragma warning(pop)
#endif // XPUINFO_USE_IPC