1#include <gtest/gtest.h>
2
3#include <base/lock.h>
4#include <base/system.h>
5#include <base/tl/threading.h>
6
7static void Nothing(void *pUser)
8{
9 (void)pUser;
10}
11
12TEST(Thread, Detach)
13{
14 void *pThread = thread_init(threadfunc: Nothing, user: 0, name: "detach");
15 thread_detach(thread: pThread);
16}
17
18static void SetToOne(void *pUser)
19{
20 *(int *)pUser = 1;
21}
22
23TEST(Thread, Wait)
24{
25 int Integer = 0;
26 void *pThread = thread_init(threadfunc: SetToOne, user: &Integer, name: "wait");
27 thread_wait(thread: pThread);
28 EXPECT_EQ(Integer, 1);
29}
30
31TEST(Thread, Yield)
32{
33 thread_yield();
34}
35
36TEST(Thread, Semaphore)
37{
38 SEMAPHORE Semaphore;
39 sphore_init(sem: &Semaphore);
40 sphore_destroy(sem: &Semaphore);
41}
42
43TEST(Thread, SemaphoreSingleThreaded)
44{
45 SEMAPHORE Semaphore;
46 sphore_init(sem: &Semaphore);
47 sphore_signal(sem: &Semaphore);
48 sphore_signal(sem: &Semaphore);
49 sphore_wait(sem: &Semaphore);
50 sphore_wait(sem: &Semaphore);
51 sphore_destroy(sem: &Semaphore);
52}
53
54TEST(Thread, SemaphoreWrapperSingleThreaded)
55{
56 CSemaphore Semaphore;
57 EXPECT_EQ(Semaphore.GetApproximateValue(), 0);
58 Semaphore.Signal();
59 EXPECT_EQ(Semaphore.GetApproximateValue(), 1);
60 Semaphore.Signal();
61 EXPECT_EQ(Semaphore.GetApproximateValue(), 2);
62 Semaphore.Wait();
63 EXPECT_EQ(Semaphore.GetApproximateValue(), 1);
64 Semaphore.Wait();
65 EXPECT_EQ(Semaphore.GetApproximateValue(), 0);
66}
67
68static void SemaphoreThread(void *pUser)
69{
70 SEMAPHORE *pSemaphore = (SEMAPHORE *)pUser;
71 sphore_wait(sem: pSemaphore);
72}
73
74TEST(Thread, SemaphoreMultiThreaded)
75{
76 SEMAPHORE Semaphore;
77 sphore_init(sem: &Semaphore);
78 sphore_signal(sem: &Semaphore);
79 void *pThread = thread_init(threadfunc: SemaphoreThread, user: &Semaphore, name: "semaphore");
80 thread_wait(thread: pThread);
81 sphore_destroy(sem: &Semaphore);
82}
83
84static void LockThread(void *pUser)
85{
86 CLock *pLock = (CLock *)pUser;
87 pLock->lock();
88 pLock->unlock();
89}
90
91TEST(Thread, Lock)
92{
93 CLock Lock;
94 Lock.lock();
95 void *pThread = thread_init(threadfunc: LockThread, user: &Lock, name: "lock");
96 Lock.unlock();
97 thread_wait(thread: pThread);
98}
99