2017-10-09 22:08:24 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-11-10 20:15:51 +00:00
|
|
|
#include <base/lock.h>
|
2017-10-09 22:08:24 +00:00
|
|
|
#include <base/system.h>
|
2022-10-23 14:07:25 +00:00
|
|
|
#include <base/tl/threading.h>
|
2017-10-09 22:08:24 +00:00
|
|
|
|
|
|
|
static void Nothing(void *pUser)
|
|
|
|
{
|
|
|
|
(void)pUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Thread, Detach)
|
|
|
|
{
|
2019-03-19 10:44:16 +00:00
|
|
|
void *pThread = thread_init(Nothing, 0, "detach");
|
2017-10-09 22:08:24 +00:00
|
|
|
thread_detach(pThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetToOne(void *pUser)
|
|
|
|
{
|
|
|
|
*(int *)pUser = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Thread, Wait)
|
|
|
|
{
|
|
|
|
int Integer = 0;
|
2019-03-19 10:44:16 +00:00
|
|
|
void *pThread = thread_init(SetToOne, &Integer, "wait");
|
2017-10-09 22:08:24 +00:00
|
|
|
thread_wait(pThread);
|
|
|
|
EXPECT_EQ(Integer, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Thread, Yield)
|
|
|
|
{
|
|
|
|
thread_yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Thread, Semaphore)
|
|
|
|
{
|
|
|
|
SEMAPHORE Semaphore;
|
|
|
|
sphore_init(&Semaphore);
|
|
|
|
sphore_destroy(&Semaphore);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Thread, SemaphoreSingleThreaded)
|
|
|
|
{
|
|
|
|
SEMAPHORE Semaphore;
|
|
|
|
sphore_init(&Semaphore);
|
|
|
|
sphore_signal(&Semaphore);
|
|
|
|
sphore_signal(&Semaphore);
|
|
|
|
sphore_wait(&Semaphore);
|
|
|
|
sphore_wait(&Semaphore);
|
|
|
|
sphore_destroy(&Semaphore);
|
|
|
|
}
|
|
|
|
|
2022-10-23 14:07:25 +00:00
|
|
|
TEST(Thread, SemaphoreWrapperSingleThreaded)
|
|
|
|
{
|
|
|
|
CSemaphore Semaphore;
|
|
|
|
EXPECT_EQ(Semaphore.GetApproximateValue(), 0);
|
|
|
|
Semaphore.Signal();
|
|
|
|
EXPECT_EQ(Semaphore.GetApproximateValue(), 1);
|
|
|
|
Semaphore.Signal();
|
|
|
|
EXPECT_EQ(Semaphore.GetApproximateValue(), 2);
|
|
|
|
Semaphore.Wait();
|
|
|
|
EXPECT_EQ(Semaphore.GetApproximateValue(), 1);
|
|
|
|
Semaphore.Wait();
|
|
|
|
EXPECT_EQ(Semaphore.GetApproximateValue(), 0);
|
|
|
|
}
|
|
|
|
|
2017-10-09 22:08:24 +00:00
|
|
|
static void SemaphoreThread(void *pUser)
|
|
|
|
{
|
|
|
|
SEMAPHORE *pSemaphore = (SEMAPHORE *)pUser;
|
|
|
|
sphore_wait(pSemaphore);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Thread, SemaphoreMultiThreaded)
|
|
|
|
{
|
|
|
|
SEMAPHORE Semaphore;
|
|
|
|
sphore_init(&Semaphore);
|
|
|
|
sphore_signal(&Semaphore);
|
2019-03-19 10:44:16 +00:00
|
|
|
void *pThread = thread_init(SemaphoreThread, &Semaphore, "semaphore");
|
2017-10-09 22:08:24 +00:00
|
|
|
thread_wait(pThread);
|
|
|
|
sphore_destroy(&Semaphore);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void LockThread(void *pUser)
|
|
|
|
{
|
2023-11-10 20:15:51 +00:00
|
|
|
CLock *pLock = (CLock *)pUser;
|
|
|
|
pLock->lock();
|
|
|
|
pLock->unlock();
|
2017-10-09 22:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Thread, Lock)
|
|
|
|
{
|
2023-11-10 20:15:51 +00:00
|
|
|
CLock Lock;
|
|
|
|
Lock.lock();
|
2019-03-19 10:44:16 +00:00
|
|
|
void *pThread = thread_init(LockThread, &Lock, "lock");
|
2023-11-10 20:15:51 +00:00
|
|
|
Lock.unlock();
|
2017-10-09 22:08:24 +00:00
|
|
|
thread_wait(pThread);
|
|
|
|
}
|