forked from bkaradzic/bx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_test.cpp
75 lines (53 loc) · 1.32 KB
/
thread_test.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
/*
* Copyright 2010-2023 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bx/blob/master/LICENSE
*/
#include "test.h"
#include <bx/thread.h>
#if BX_CONFIG_SUPPORTS_THREADING
bx::DefaultAllocator s_allocator;
bx::MpScUnboundedBlockingQueue<void> s_mpsc(&s_allocator);
int32_t threadExit0(bx::Thread* _thread, void*)
{
_thread->pop();
s_mpsc.push(reinterpret_cast<void*>(uintptr_t(0x1300) ) );
return bx::kExitSuccess;
}
int32_t threadExit1(bx::Thread* _thread, void*)
{
BX_UNUSED(_thread);
s_mpsc.push(reinterpret_cast<void*>(uintptr_t(0x89) ) );
return bx::kExitFailure;
}
TEST_CASE("Semaphore", "")
{
bx::Semaphore sem;
REQUIRE(!sem.wait(10) );
sem.post(1);
REQUIRE(sem.wait() );
}
TEST_CASE("Thread", "")
{
bx::Thread th;
REQUIRE(!th.isRunning() );
bool init = th.init(threadExit0, NULL, 0, NULL);
REQUIRE(init);
REQUIRE(th.isRunning() );
th.push(NULL);
th.shutdown();
REQUIRE(!th.isRunning() );
REQUIRE(th.getExitCode() == 0);
th.init(threadExit1);
REQUIRE(th.isRunning() );
th.shutdown();
REQUIRE(!th.isRunning() );
REQUIRE(th.getExitCode() == 1);
}
TEST_CASE("MpScUnboundedBlockingQueue", "")
{
void* p0 = s_mpsc.pop();
void* p1 = s_mpsc.pop();
uintptr_t result = uintptr_t(p0) | uintptr_t(p1);
REQUIRE(result == 0x1389);
}
#endif // BX_CONFIG_SUPPORTS_THREADING