-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibXPUInfo_IPC.cpp
238 lines (221 loc) · 6.49 KB
/
LibXPUInfo_IPC.cpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
// Utility code for using XPUInfo out-of-process
#ifdef XPUINFO_USE_IPC
#include "LibXPUInfo_IPC.h"
#define OPEN_FILE_MAPPING_ERROR ((DWORD)0xC00007D0L)
#define UNABLE_MAP_VIEW_OF_FILE ((DWORD)0xC00007D1L)
namespace XI
{
#ifdef _WIN32
namespace Win
{
ProcessInformation::~ProcessInformation()
{
if (hProcess)
{
CloseHandle(hProcess);
}
if (hThread)
{
CloseHandle(hThread);
}
}
NamedEvent::NamedEvent(const char* sharedName)
{
m_hEvent = CreateEventA(nullptr, FALSE, FALSE, sharedName);
XPUINFO_REQUIRE(m_hEvent);
}
NamedMutex::NamedMutex(const char* sharedName)
{
m_hMutex = CreateMutexA(nullptr, FALSE, sharedName);
m_CreateError = GetLastError();
XPUINFO_REQUIRE(m_hMutex);
}
NamedMutex::ScopedLock::ScopedLock(NamedMutex& m, UI32 timeout, UI32* pResult)
:
m_Mutex(m), m_pOutResult(pResult)
{
m_Result = WaitForSingleObject(m.m_hMutex, timeout);
if (m_pOutResult)
{
*m_pOutResult = m_Result;
}
}
NamedSemaphore::NamedSemaphore(const char* name, const I32 initialCount)
{
m_hSem = CreateSemaphoreA(nullptr, initialCount, initialCount, name);
m_CreateError = GetLastError();
}
NamedSemaphore::ScopedAcquire::ScopedAcquire(NamedSemaphore& sem, UI32 timeout) :
m_sem(sem)
{
m_Result = WaitForSingleObject(sem.m_hSem, timeout);
}
NamedSemaphore::ScopedAcquire::~ScopedAcquire()
{
if (m_Result == WAIT_OBJECT_0)
{
UI32 res = ReleaseSemaphore(m_sem.m_hSem, 1, nullptr);
XPUINFO_REQUIRE(res);
}
}
NamedSharedMemory::NamedSharedMemory(size_t size, const char* sharedName, bool bReadOnlyAccess) :
m_Size(size),
m_hMutex((std::string(sharedName) + "_MUTEX").c_str())
{
static_assert(sizeof(size_t) == 8, "Assuming 64-bit");
m_hSharedMemory = CreateFileMappingA(
(HANDLE)INVALID_HANDLE_VALUE,
nullptr, // no security
PAGE_READWRITE, // to allow read & write access
(DWORD)(size >> 32),
(DWORD)size, // file size
sharedName); // object name
DWORD MemStatus = GetLastError(); // to see if this is the first opening
if (m_hSharedMemory == nullptr)
{
m_Status = OPEN_FILE_MAPPING_ERROR;
// this is fatal, if we can't get data then there's no
// point in continuing.
}
else
{
NamedMutex::ScopedLock lock(m_hMutex);
if (MemStatus != ERROR_ALREADY_EXISTS)
{
// this is the first access to the file so initialize the
// instance count
m_pMappedMemory = MapViewOfFile(
m_hSharedMemory, // shared mem handle
FILE_MAP_WRITE, // access desired
0, // starting offset
0,
0); // map the entire object
if (m_pMappedMemory != nullptr)
{
// if here, then pdwInstanceCount should be valid
// so initialize the shared memory structure
// clear memory block
memset(m_pMappedMemory, 0, size);
m_Status = ERROR_SUCCESS;
}
else
{
m_Status = UNABLE_MAP_VIEW_OF_FILE;
}
}
else
{
// the status is ERROR_ALREADY_EXISTS which is successful
m_Status = ERROR_SUCCESS;
}
// see if Read Only access is required
if (m_Status == ERROR_SUCCESS)
{
// by now the shared memory has already been initialized so
// we if we don't need write access any more or if it has
// already been opened, then open with the desired access
m_pMappedMemory = MapViewOfFile(
m_hSharedMemory, // shared mem handle
(bReadOnlyAccess ? FILE_MAP_READ :
FILE_MAP_WRITE), // access desired
0, // starting offset
0,
0); // map the entire object
if (m_pMappedMemory == nullptr)
{
m_Status = UNABLE_MAP_VIEW_OF_FILE;
// this is fatal, if we can't get data then there's no
// point in continuing.
}
else
{
m_Status = ERROR_SUCCESS;
}
}
}
}
NamedSharedMemory::~NamedSharedMemory()
{
if (ERROR_SUCCESS == m_Status)
{
if (m_pMappedMemory)
{
XPUINFO_REQUIRE(UnmapViewOfFile(m_pMappedMemory));
}
if (m_hSharedMemory)
{
XPUINFO_REQUIRE(CloseHandle(m_hSharedMemory));
}
}
}
NamedPipe::NamedPipe(const String& pipeName, // Must have format "\\\\.\\pipe\\Name"
const String& mutexName, // Must NOT begin with "\\\\.\\pipe"
size_t bufferSize,
bool isServer) : m_bServer(isServer),
m_bufferSize(bufferSize),
m_Mutex(mutexName.c_str())
{
XPUINFO_REQUIRE(m_bufferSize && (m_bufferSize < 0x100000000ULL)); // Check for 32-bit-safe size
if (!m_bServer)
{
// Client-scope lock used to prevent server exit before client completes
m_pLock.reset(new NamedMutex::ScopedLock(m_Mutex));
m_hPipe = CreateFileA(pipeName.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
nullptr,
OPEN_EXISTING,
0,
nullptr);
}
else
{
// Create pipe
m_hPipe = CreateNamedPipeA(pipeName.c_str(),
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1, // Single instance
(DWORD)bufferSize,
(DWORD)bufferSize,
PIPE_WAIT,
nullptr);
}
}
bool NamedPipe::Connect()
{
if (!Valid())
{
return false;
}
if (!m_bConnected)
{
bool bRet = ConnectNamedPipe(m_hPipe, nullptr) ?
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
m_bConnected = bRet;
return bRet;
}
else
{
return true;
}
}
bool NamedPipe::Disconnect()
{
if (m_bConnected && m_bServer)
{
NamedMutex::ScopedLock lock(m_Mutex); // Client must be done first
bool bRet = !!DisconnectNamedPipe(m_hPipe);
m_bConnected = false; // Play it safe if disconnect failed
return bRet;
}
else
{
return true;
}
}
} // Win
#endif
} // XI
#endif //XPUINFO_USE_IPC