aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/thread.cpp
blob: 9e3486e4914b29e18d0d60606cda94ce68f61975 (plain) (blame)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zencore/thread.h>

#include <zencore/except.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/scopeguard.h>
#include <zencore/string.h>
#include <zencore/testing.h>
#include <zencore/trace.h>

#include <thread>

#if ZEN_PLATFORM_LINUX
#	if !defined(_GNU_SOURCE)
#		define _GNU_SOURCE	 // for semtimedop()
#	endif
#endif

#if !ZEN_USE_WINDOWS_EVENTS
#	include <chrono>
#	include <condition_variable>
#	include <mutex>
#endif

#if ZEN_PLATFORM_WINDOWS
#	include <zencore/windows.h>
#else
#	include <fcntl.h>
#	include <pthread.h>
#	include <signal.h>
#	include <sys/file.h>
#	include <sys/sem.h>
#	include <sys/stat.h>
#	include <sys/syscall.h>
#	include <sys/wait.h>
#	include <time.h>
#	include <unistd.h>
#endif

ZEN_THIRD_PARTY_INCLUDES_START
#include <fmt/format.h>
ZEN_THIRD_PARTY_INCLUDES_END

namespace zen {

static unsigned int LimitConcurrency = 0;

void
LimitHardwareConcurrency(unsigned int Limit)
{
	LimitConcurrency = Limit;
}

unsigned int
GetHardwareConcurrency()
{
	static unsigned int SysLimit = std::thread::hardware_concurrency();

	if (LimitConcurrency)
	{
		return Min(SysLimit, LimitConcurrency);
	}

	return SysLimit;
}

#if ZEN_PLATFORM_WINDOWS
// The information on how to set the thread name comes from
// a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx
const DWORD kVCThreadNameException = 0x406D1388;
typedef struct tagTHREADNAME_INFO
{
	DWORD  dwType;		// Must be 0x1000.
	LPCSTR szName;		// Pointer to name (in user addr space).
	DWORD  dwThreadID;	// Thread ID (-1=caller thread).
	DWORD  dwFlags;		// Reserved for future use, must be zero.
} THREADNAME_INFO;
// The SetThreadDescription API was brought in version 1607 of Windows 10.
typedef HRESULT(WINAPI* SetThreadDescription)(HANDLE hThread, PCWSTR lpThreadDescription);
// This function has try handling, so it is separated out of its caller.
void
SetNameInternal(DWORD thread_id, const char* name)
{
	THREADNAME_INFO info;
	info.dwType		= 0x1000;
	info.szName		= name;
	info.dwThreadID = thread_id;
	info.dwFlags	= 0;
	__try
	{
		RaiseException(kVCThreadNameException, 0, sizeof(info) / sizeof(DWORD), reinterpret_cast<DWORD_PTR*>(&info));
	}
	__except (EXCEPTION_CONTINUE_EXECUTION)
	{
	}
}
#endif

void
SetCurrentThreadName([[maybe_unused]] std::string_view ThreadName)
{
	constexpr std::string_view::size_type  MaxThreadNameLength = 255;
	std::string_view					   LimitedThreadName   = ThreadName.substr(0, MaxThreadNameLength);
	StringBuilder<MaxThreadNameLength + 1> ThreadNameZ;
	ThreadNameZ << LimitedThreadName;
	const int ThreadId = GetCurrentThreadId();

#if ZEN_WITH_TRACE
	trace::ThreadRegister(ThreadNameZ.c_str(), /* system id */ ThreadId, /* sort id */ 0);
#endif	// ZEN_WITH_TRACE

#if ZEN_PLATFORM_WINDOWS
	// The SetThreadDescription API works even if no debugger is attached.
	static auto SetThreadDescriptionFunc =
		reinterpret_cast<SetThreadDescription>(::GetProcAddress(::GetModuleHandle(L"Kernel32.dll"), "SetThreadDescription"));

	if (SetThreadDescriptionFunc)
	{
		WideStringBuilder<MaxThreadNameLength + 1> ThreadNameW;
		Utf8ToWide(LimitedThreadName, ThreadNameW);

		SetThreadDescriptionFunc(::GetCurrentThread(), ThreadNameW.c_str());
	}

	// The debugger needs to be around to catch the name in the exception.  If
	// there isn't a debugger, we are just needlessly throwing an exception.
	if (::IsDebuggerPresent())
	{
		SetNameInternal(ThreadId, ThreadNameZ.c_str());
	}
#elif ZEN_PLATFORM_MAC
	pthread_setname_np(ThreadNameZ.c_str());
#else
	pthread_setname_np(pthread_self(), ThreadNameZ.c_str());
#endif
}  // namespace zen

void
RwLock::AcquireShared() noexcept
{
	m_Mutex.lock_shared();
}

void
RwLock::ReleaseShared() noexcept
{
	m_Mutex.unlock_shared();
}

void
RwLock::AcquireExclusive() noexcept
{
	m_Mutex.lock();
}

void
RwLock::ReleaseExclusive() noexcept
{
	m_Mutex.unlock();
}

//////////////////////////////////////////////////////////////////////////

#if !ZEN_USE_WINDOWS_EVENTS
struct EventInner
{
	std::mutex				Mutex;
	std::condition_variable CondVar;
	std::atomic_bool		bSet{false};
};
#endif	// !ZEN_PLATFORM_WINDOWS

Event::Event()
{
	bool bManualReset  = true;
	bool bInitialState = false;

#if ZEN_USE_WINDOWS_EVENTS
	m_EventHandle = CreateEvent(nullptr, bManualReset, bInitialState, nullptr);
#else
	ZEN_UNUSED(bManualReset);
	auto*			 Inner = new EventInner();
	std::unique_lock Lock(Inner->Mutex);
	Inner->bSet	  = bInitialState;
	m_EventHandle = Inner;
#endif
}

Event::~Event()
{
	Close();
}

void
Event::Set()
{
#if ZEN_USE_WINDOWS_EVENTS
	SetEvent(m_EventHandle);
#else
	std::atomic_thread_fence(std::memory_order_acquire);
	auto* Inner = (EventInner*)m_EventHandle.load();
	{
		std::unique_lock Lock(Inner->Mutex);
		Inner->bSet.store(true);
		Inner->CondVar.notify_all();
	}
#endif
}

void
Event::Reset()
{
#if ZEN_USE_WINDOWS_EVENTS
	ResetEvent(m_EventHandle);
#else
	std::atomic_thread_fence(std::memory_order_acquire);
	auto* Inner = (EventInner*)m_EventHandle.load();
	{
		std::unique_lock Lock(Inner->Mutex);
		Inner->bSet.store(false);
	}
#endif
}

void
Event::Close()
{
#if ZEN_USE_WINDOWS_EVENTS
	CloseHandle(m_EventHandle);
	m_EventHandle = nullptr;
#else
	std::atomic_thread_fence(std::memory_order_acquire);
	auto* Inner = (EventInner*)m_EventHandle.load();
	{
		std::unique_lock Lock(Inner->Mutex);
		Inner->bSet.store(true);
		m_EventHandle = nullptr;
	}
	delete Inner;
#endif
}

bool
Event::Wait(int TimeoutMs)
{
#if ZEN_USE_WINDOWS_EVENTS
	using namespace std::literals;

	const DWORD Timeout = (TimeoutMs < 0) ? INFINITE : TimeoutMs;

	DWORD Result = WaitForSingleObject(m_EventHandle, Timeout);

	if (Result == WAIT_FAILED)
	{
		zen::ThrowLastError("Event wait failed"sv);
	}

	return (Result == WAIT_OBJECT_0);
#else
	std::atomic_thread_fence(std::memory_order_acquire);
	auto* Inner = reinterpret_cast<EventInner*>(m_EventHandle.load());

	if (Inner->bSet.load())
	{
		return true;
	}

	if (TimeoutMs >= 0)
	{
		std::unique_lock Lock(Inner->Mutex);

		if (Inner->bSet.load())
		{
			return true;
		}

		return Inner->CondVar.wait_for(Lock, std::chrono::milliseconds(TimeoutMs), [&] { return Inner->bSet.load(); });
	}

	// Infinite wait. This does not actually call the wait() function to work around
	// an apparent issue in the underlying implementation.

	std::unique_lock Lock(Inner->Mutex);

	if (!Inner->bSet.load())
	{
		while (!Inner->CondVar.wait_for(Lock, std::chrono::milliseconds(1000), [&] { return Inner->bSet.load(); }))
			;
	}

	return true;
#endif
}

//////////////////////////////////////////////////////////////////////////

NamedEvent::NamedEvent(std::string_view EventName)
{
#if ZEN_PLATFORM_WINDOWS
	using namespace std::literals;

	ExtendableStringBuilder<64> Name;
	Name << "Local\\"sv;
	Name << EventName;

	m_EventHandle = CreateEventA(nullptr, true, false, Name.c_str());
#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
	// Create a file to back the semaphore
	ExtendableStringBuilder<64> EventPath;
	EventPath << "/tmp/" << EventName;

	int Fd = open(EventPath.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0666);
	if (Fd < 0)
	{
		ThrowLastError(fmt::format("Failed to create '{}' for named event", EventPath));
	}
	fchmod(Fd, 0666);

	// Use the file path to generate an IPC key
	key_t IpcKey = ftok(EventPath.c_str(), 1);
	if (IpcKey < 0)
	{
		close(Fd);
		ThrowLastError("Failed to create an SysV IPC key");
	}

	// Use the key to create/open the semaphore
	int Sem = semget(IpcKey, 1, 0600 | IPC_CREAT);
	if (Sem < 0)
	{
		close(Fd);
		ThrowLastError("Failed creating an SysV semaphore");
	}

	// Atomically claim ownership of the semaphore's key. The owner initialises
	// the semaphore to 1 so we can use the wait-for-zero op as that does not
	// modify the semaphore's value on a successful wait.
	int LockResult = flock(Fd, LOCK_EX | LOCK_NB);
	if (LockResult == 0)
	{
		// This isn't thread safe really. Another thread could open the same
		// semaphore and successfully wait on it in the period of time where
		// this comment is but before the semaphore's initialised.
		semctl(Sem, 0, SETVAL, 1);
	}

	// Pack into the handle
	static_assert(sizeof(Sem) + sizeof(Fd) <= sizeof(void*), "Semaphore packing assumptions not met");
	intptr_t Packed;
	Packed = intptr_t(Sem) << 32;
	Packed |= intptr_t(Fd) & 0xffff'ffff;
	m_EventHandle = (void*)Packed;
#endif
	ZEN_ASSERT(m_EventHandle != nullptr);
}

NamedEvent::~NamedEvent()
{
	Close();
}

void
NamedEvent::Close()
{
	if (m_EventHandle == nullptr)
	{
		return;
	}

#if ZEN_PLATFORM_WINDOWS
	CloseHandle(m_EventHandle);
#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
	int Fd		  = int(intptr_t(m_EventHandle.load()) & 0xffff'ffff);

	if (flock(Fd, LOCK_EX | LOCK_NB) == 0)
	{
		std::error_code		  Ec;
		std::filesystem::path Name = PathFromHandle((void*)(intptr_t(Fd)), Ec);
		if (Ec)
		{
			ZEN_WARN("Error reported on get file path from handle {} for named event unlink operation, reason '{}'", Fd, Ec.message());
		}
		else
		{
			unlink(Name.c_str());
		}

		flock(Fd, LOCK_UN | LOCK_NB);
		close(Fd);

		int Sem = int(intptr_t(m_EventHandle.load()) >> 32);
		semctl(Sem, 0, IPC_RMID);
	}
#endif

	m_EventHandle = nullptr;
}

std::error_code
NamedEvent::Set()
{
	ZEN_ASSERT(m_EventHandle != nullptr);
#if ZEN_PLATFORM_WINDOWS
	if (!SetEvent(m_EventHandle))
	{
		return MakeErrorCodeFromLastError();
	}
#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
	int Sem = int(intptr_t(m_EventHandle.load()) >> 32);
	if (semctl(Sem, 0, SETVAL, 0) == -1)
	{
		return MakeErrorCodeFromLastError();
	}
#endif
	return {};
}

bool
NamedEvent::Wait(int TimeoutMs)
{
	ZEN_ASSERT(m_EventHandle != nullptr);
#if ZEN_PLATFORM_WINDOWS
	const DWORD Timeout = (TimeoutMs < 0) ? INFINITE : TimeoutMs;

	DWORD Result = WaitForSingleObject(m_EventHandle, Timeout);

	if (Result == WAIT_FAILED)
	{
		using namespace std::literals;
		zen::ThrowLastError("Event wait failed"sv);
	}

	return (Result == WAIT_OBJECT_0);
#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
	int Sem = int(intptr_t(m_EventHandle.load()) >> 32);

	int			  Result;
	struct sembuf SemOp = {};

	if (TimeoutMs < 0)
	{
		Result = semop(Sem, &SemOp, 1);
		return Result == 0;
	}

#	if defined(_GNU_SOURCE)
	const int		TimeoutSec	 = TimeoutMs / 1000;
	struct timespec TimeoutValue = {
		.tv_sec	 = TimeoutSec,
		.tv_nsec = (TimeoutMs - (TimeoutSec * 1000)) * 1000000,
	};
	Result = semtimedop(Sem, &SemOp, 1, &TimeoutValue);
#	else
	const int SleepTimeMs = 10;
	SemOp.sem_flg		  = IPC_NOWAIT;
	do
	{
		Result = semop(Sem, &SemOp, 1);
		if (Result == 0 || errno != EAGAIN)
		{
			break;
		}

		Sleep(SleepTimeMs);
		TimeoutMs -= SleepTimeMs;
	} while (TimeoutMs > 0);
#	endif	// _GNU_SOURCE
	return Result == 0;
#endif
}

//////////////////////////////////////////////////////////////////////////

NamedMutex::~NamedMutex()
{
#if ZEN_PLATFORM_WINDOWS
	if (m_MutexHandle)
	{
		CloseHandle(m_MutexHandle);
	}
#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
	int Inner = int(intptr_t(m_MutexHandle));
	flock(Inner, LOCK_UN);
	close(Inner);
#endif
}

bool
NamedMutex::Create(std::string_view MutexName)
{
#if ZEN_PLATFORM_WINDOWS
	ZEN_ASSERT(m_MutexHandle == nullptr);

	using namespace std::literals;

	ExtendableStringBuilder<64> Name;
	Name << "Global\\"sv;
	Name << MutexName;

	m_MutexHandle = CreateMutexA(nullptr, /* InitialOwner */ TRUE, Name.c_str());

	return !!m_MutexHandle;
#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
	ExtendableStringBuilder<64> Name;
	Name << "/tmp/" << MutexName;

	int Inner = open(Name.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, geteuid() == 0 ? 0766 : 0666);
	if (Inner < 0)
	{
		return false;
	}
	fchmod(Inner, 0666);

	if (flock(Inner, LOCK_EX) != 0)
	{
		close(Inner);
		Inner = 0;
		return false;
	}

	m_MutexHandle = (void*)(intptr_t(Inner));
	return true;
#endif	// ZEN_PLATFORM_WINDOWS
}

bool
NamedMutex::Exists(std::string_view MutexName)
{
#if ZEN_PLATFORM_WINDOWS
	using namespace std::literals;

	ExtendableStringBuilder<64> Name;
	Name << "Global\\"sv;
	Name << MutexName;

	void* MutexHandle = OpenMutexA(SYNCHRONIZE, /* InheritHandle */ FALSE, Name.c_str());

	if (MutexHandle == nullptr)
	{
		return false;
	}

	CloseHandle(MutexHandle);

	return true;
#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
	ExtendableStringBuilder<64> Name;
	Name << "/tmp/" << MutexName;

	bool bExists = false;
	int	 Fd		 = open(Name.c_str(), O_RDWR | O_CLOEXEC);
	if (Fd >= 0)
	{
		if (flock(Fd, LOCK_EX | LOCK_NB) == 0)
		{
			flock(Fd, LOCK_UN | LOCK_NB);
		}
		else
		{
			bExists = true;
		}
		close(Fd);
	}

	return bExists;
#endif	// ZEN_PLATFORM_WINDOWS
}

int
GetCurrentThreadId()
{
#if ZEN_PLATFORM_WINDOWS
	return ::GetCurrentThreadId();
#elif ZEN_PLATFORM_LINUX
	return int(syscall(SYS_gettid));
#elif ZEN_PLATFORM_MAC
	return int(pthread_mach_thread_np(pthread_self()));
#endif
}

void
Sleep(int ms)
{
#if ZEN_PLATFORM_WINDOWS
	::Sleep(ms);
#else
	usleep(ms * 1000U);
#endif
}

//////////////////////////////////////////////////////////////////////////
//
// Testing related code follows...
//

#if ZEN_WITH_TESTS

void
thread_forcelink()
{
}

TEST_SUITE_BEGIN("core.thread");

TEST_CASE("GetCurrentThreadId")
{
	CHECK_FALSE(GetCurrentThreadId() == 0);
}

TEST_CASE("NamedEvent")
{
	std::string Name = "zencore_test_event";
	NamedEvent	TestEvent(Name);

	// Timeout test
	for (uint32_t i = 0; i < 8; ++i)
	{
		bool bEventSet = TestEvent.Wait(10);
		CHECK(!bEventSet);
	}

	NamedEvent ReadyEvent(Name + "_ready");

	// Thread check
	std::thread Waiter = std::thread([Name]() {
		NamedEvent ReadyEvent(Name + "_ready");
		ReadyEvent.Set();

		NamedEvent TestEvent(Name);
		TestEvent.Wait(100);
	});

	ReadyEvent.Wait();

	zen::Sleep(10);
	TestEvent.Set();

	Waiter.join();

	// Manual reset property
	for (uint32_t i = 0; i < 8; ++i)
	{
		bool bEventSet = TestEvent.Wait(10);
		CHECK(bEventSet);
	}
}

TEST_CASE("NamedMutex")
{
	static const char* Name = "zen_test_mutex";

	CHECK(!NamedMutex::Exists(Name));

	{
		NamedMutex TestMutex;
		CHECK(TestMutex.Create(Name));
		CHECK(NamedMutex::Exists(Name));
	}

	CHECK(!NamedMutex::Exists(Name));
}

TEST_SUITE_END();

#endif	// ZEN_WITH_TESTS

}  // namespace zen