aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/thread.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/zencore/thread.cpp')
-rw-r--r--src/zencore/thread.cpp16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/zencore/thread.cpp b/src/zencore/thread.cpp
index 067e66c0d..1f4004373 100644
--- a/src/zencore/thread.cpp
+++ b/src/zencore/thread.cpp
@@ -99,7 +99,7 @@ SetNameInternal(DWORD thread_id, const char* name)
#endif
void
-SetCurrentThreadName([[maybe_unused]] std::string_view ThreadName)
+SetCurrentThreadName([[maybe_unused]] std::string_view ThreadName, [[maybe_unused]] int32_t SortHint)
{
constexpr std::string_view::size_type MaxThreadNameLength = 255;
std::string_view LimitedThreadName = ThreadName.substr(0, MaxThreadNameLength);
@@ -108,7 +108,7 @@ SetCurrentThreadName([[maybe_unused]] std::string_view ThreadName)
const int ThreadId = GetCurrentThreadId();
#if ZEN_WITH_TRACE
- trace::ThreadRegister(ThreadNameZ.c_str(), /* system id */ ThreadId, /* sort id */ 0);
+ trace::ThreadRegister(ThreadNameZ.c_str(), /* system id */ ThreadId, /* sort id */ SortHint);
#endif // ZEN_WITH_TRACE
#if ZEN_PLATFORM_WINDOWS
@@ -336,13 +336,17 @@ NamedEvent::NamedEvent(std::string_view EventName)
}
fchmod(Fd, 0666);
- // Use the file path to generate an IPC key
- key_t IpcKey = ftok(EventPath.c_str(), 1);
- if (IpcKey < 0)
+ // Derive the IPC key via fstat() on the fd instead of ftok() on the path:
+ // another owner's destructor can unlink the backing file between our open()
+ // and ftok(), in which case ftok's internal stat() fails with ENOENT. The
+ // formula below matches what glibc/macOS ftok() compute internally.
+ struct stat IpcStat;
+ if (fstat(Fd, &IpcStat) < 0)
{
close(Fd);
- ThrowLastError("Failed to create an SysV IPC key");
+ ThrowLastError("Failed to stat backing file for SysV IPC key");
}
+ const key_t IpcKey = key_t((IpcStat.st_ino & 0xffff) | ((IpcStat.st_dev & 0xff) << 16) | (1 << 24));
// Use the key to create/open the semaphore
int Sem = semget(IpcKey, 1, 0600 | IPC_CREAT);